子组件通过 $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: `{{ message }}
`,
data: function () {
return {
par:'',
}
},
methods: {
incrementHandler: function (v){
this.message = +this.message;
if (v == 1) {
this.message -= 1
this.$emit('love', 1)
} else {
this.message += 1
this.$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>
- 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