vue2.0中組件傳值的幾種方式總結(jié)
搭建好測(cè)試環(huán)境

app.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<child></child>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
import Child from './components/Child.vue'
export default {
name: 'App',
components: {
HelloWorld,
Child
}
}
</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>
Child.vue
````html
!<template>
<div class="Child">
<h1>這是子組件</h1>
</div>
</template>
<script>
export default {
name:'Child',
data() {
return {
}
},
}
</script>
<style>
</style>

1.方法一
父?jìng)髯?/h3>
父組件向子組件使用props傳值
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<child :sendParam="send"></child>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
import Child from './components/Child.vue'
export default {
name: 'App',
components: {
HelloWorld,
Child
},
data() {
return {
send:'父組件傳給子組件的值'
}
},
}
</script>
<template>
<div class="Child">
<h1>這是子組件</h1>
<div>{{sendParam}}</div>
</div>
</template>
<script>
export default {
name:'Child',
props:['sendParam'],
data() {
return {
}
},
}
</script>

這里的props除了寫成數(shù)組還可以寫成對(duì)象的方式:
<script>
export default {
name:'Child',
data() {
return {
}
},
//方法一:用數(shù)組獲取值
// , props:['sendParam'],
//方法二:用對(duì)象獲取值
props:{
sendParam:String,
}
}
</script>
所以說(shuō)在父組件給子組件傳值的時(shí)候是可以傳對(duì)象,布爾值,函數(shù)等,在子組件接收值的時(shí)候也可以做相應(yīng)的限制或設(shè)置默認(rèn)值。以對(duì)象接收時(shí)有type,default,require等參數(shù)可以設(shè)置,詳細(xì)的內(nèi)容可參考官網(wǎng)的文檔。
Vue.component('my-component', {
props: {
// 基礎(chǔ)的類型檢查 (`null` 和 `undefined` 會(huì)通過(guò)任何類型驗(yàn)證)
propA: Number,
// 多個(gè)可能的類型
propB: [String, Number],
// 必填的字符串
propC: {
type: String,
required: true
},
// 帶有默認(rèn)值的數(shù)字
propD: {
type: Number,
default: 100
},
// 帶有默認(rèn)值的對(duì)象
propE: {
type: Object,
// 對(duì)象或數(shù)組默認(rèn)值必須從一個(gè)工廠函數(shù)獲取
default: function () {
return { message: 'hello' }
}
},
// 自定義驗(yàn)證函數(shù)
propF: {
validator: function (value) {
// 這個(gè)值必須匹配下列字符串中的一個(gè)
return ['success', 'warning', 'danger'].indexOf(value) !== -1
}
}
}
})
子傳父
子組件向父組件傳值需要使用自定義事件
<template>
<div class="Child">
<h1>這是子組件</h1>
<div>{{sendParam}}</div>
<button @click="run">子傳父</button>
</div>
</template>
<script>
export default {
name:'Child',
data() {
return {
childata:'這是子傳父的值'
}
},
props:['sendParam'],
methods: {
run(){
this.$emit('event',this.childata)
}
},
}
</script>
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<child :sendParam="send" @event="reviceChild"></child>
<div>{{revice}}</div>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
import Child from './components/Child.vue'
export default {
name: 'App',
components: {
HelloWorld,
Child
},
data() {
return {
send:'父組件傳給子組件的值',
revice:''
}
},
methods: {
reviceChild(val){
this.revice=val
}
},
}
</script>

子組件中的事件不一定要用click點(diǎn)擊事件,還可以是各種可觸發(fā)的事件,比如mouseover,dbclick等。
2.方法二
在父組件中加上一個(gè)ref屬性并打印其結(jié)果
<child :sendParam="send" @event="reviceChild" ref="child"></child>
console.log(this.$refs.child);

在結(jié)果中我們發(fā)現(xiàn)了很多子組件Child中有的方法,數(shù)據(jù)。

