vue 中幾種傳值方法(3種)
前言
vue項目中,組件跟組件之間數據的傳遞是很普遍的行為,在這里總結幾種常見的vue組件跟組件之間傳值方式,其中,主要有父子組件,非父子組件傳值。
父組件向子組件傳值
方法:父組件內設置要傳的數據,在父組件中引用的子組件上綁定一個自定義屬性并把數據綁定在自定義屬性上,在子組件添加參數props接收即可。具體可參考官方文檔。
父組件傳遞參數代碼如下:
<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)參數:form='userinfo'將父組件中的參數傳遞給子組件,子組件就可以通過props來進行接收。
子組件接收參數代碼如下:
...
export default {
props: {
// 接收
form: { userinfo: Object }
}
},
}
// 另一種寫法
export default {
props: {
// 接收
form: ['userinfo']
}
},
}
上面代碼中,還可以使用數組來接受參數,但是不能指定參數的類型。
子組件向父組件傳值
方法:子組件通過vue實例方法$emit進行觸發(fā)并且可以攜帶參數,父組件監(jiān)聽使用@(v-on)進行監(jiā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ā)事件和傳遞參數。
父組件監(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ā)方法,父子間這邊就可以得到子組件傳過來的參數了。
非父子組件傳值一
Event BUS總線方法:通過新建一個vue實例,來實現$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ā)這個函數,其他的接收函數都會收到相應的信息
bus.$emit('collapse', '信息')
}
}
}
非父子組件傳值二
借組vux插件實現組件之間的傳值。
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ā)回調,并將輸入值作為參數返回到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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue-router beforeEach跳轉路由驗證用戶登錄狀態(tài)
這篇文章主要介紹了vue-router beforeEach跳轉路由驗證用戶登錄狀態(tài),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

