vue组件间值传递
夫传子
父组件代码

子组件代码

效果展示

<template>
<h1>{{ msg }}</h1>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
msg:{
type:String,
default:""
}
}
}
</script>
<style >
</style>
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<MyComponent :msg="msg2"/>
</div>
</template>
<script>
import MyComponent from './components/MyComponent.vue'
export default {
name: 'App',
data(){
return{
msg2:"Welcome222"
}
},
components: {
MyComponent
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
子传父
父组件代码

子组件代码

<template>
<button @click="sendClickHandle">点击传递</button>
</template>
<script>
export default {
name: 'HelloWorld',
data(){
return{
msg:"Welcome222"
}
},
methods:{
sendClickHandle(){
this.$emit("onEvent",this.msg)
}
}
}
</script>
<style >
</style>
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<p>{{ msg2 }}</p>
<MyComponent @onEvent="getDataHandle"/>
</div>
</template>
<script>
import MyComponent from './components/MyComponent.vue'
export default {
name: 'App',
data(){
return{
msg2:""
}
},
components: {
MyComponent
},
methods:{
getDataHandle(data){
this.msg2 = data
}
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

浙公网安备 33010602011771号