Vue动态添加响应式属性

 

不能给Vue实例、Vue实例的根数据对象添加属性。

 

文件

 

<template>
  <div id="app">
    <h2>{{hello}}:{{animal.name}}</h2>
  </div>
</template>

<script>
export default {
  name: "app",
  data: () => {
    return {
      hello: "你好",
      animal: {}
    };
  },
  mounted() {
    setTimeout(() => {
      this.$set(this.animal, "name", "大象🐘");
    }, 3000);
  }
};
</script>

<style>
#app {
  color: gold;
}
</style>

 

效果

 

添加前:

添加后:

 

posted on 2019-06-19 09:57  沙滩海风  阅读(2532)  评论(0编辑  收藏  举报

导航