添加新的键,以使空数据对象在VUEJS中不起作用

发布于 2025-02-05 14:14:48 字数 399 浏览 0 评论 0原文

我有与VUE数据有关的问题。

我用一个空对象创建了数据。

data(){
 return {
    myObj: {}
 }
}

并这样的功能:

methods: {
  changeMyObj() {
    this.myObj.newKey = 'aaaa';
  }
}

然后,我单击此单击,在模板上显示它

  <a @click="changeMyObj">Click change</a>
     {{myObj.newKey}}

,嵌套键不会在模板上呈现。如何解决这个问题?

注意:我不遇到Vuex或React状态的问题。

I have question related to data of Vue.

I created data with an empty object.

data(){
 return {
    myObj: {}
 }
}

and function like this:

methods: {
  changeMyObj() {
    this.myObj.newKey = 'aaaa';
  }
}

Then I show it on template by click

  <a @click="changeMyObj">Click change</a>
     {{myObj.newKey}}

With this click, the nested key is not rendered on template. How can I resolve this issue?

Note: I do not meet this issue with Vuex or state of React.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆萌少年 2025-02-12 14:14:49

这是因为 vue.js反应性。实际上,在这里,您正在修改一个值时未声明的值,并且VUE无法跟踪更改。


您可以使用vue.set方法更新值,该方法允许VUE跟踪数据。

Vue.set(this.myObj, "myKey", "Hello world")

您还可以对对象进行深层复制,而不仅仅是添加密钥。

可以使用 spread operator

例如,

this.myObj = {...this.myObj, myKey: "Hello world"}

这里是使用两个版本的一个小示例

new Vue({
  el: "#app",
  data: () => ({
    myObj: {}
  }),
  
  methods: {  
    addKey(){
      this.myObj = {...this.myObj, myKey: "Hello world foo"}
    },
    addKey2(){
      Vue.set(this.myObj, "myKey", "Hello world bar")
    },
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  {{ myObj.myKey }}
  <button @click="addKey">Add key</button>
  <button @click="addKey2">Add key 2</button>
</div>

This happens because of vue.js reactivity. In fact, here you are modifying a value that was not declared when the component mounted and Vue cannot track the changes.


You can update values by using the Vue.set method which allows Vue to track the data.

Vue.set(this.myObj, "myKey", "Hello world")

You can also make a deep copy of the object instead of just adding the key.

This can be done using the spread operator.

For example

this.myObj = {...this.myObj, myKey: "Hello world"}

Here is a small example using the two versions

new Vue({
  el: "#app",
  data: () => ({
    myObj: {}
  }),
  
  methods: {  
    addKey(){
      this.myObj = {...this.myObj, myKey: "Hello world foo"}
    },
    addKey2(){
      Vue.set(this.myObj, "myKey", "Hello world bar")
    },
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  {{ myObj.myKey }}
  <button @click="addKey">Add key</button>
  <button @click="addKey2">Add key 2</button>
</div>

可爱暴击 2025-02-12 14:14:49

我找到了解决方案。

this.myObj = {...this.myObj, newKey:'aaaa'}

我认为这不是解决方案,尽管它没有区别:

this.myObj['newKey'] = 'aaaa';
or
this.myObj.newKey = 'aaaa';

如果有人可以解释为什么请告诉我。谢谢

I found the solution for this.

this.myObj = {...this.myObj, newKey:'aaaa'}

I do not think it is solution while it has no difference with:

this.myObj['newKey'] = 'aaaa';
or
this.myObj.newKey = 'aaaa';

If someone can explain why please let me know. Thanks

你曾走过我的故事 2025-02-12 14:14:49

在现有对象中分配新属性的正确方法是vue.set(this.myobj,'newkey','aaaa')使其具有反应性,而不是this.myobj.newkey = 'aaaa'

demo

new Vue({
  el: '#app',
  data: {
    myObj: {}
  },
  methods: {
    changeMyObj() {
      Vue.set(this.myObj, 'newKey', 'aaaa')
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="changeMyObj">Click change</button>
    {{ myObj.newKey }}
</div>

Correct way to assign a new property in an existing object is Vue.set(this.myObj, 'newKey', 'aaaa') to make it reactive instead of this.myObj.newKey = 'aaaa'

Demo :

new Vue({
  el: '#app',
  data: {
    myObj: {}
  },
  methods: {
    changeMyObj() {
      Vue.set(this.myObj, 'newKey', 'aaaa')
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="changeMyObj">Click change</button>
    {{ myObj.newKey }}
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文