Vue基礎知識簡介4:watch、$set $delete、自定義指令、元件
阿新 • • 發佈:2019-02-10
var vm = null; window.onload=function(){ vm = new Vue({//vm對應的是例項化 el:'#my',// 2.0不允許掛載到html,body元素上 data:{ name:'moris', user:{ name:'sonia', } }, methods:{ //新增對應的屬性和值,物件屬性 add(){ this.$set(this.user,'age',22); }, //修改 upd(){ this.user.age=23; }, //刪除 del(){ this.$delte(this.user,'age'); } }, computed:{ } }); }
<div id="my">
<h2>{{user.name}}</h2>
<h2>{{user.age}}</h2>
<button @click="add">add</button>
<button @click="upd">upd</button>
<button @click="del">del</button>
</div>
指令 |
|
元件 |
//元件建立方式,全域性元件 //方式1 //先建立 var myComponent=Vue.extend({ template:'<h2>hello</h2>' }); //後引用 Vue.component('hello',myComponent); //方式2 Vue.component('world',{ template:'<h2>wold</h2>' }); var vm = null; window.onload=function(){ vm = new Vue({ el:'#my',// 2.0不允許掛載到html,body元素上 data:{ name:'moris', age:22, user:{ id:100 } }, methods:{ /*change(){ this.name="tom" }*/ }, components:{//區域性元件 'addressed':{ template:'#myaddress', data(){ return{ addresse:"wuhan123", arr:['a','b'] } } } } }); }
|