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除了寫(xiě)成數(shù)組還可以寫(xiě)成對(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ǔ)的類(lèi)型檢查 (`null` 和 `undefined` 會(huì)通過(guò)任何類(lèi)型驗(yàn)證) propA: Number, // 多個(gè)可能的類(lèi)型 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è)工廠(chǎng)函數(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)解決該需求,這里可以查看我之前寫(xiě)的一篇筆記
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于vue 項(xiàng)目中瀏覽器跨域的配置問(wèn)題
這篇文章主要介紹了vue 項(xiàng)目中瀏覽器跨域的配置問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11vue前端路由以及vue-router兩種模式實(shí)例詳解
路由這個(gè)概念最先是后端出現(xiàn)的,下面這篇文章主要給大家介紹了關(guān)于vue前端路由以及vue-router兩種模式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03uniapp組件uni-file-picker中對(duì)上傳的圖片進(jìn)行壓縮至1兆以?xún)?nèi)(推薦)
我在做uniapp項(xiàng)目時(shí),用的uni-file-picker組件,這是我做的一個(gè)項(xiàng)目實(shí)例,主要是將圖片通過(guò)接口傳至后臺(tái)服務(wù)器,本文給大家分享uniapp組件uni-file-picker中對(duì)上傳的圖片進(jìn)行壓縮再上傳,感興趣的朋友跟隨小編一起看看吧2022-11-11Vue3 構(gòu)建 Web Components使用詳解
這篇文章主要為大家介紹了Vue3 構(gòu)建 Web Components使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09淺談vue權(quán)限管理實(shí)現(xiàn)及流程
這篇文章主要介紹了淺談vue權(quán)限管理實(shí)現(xiàn)及流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04