1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@include file="/WEB-INF/webpage/common/taglibs.jspf"%> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>${title}</title> <meta name="decorator" content="single"/> <script src="/static/common/js/vue.js"></script>
</head> <body> <div id="app"> <div v-for="n in arr"> n-{{n}} </div> <template v-for="(v,k) in user"> <div> <span>{{k}}=</span> <span>{{v}}</span> </div> </template> <input v-model="property"> <button @click="addProperty">addProperty</button> <button @click="rootAdd">rootAdd</button> <button @click="arrlist">arrlist</button> </div>
<script>
var app=new Vue({ el:"#app", data:{ property:"xp", user:{ name:"user1", }, arr:[1,2,3] }, methods:{ arrlist:function(){ Vue.set(this.arr,0,123); }, addProperty:function(){ // this.user.age=123; // Vue.set(this.user,this.property,"1_1"); 这种写法也是对的 this.$set(this.user,this.property,"1_1"); console.info(this.user) }, rootAdd:function(){ //Vue 不允许动态添加根级响应式属性,所以你必须在初始化实例前声明根级响应式属性,哪怕只是一个空值 this.$set(this.data,"a",1); },
} }) </script> </body>
</html>
|