vue 中幾種傳值方法(3種)
前言
vue項目中,組件跟組件之間數(shù)據(jù)的傳遞是很普遍的行為,在這里總結(jié)幾種常見的vue組件跟組件之間傳值方式,其中,主要有父子組件,非父子組件傳值。
父組件向子組件傳值
方法:父組件內(nèi)設(shè)置要傳的數(shù)據(jù),在父組件中引用的子組件上綁定一個自定義屬性并把數(shù)據(jù)綁定在自定義屬性上,在子組件添加參數(shù)props接收即可。具體可參考官方文檔。
父組件傳遞參數(shù)代碼如下:
<template> <center-template :form='userinfo'></center-template> </template> <script> import CenterTemplate from '../../components/admin/userCenterTemplate' export default { components: { 'center-template': CenterTemplate }, data () { return { userinfo: {name: 'jack'} } }, } </script>
上面代碼,通過在子組件上面綁定動態(tài)參數(shù):form='userinfo'將父組件中的參數(shù)傳遞給子組件,子組件就可以通過props來進(jìn)行接收。
子組件接收參數(shù)代碼如下:
... export default { props: { // 接收 form: { userinfo: Object } } }, } // 另一種寫法 export default { props: { // 接收 form: ['userinfo'] } }, }
上面代碼中,還可以使用數(shù)組來接受參數(shù),但是不能指定參數(shù)的類型。
子組件向父組件傳值
方法:子組件通過vue實例方法$emit進(jìn)行觸發(fā)并且可以攜帶參數(shù),父組件監(jiān)聽使用@(v-on)進(jìn)行監(jiān)聽,然后進(jìn)行方法處理。
子組件向上傳值
<template> <ul class="letter_city"> <li @click="selectItem('子組件向父組件傳值')"> </li> </ul> </template> <script> export default { methods: { selectItem(value) { this.$emit('selectItems', value) } } } </script>
上面代碼中,this指代的是vue實例,子組件通過$emit向父組件觸發(fā)事件和傳遞參數(shù)。
父組件監(jiān)聽子組件傳來的值
<template> <city-pages @selectItem='selectItem'></city-pages> </template> <script> import cityPages from './cityPages' export default { components: { cityPages }, methods: { selectItem(value) { console.log(value) // 子組件向父組件傳值 } } } </script>
上面代碼中,在子組件中監(jiān)聽方法,如果子組件觸發(fā)方法,父子間這邊就可以得到子組件傳過來的參數(shù)了。
非父子組件傳值一
Event BUS總線方法:通過新建一個vue實例,來實現(xiàn)$on接收和$emit 來觸發(fā)事件
1、新建bus.js文件:
// common/bus.js import Vue from 'vue'; // 使用 Event Bus const bus = new Vue(); export default bus;
2、組件1(接收通知信息)
import bus from '@/common/bus.js' export default{ data(){ return { collapseData: '' } }, created() { // 監(jiān)聽collapse,有變動就會收到通知,并改變collapseData值 bus.$on('collapse', msg => { this.collapseData = msg }) } }
3、組件2(發(fā)布信息)
import bus from '@/common/bus.js' export default { methods: { sendData(){ // 發(fā)布信號,觸發(fā)這個函數(shù),其他的接收函數(shù)都會收到相應(yīng)的信息 bus.$emit('collapse', '信息') } } }
非父子組件傳值二
借組vux插件實現(xiàn)組件之間的傳值。
1、通過npm加載vuex,創(chuàng)建store.js文件,然后在main.js中引入,store.js文件代碼如下:
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); const state = { message:'Hello World' }; const mutations = { newMessage(state,msg){ state.message = msg } } export default new Vuex.Store({ state, mutations })
3、在組件中存vuex的值,一般如下:
state里面的值只能通過Action來提交來修改和賦值。
<template> <div> <input type="text" @blur=saveName(username) v-model="username"> </div> </template> <script type="text/javascript"> // 引入mapActions,很重要 import { mapActions } from 'vuex' export default { data() { return { username:'', password: '' } }, methods: { ...mapActions({ // 在input 的blur 事件中觸發(fā)回調(diào),并將輸入值作為參數(shù)返回到store中 saveName: 'saveName' }) } } </script>
3、在組件中獲取Vuex的值,一般如下:
<template> <div id="list"> {{_name}} </div> </template> <script> import {mapState} from 'vuex' export default { name: 'List', data() { return{} }, computed: { // 1普通寫法 // name() { return this.$store.state.name } // 2簡潔寫法 //...mapState(['name']) // 3重命名 ...mapState({_name: "name"}) } </script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3使用vuedraggable實現(xiàn)拖拽功能
這篇文章主要為大家詳細(xì)介紹了vue3使用vuedraggable實現(xiàn)拖拽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Vue Router根據(jù)后臺數(shù)據(jù)加載不同的組件實現(xiàn)
本文主要介紹了根據(jù)用戶所購買服務(wù)的不同,有不同的頁面展現(xiàn)。文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08vue的異步數(shù)據(jù)更新機(jī)制與$nextTick用法解讀
這篇文章主要介紹了vue的異步數(shù)據(jù)更新機(jī)制與$nextTick用法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03SpringBoot+Vue3實現(xiàn)文件的上傳和下載功能
上傳文件和下載文件是我們平時經(jīng)常用到的功能,接下來就讓我們用SpringBoot,Vue3和ElementPlus組件實現(xiàn)文件的上傳和下載功能吧,感興趣的朋友跟隨小編一起看看吧2023-01-01vue-router beforeEach跳轉(zhuǎn)路由驗證用戶登錄狀態(tài)
這篇文章主要介紹了vue-router beforeEach跳轉(zhuǎn)路由驗證用戶登錄狀態(tài),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12