結(jié)果表明,我們打印的this.$refs.child就是整個(gè)子組件,也就是說(shuō),我們可以在這里直接拿到子組件的值。父組件拿子組件的值同理。
父?jìng)髯?/h3>
<button @click="getParent">獲取父組件的值</button>
getParent(){
console.log(this.$parent.send)
}
<button @click="getParent">獲取父組件的值</button>
getParent(){
console.log(this.$parent.send)
}

子傳父
<child :sendParam="send" @event="reviceChild" ref="child"></child> <button @click="go">獲取ref</button>
go(){
console.log(this.$refs.child.childata);
}

奇怪的傳值
父組件中添加:that="this"
<child :sendParam="send" @event="reviceChild" ref="child" :that="this"></child>
子組件中props接收
props:{
sendParam:{
type:String,
default:'111'
},
that:{}
},
在生命周期中輸出
mounted() {
console.log(this.that)
},

這里也可以拿到整個(gè)父組件
mounted() {
console.log(this.that.send)//子組件拿到父組件的值
},
3.方法三
vue提供了provide,inject來(lái)實(shí)現(xiàn)如下場(chǎng)景的組件傳值,父組件向子組件跨級(jí)傳值

父組件:
export default {
name: 'App',
components: {
HelloWorld,
Child
},
provide:{
psend:'123456'
},
}
子組件:
export default {
name:'Child',
data() {
return {
}
},
inject:['psend'],
mounted() {
console.log(this.psend);
},
}

4.兄弟組件之間傳值
這里vue提供了vuex來(lái)解決該需求,這里可以查看我之前寫的一篇筆記
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue props 單項(xiàng)數(shù)據(jù)流實(shí)例分享
在本篇文章里小編給大家分享的是一篇關(guān)于vue props 單項(xiàng)數(shù)據(jù)流實(shí)例分享內(nèi)容,需要的朋友們可以參考下。2020-02-02
vue實(shí)現(xiàn)宮格輪轉(zhuǎn)抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)宮格輪轉(zhuǎn)抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
強(qiáng)烈推薦!Vue3.2中的setup語(yǔ)法糖
script?setup是vue3的新語(yǔ)法糖,并不是新增的功能模塊,只是簡(jiǎn)化了以往的組合式API必須返回(return)的寫法,并且有更好的運(yùn)行時(shí)性能,這篇文章主要給大家介紹了關(guān)于Vue3.2中setup語(yǔ)法糖的相關(guān)資料,需要的朋友可以參考下2021-12-12
vue中ts無(wú)法識(shí)別引入的vue文件,提示找不到xxx.vue模塊的解決
這篇文章主要介紹了vue中ts無(wú)法識(shí)別引入的vue文件,提示找不到xxx.vue模塊的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
vue中echarts關(guān)系圖動(dòng)態(tài)增刪節(jié)點(diǎn)以及連線方式
這篇文章主要介紹了vue中echarts關(guān)系圖動(dòng)態(tài)增刪節(jié)點(diǎn)以及連線方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue實(shí)現(xiàn)的父組件向子組件傳值功能示例
這篇文章主要介紹了Vue實(shí)現(xiàn)的父組件向子組件傳值功能,結(jié)合完整實(shí)例形式簡(jiǎn)單分析了vue.js組件傳值的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
iview給radio按鈕組件加點(diǎn)擊事件的實(shí)例
下面小編就為大家?guī)?lái)一篇iview給radio按鈕組件加點(diǎn)擊事件的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
vue2.0移動(dòng)端滑動(dòng)事件vue-touch的實(shí)例代碼
這篇文章主要介紹了vue2.0移動(dòng)端滑動(dòng)事件vue-touch的實(shí)例代碼,需要的朋友可以參考下2018-11-11
vue.js將unix時(shí)間戳轉(zhuǎn)換為自定義時(shí)間格式
這篇文章主要介紹了vue.js將unix時(shí)間戳轉(zhuǎn)換為自定義時(shí)間格式的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

