《Vue.js實戰》筆記 第七章(重點)
阿新 • • 發佈:2018-12-16
Vue.component("first-component",{
template:'\
<button @click="handleIncrease">+1</button>\
<button @click="handleReduce">-1</button>\
',
data: function(){
return {
counter: 0
}
} ,
methods: {
handleIncrease:function(){
this.counter++;
this.$emit("increase",this.counter);
},
handleReduce:function(){
this.counter--;
this.$emit("reduce",this.counter);
}
}
});
Vue.component("second-component",{
template:'\
<button @click="handleClick">+1</button>\
',
data: function(){
return {
counter: 0
}
},
methods: {
handleClick:function() {
this.counter++;
this.$emit("input",this.counter);
}
}
});
Vue.component("third-component",{
props:['value'],
template:'<input :value="value" @input="updateValue">',
methods: {
updateValue:function(event) {
this.$emit("input",event.target.value);
}
}
});
var bus = new Vue();
Vue.component("forth-component",{
template:'<button @click="handleEvent">傳遞事件</button>',
methods: {
handleEvent:function(){
bus.$emit("on-message","來自元件forth的內容");
this.$parent.msg = "來自forth的內容,修改了父鏈"
}
}
});
Vue.component("fifth-component",{
template:'<p>ref=comA的子元件</p>',
data:function(){
return {
message:"fifth子元件的內容"
}
}
})
var first = new Vue({
el:"#first",
data:{
total: 0,
totals: 0,
totales: 0,
msg:"Parent",
message:"",
refmsg:"",
},
mounted:function() {
var _this = this ;
bus.$on("on-message",function(msg){
_this.message = msg;
})
},
methods:{
handleGetTotal:function(total){
this.total = total;
},
handleRe:function(){
this.totales--;
},
handleRef:function(){
var msg = this.$refs.comA.message;
this.refmsg = msg;
console.log(msg);
}
}
})