vue新手问题,求问各路大佬.为什么我点击电影名后不变色

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    .nmsl{
        background: red;
    }
</style>
<body>
<div id="app">
    <ul>
        <li v-for="(item,index) in movies" v-on:click="dj(index)" :class="{nmsl:bs[index],line:true}">{{index}}--{{item}}</li>
    </ul>
</div>
<script src="../js/vue.js"></script>
<script>
    const aoo=new Vue({
        el:"#app",
        data:{
            movies: ['海王','海尔兄弟','火影忍者','死神'],
            bs:[false,true,false,false]
        },
        methods:{
            dj:function(n){
                console.log(n);
                for(let i=0;i<this.bs.length;i++)
                {
                    if(i==n)
                    {
                        this.bs[i]=true;
                        console.log(i+"true");
                    }
                    else
                    {
                        this.bs[i]=false;
                        console.log(i+"false");
                    }

                }
            },
        }
    })
</script>
</body>
</html>

求问各路大佬.为什么我点击电影名后不变色

vue的数组不支持这种更新方式,你可以把变更后的数组整个重新赋值

https://cn.vuejs.org/v2/guide/list.html#数组更新检测

我也不知道你的为什么不可以,我觉得可能是 `nmsl:bs[index]` 这里有问题

我换了一个思路,你可以看看。我的思路是用activeIndex实时保存最新点击的index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    .nmsl{
        background: red;
    }
</style>
<body>
<div id="app">
    <ul>
        <li v-for="(item,index) in movies" v-on:click="dj(index)" :class="{ nmsl : activeIndex === index}">{{index}}--{{item}}</li>
    </ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    const aoo=new Vue({
        el:"#app",
        data:{
            movies: ['海王','海尔兄弟','火影忍者','死神'],
            bs:[false,true,false,false],
            activeIndex: 1
        },
        methods:{
            dj:function(n){
                console.log(n)
                this.activeIndex = n;
                for(let i=0;i<this.bs.length;i++)
                {
                    if(i==n)
                    {
                        this.bs[i]=true;
                        console.log(i+"true");
                    }
                    else
                    {
                        this.bs[i]=false;
                        console.log(i+"false");
                    }

                }
            },
        }
    })
</script>
</body>
</html>

 

好的,谢谢各位大佬支招

因为页面没有重绘, 把你赋值 改成this.$set  就行啦  !你For的代码有点多余  ,我给你精简下  

            dj:function(n){
                for(let i=0;i<this.bs.length;i++)
                {
                    this.$set(this.bs,`${i}`, i==n)
                }
            },

 

你的关注和采纳  是我给你解决问题的动力,也是对我的肯定

居然没采纳我的