vue使用this.$options.data() 无法获取初始值

如题,在使用this.$options.data()时,若data值改变,this.$options.data()值也会随之改变。

解决:在单页面中引入newVue实例时data若作为一个对象,例如:

    newVue({
      el: '#container',
      data: {
        // 任务中心
        taskCenter: {
          // 提交记录进度条
          progressList: [],
         }//this.$options.data()会随data中的值改变

应为

    newVue({
      el: '#container',
      data(){
        return{
        innn:false,
        // 任务中心
        taskCenter: {
          // 提交记录进度条
          progressList: [],
          TaskNames: ["submitProductparamTiming", "updateProductSourceMaterialBatch", "saveProductparamList", 'updateProductDetailBySet'],
        },

因为对象是引用类型,this.$options.data()返回的值和this.$data 都指向 data 的引用地址 所以当你改变this.data的时候,这两个值都会改变。如果你把data改成 data(){return {text:'aaa'}} 返回一个新对象 。this.$options.data()返回的值和this.$data 都指向不同的地址。这个时候改变this.data时 this.$options.data()返回值就不会改变。

参考地址:javascript - vue 中的 this.$options.data() 无法获取初始值 - SegmentFault 思否

的评论区一楼回答

猜你喜欢

转载自blog.csdn.net/weixin_42574985/article/details/130810724
今日推荐