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>

 

效果

 

添加前:

添加后:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM