• vue学习(九)对象变更检测注意事项


    Vue不能检测对象属性的添加和删除,要是必须这么做的话 

    需要使用 vue.$set()

    <body>
    
    <div id="app">
        <h3>
            {{user.name}}
       
            {{user.age}}              <!--this.user.age=20 这个是不会成功的-->
        </h3>
        <button @click="handlerAdd">添加属性</button>
    </div>
    
    <script>
        // vue 不能检测对象属性的添加和删除
        // 要用 vue的方法 Vue.set
        let app = new Vue({
            el:'#app',
            data:{
                user:''
            },
            methods:{
              handlerAdd(){
                  // vue.$set(object,key,value)
                  this.$set(this.user,'age',20)
                  // this.user.age=20 这个是不会成功的
              }
            },
            created(){
                setTimeout(()=>{
                    this.user={
                        name:'张三'
                    }
                },1250)
            }
        })
    </script>
    </body>

    添加多个响应式属性

        <h3>
            {{user.name}}
            {{user.phone}}
            {{user.age}}              <!--this.user.age=20 这个是不会成功的-->
        </h3>
              handlerAdd(){
                  // 添加多个响应式
                  this.user = Object.assign({},this.user,{
                      age:20,
                      phone:13811091109
                  })
              }
  • 相关阅读:
    23种设计模式
    设计模式中类的关系
    简单工厂模式
    SQL正则表达式
    C#中各种计时器
    C# List 排序
    常见名词解释
    PetaPoco入门
    jQuery UI Dialog
    c# winform 获取当前程序运行根目录
  • 原文地址:https://www.cnblogs.com/a438842265/p/11935866.html
Copyright © 2020-2023  润新知