vue @click 传值_vue中组件通信的方式

我看了网上的博客,觉得不是特别详细,我也说说组件通信的方式,分别是父传子、子传父、兄弟传值!

父传子:

父组件App.vue中定义一个自定义属性title,然后在子组件中用props进行接收,最后用{{ title }}渲染页面

父组件App.vue代码:

<template>

<div id="app">

<big :title="msg"></big>

<smal></smal>

</div>

</template>

<script>

import Big from './components/Big'

import Smal from './components/Small'

export default {

name: 'App',

data() {

return {

msg:'吴刚'

}

},

components:{

Big,

Smal

},

}

</script>

<style>

#app {

font-family: 'Avenir', Helvetica, Arial, sans-serif;

-webkit-font-smoothing: antialiased;

-moz-osx-font-smoothing: grayscale;

text-align: center;

color: #2c3e50;

}

</style>

子组件Big.vue代码:

<template>

<div>

big{{ title }}

<button>传值</button>

</div>

</template>

<script>

export default {

data() {

return {

msg:'吴与卿'

};

},

props:{

title:{

type:String, //检测类型

default :"岳秀清" //默认值

}

},

created() {

},

mounted() {

},

};

</script>

<style scoped>

</style>

de13befa0f290b8db028df51935f28c3.png
吴刚是从父组件传递到子组件的值

子传父:

在子组件Big.vue中定义一个方法btn,用this.$emit给父组件App传值(第一个参数是自定义方法,第二个参数是要传递的值),父组件App.vue中用自定义的方法aaa接收子组件传过来的值( res=this.msg)

子组件Big.vue代码:

<template>

<div>

big{{ title }}

<button @click="btn">传值</button>

</div>

</template>

<script>

export default {

data() {

return {

msg:'吴与卿'

};

},

props:{

title:{

type:String,

default :"岳秀清"

}

},

created() {

},

mounted() {

},

methods: {

btn(){

console.log(8)

this.$emit('aaa',this.msg)

}

}

};

</script>

<style scoped>

</style>

父组件App.vue代码:

<template>

<div id="app">

<big @aaa="btn"></big>

<smal></smal>

</div>

</template>

<script>

import Big from './components/Big'

import Smal from './components/Small'

export default {

name: 'App',

data() {

return {

msg:'吴刚'

}

},

components:{

Big,

Smal

},

methods: {

btn(res){

console.log(3)

console.log(res)

}

},

}

</script>

<style>

#app {

font-family: 'Avenir', Helvetica, Arial, sans-serif;

-webkit-font-smoothing: antialiased;

-moz-osx-font-smoothing: grayscale;

text-align: center;

color: #2c3e50;

}

</style>

c1bb48f455773d83bea650d9fc1f6ad7.png
吴与卿是从自组件传递到父组件的值

兄弟组件传值:

创建一个第三方js文件,然后在Big.vue中引入这个第三方文件,在组件中第一一个方法btn,方法中用$emit("自定义方法","传递的值")进行传值,之后在Small.vue中引入第三方js文件,组件中用$on("自定义方法",()=>{})接收值

第三方bus.js文件代码:

import Vue from 'vue'

export default new Vue({

})

Big.vue代码:

<template>

<div>

big{{ title }}

<button @click="btn">传值</button>

</div>

</template>

<script>

//引入第三方文件

import bus from './bus'

console.log(bus)

export default {

data() {

return {

msg:'吴与卿'

};

},

created() {

},

mounted() {

},

methods: {

btn(){

bus.$emit('chuanzhi',{

name : "韩美珍"

})

}

}

};

</script>

<style scoped>

</style>

Small.vue代码:

<template>

<div>

small

{{ msg }}

</div>

</template>

<script>

import bus from './bus'

// console.log(bus)

export default {

data() {

return {

};

},

created() {

bus.$on('chuanzhi',(res)=>{

console.log(res)

this.msg = res.name

})

},

mounted() {

},

methods: {

},

};

</script>

<style scoped>

</style>

aaac466b6e280b4e888eba67b4b66519.png
韩美珍是Big组件传递到Small组件的值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值