Vue-18、Vue更新数据时的一个问题

1、修改凑效

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>更新时的一个问题</title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="root">
    <h2>人员列表</h2>
    <button @click="updatemei">更新马冬梅的信息</button>
    <ul>
        <li v-for="p in persons" :key="p.id">
            {{p.name}}-{{p.age}}-{{p.sex}}
        </li>
    </ul>
</div>
<script type="text/javascript">
    Vue.config.productionTip=false;
    new Vue({
        el:"#root",
        data:{
            message:'请输入名字搜索',
            keyword:'',
            persons:[
                {id:'1',name:'马冬梅',age:30,sex:'女'},
                {id:'2',name:'周冬雨',age:35,sex:'女'},
                {id:'3',name:'周杰伦',age:18,sex:'男'},
                {id:'4',name:'文兆伦',age:19,sex:'男'},
            ],
        },
        methods:{
            updatemei(){
                this.persons[0].name='马老师';
                this.persons[0].age='50';
                this.persons[0].sex='男';
            }
        }

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

2、修改失败

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>更新时的一个问题</title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="root">
    <h2>人员列表</h2>
    <button @click="updatemei">更新马冬梅的信息</button>
    <ul>
        <li v-for="p in persons" :key="p.id">
            {{p.name}}-{{p.age}}-{{p.sex}}
        </li>
    </ul>
</div>
<script type="text/javascript">
    Vue.config.productionTip=false;
    new Vue({
        el:"#root",
        data:{
            message:'请输入名字搜索',
            keyword:'',
            persons:[
                {id:'1',name:'马冬梅',age:30,sex:'女'},
                {id:'2',name:'周冬雨',age:35,sex:'女'},
                {id:'3',name:'周杰伦',age:18,sex:'男'},
                {id:'4',name:'文兆伦',age:19,sex:'男'},
            ],
        },
        methods:{
            updatemei(){
               // this.persons[0].name='马老师';
               // this.persons[0].age='50';
                //this.persons[0].sex='男';
                this.persons[0]= {id:'1',name:'马老师',age:50,sex:'男'};
            }
        }

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

当点击按钮时的的确确把内存中persons中的第一个修改了,但是vue并没有监测到本次变化。

你可能感兴趣的:(vue,vue.js,javascript,前端)