子组件通过 $emit 触发父组件的方法时,如果需要传递参数,可在方法名后面加可选参数,参数以逗号隔开。
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>子组件通过 $emit 触发父组件的方法时,如果需要传递参数,可在方法名后面加可选参数,参数以逗号隔开。</title>
</head>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script><body><div id="app"><div id="app"><input v-model="par"><button-counter v-on:love="incrementTotal" v-bind:message="par"></button-counter><br/></div></div>
</body>
<script>Vue.component('button-counter', {props:['message'],template: `<div><button v-on:click="incrementHandler(1)">-</button>{{ message }}<button v-on:click="incrementHandler(2)">+</button></div>`,data: function () {return {par:'',}},methods: {incrementHandler: function (v){this.message = +this.message;if (v == 1) {this.message -= 1this.$emit('love', 1)} else {this.message += 1this.$emit('love', 2)}}},})new Vue({el: '#app',data: {par:'', },methods: {incrementTotal: function (par) {if (par == 2) {console.log(123)this.par += 1} else {console.log(456)this.par -= 1}console.log(this.par,"tttt");}}})
</script></html>