看图
one
<template>
<div>
<img :src='imgurl' width=450 height=300/> 子给传父的图片
<App><h3>{{fuchuanzi}}</h3></App>
<el-button @click="toTwo()" type="success">给two传值</el-button>
</div>
</template>
<script>
import PubSub from 'pubsub-js'
export default {
name: "VueOne",
props: {
fuchuanzi: String
},
data() {
return {
imgurl: require("../assets/aaaa.jpg"),
};
},
mounted() {
PubSub.subscribe("hello", (mes, data) => {
alert("子收到了一条消息:"+data);
});
},
methods: {
toTwo(){
this.$bus.$emit('to-two', '您好兄弟two')
}
}
};
</script>
<style lang="scss" scoped>
</style>
app
<template>
<div id="app">
<HelloWorld msg="这是HelloWorld的部分"/>
<div id="user" width="500px">
<h1>这是two部分</h1>
<Two></Two>
</div>
<div id="one" width="500px">
<h1>这是one部分</h1>
<one :fuchuanzi="fuchuanzi1"/>
请输入发布的内容:<input v-model="msg"/>
<el-button @click="zzz()" type="danger">父亲发布消息</el-button>
</div>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
import Two from './components/Two.vue'
import One from './components/One.vue'
import PubSub from 'pubsub-js'
export default {
name: 'App',
components: {
HelloWorld,
Two,
One,
},
props: {
sayHello: Function
},
data() {
return {
fuchuanzi1: "父传给子的一段消息",
msg: ""
}
},
methods: {
zzz(){
PubSub.publish('hello', this.msg)
}
},
}
</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;
}
#user {
background: pink;
}
#one {
background: yellowgreen;
}
</style>
two
<template>
<div>
<h1>{{msg}}</h1>
<el-button @click="hello()" type="success">点我试试!</el-button>
</div>
</template>
<script>
export default {
name: 'Two',
data() {
return {
msg: '我是一个变量 子的变量传到父'
};
},
methods: {
hello() {
alert('子的方法在父应用')
}
},
mounted(){
this.$bus.$on('to-two', function (data) {
console.log(data,'哥哥two组件传过来的数据')
})
}
};
</script>
<style lang="scss" scoped>
</style>
|