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>
效果
添加前:
添加后: