This article shares with you the three commonly used value transfer methods in Vue. It has certain reference value. Friends in need can refer to it.
Parent component passes value to child component:
Parent component:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <template>
<p>
父组件:
<input type= "text" v-model= "name" >
<br>
<br>
<!-- 引入子组件 -->
<child :inputName= "name" ></child>
</p>
</template>
<script>
import child from './child'
export default {
components: {
child
},
data () {
return {
name: ''
}
}
}
</script>
|
Copy after login
Child component:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <template>
<p>
子组件:
<span>{{inputName}}</span>
</p>
</template>
<script>
export default {
props: {
inputName: String,
required: true
}
}
</script>
|
Copy after login
2. Child component passes value to parent component
Child component:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <template>
<p>
子组件:
<span>{{childValue}}</span>
<!-- 定义一个子组件传值的方法 -->
<input type= "button" value= "点击触发" @click= "childClick" >
</p>
</template>
<script>
export default {
data () {
return {
childValue: '我是子组件的数据'
}
},
methods: {
childClick () {
this. $emit ( 'childByValue' , this.childValue)
}
}
}
</script>
|
Copy after login
Parent component:
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 | <template>
<p>
父组件:
<span>{{name}}</span>
<br>
<br>
<!-- 引入子组件 定义一个on的方法监听子组件的状态-->
<child v-on:childByValue= "childByValue" ></child>
</p>
</template>
<script>
import child from './child'
export default {
components: {
child
},
data () {
return {
name: ''
}
},
methods: {
childByValue: function (childValue) {
this.name = childValue
}
}
}
</script>
|
Copy after login
3. Non-parent-child component passes value. (To transfer values between non-parent and child components, you need to define a public public instance file bus.js as an intermediate warehouse to transfer values, otherwise the value transfer effect between routing components will not be achieved.)
Public bus .js
1 2 3 | import Vue from 'vue'
export default new Vue()
|
Copy after login
Component A:
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 | <template>
<p>
A组件:
<span>{{elementValue}}</span>
<input type= "button" value= "点击触发" @click= "elementByValue" >
</p>
</template>
<script>
import Bus from './bus.js'
export default {
data () {
return {
elementValue: 4
}
},
methods: {
elementByValue: function () {
Bus. $emit ( 'val' , this.elementValue)
}
}
}
</script>
组件B:
<template>
<p>
B组件:
<input type= "button" value= "点击触发" @click= "getData" >
<span>{{name}}</span>
</p>
</template>
<script>
import Bus from './bus.js'
export default {
data () {
return {
name: 0
}
},
mounted: function () {
var vm = this
Bus. $on ( 'val' , (data) => {
console.log(data)
vm.name = data
})
},
methods: {
getData: function () {
this.name++
}
}
}
</script>
|
Copy after login
Related recommendations:
Detailed introduction to hook functions in Vue
The above is the detailed content of Three commonly used value transfer methods in Vue. For more information, please follow other related articles on the PHP Chinese website!