欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談Vue的組件間傳值(包括Vuex)

 更新時(shí)間:2021年08月16日 17:33:06   作者:RisingSun  
這篇文章主要介紹了Vue的組件間傳值(包括Vuex),全文通過(guò)舉例子及代碼的形式進(jìn)行了一個(gè)簡(jiǎn)單的介紹,希望大家能夠理解并且學(xué)習(xí)到其中知識(shí)

在不使用Vuex的情況下,組件間傳值的方式是通過(guò)父?jìng)髯拥姆绞交蛘咝值芙M件傳值。

父?jìng)髯?

fatherComponent:

<template>
    <div>
        <HELLOWORLD :needData="content"></HELLOWORLD>
    </div>
</template>

<script>
import HELLOWORLD from '../components/HelloWorld.vue'
export default {
    components:{
        HELLOWORLD
    },
    data(){
        return{
            content:"content"
        }
    }
}
</script>

<style lang="less" scoped>

</style>

SonComponent(子組件名稱(chēng)為HELLOWORLD):

<template>
    <div>
        <h1>HELLOWORLD</h1>
    </div>
</template>

<script>
export default {
    props:["needData"],
    data(){
        return{
            H:this.needData,
        }
    },
    mounted(){
        console.log(this.H);
    }
}
</script>

<style lang="less" scoped>

</style>

在這里插入圖片描述

子傳父:

FatherComponent:

<template>
    <div>
        <HELLOWORLD @sendData="getData"></HELLOWORLD>
    </div>
</template>

<script>
import HELLOWORLD from '../components/HelloWorld.vue'
export default {
    components:{
        HELLOWORLD
    },
    data(){
        return{
            
        }
    },
    methods:{
        getData(sonData){
            console.log("data=>",sonData);
        },
    }
}
</script>

<style lang="less" scoped>

</style>

SonComponent:

<template>
    <div>
        <h1>HELLOWORLD</h1>
    </div>
</template>

<script>
export default {
    data(){
        return{
            content:"content"
        }
    },
    mounted(){
        this.$emit("sendData",this.content);
    }
}
</script>

<style lang="less" scoped>

</style>

效果圖:

在這里插入圖片描述

實(shí)際上,為了數(shù)據(jù)能在父子組件間傳值;還可以通過(guò)調(diào)用父組件的函數(shù)或調(diào)用子組件的函數(shù)的方式實(shí)現(xiàn)傳值。 Vue中子組件調(diào)用父組件的函數(shù)

http://www.dbjr.com.cn/article/134732.htm

Vue父組件調(diào)用子組件的函數(shù)

http://www.dbjr.com.cn/article/219793.htm

Vuex是Vue框架中不可或缺的一部分;

Vuex在需要多組件通信的時(shí)候顯得格外重要;比如數(shù)據(jù)在父組件形成,但數(shù)據(jù)需要在子組件的子組件中使用時(shí),就可以使用Vuex管理;或者說(shuō)需要兄弟組件傳值時(shí),可以使用Vuex。

在Vue的store.js中有五個(gè)屬性:
分別是state,mutations,actions,getters,modules

結(jié)構(gòu)為:

let a={
  state: {
  	name:"moduleA"
  },
  //mutations專(zhuān)門(mén)用于改變state屬性中的數(shù)據(jù)
  mutations: {
  	setFun(state,item){
		state.name=item;
	}
  }
}

export default new Vuex.Store({
  //state專(zhuān)門(mén)存放數(shù)據(jù)
  state: {
  	num:100,
  	useAcomponent:{
		name:"A",
	},
	useBcomponent:"content",
  },
  //mutations專(zhuān)門(mén)用于改變state屬性中的數(shù)據(jù)
  mutations: {
  	setStateFun(state,item){
		state.useBcomponent="Bcomponent";
	}
  },
  actions: {
  	httpGetData(store,item){
		setTimeout(()=>{
			console.log(item);
			store.commit("setStateFun",item);
		},3000)
	}
  },
  getters:{
  //調(diào)用getters中的函數(shù)時(shí)沒(méi)有入?yún)?
	getterFun1(state){
		return state.num++
	}
  //調(diào)用getters中的函數(shù)時(shí)有入?yún)?
  	gettterFun2(state){
		return function(val){
			return state.num+=val;
		}
	}
  },
  modules: {
  	ModuleA:a
  }
});
}

state中的數(shù)據(jù)可以在不同組件中訪問(wèn)獲取。

獲取state的數(shù)據(jù):

this.$store.state.state對(duì)象中的數(shù)據(jù);
例如
let val=this.$store.state.num;

更改state數(shù)據(jù),就是調(diào)用Vuex的mutations對(duì)象中的函數(shù):

this.$store.commit("函數(shù)名","數(shù)據(jù)");
例如
this.$store.commit("setStateFun","testSetItem");

actions對(duì)象,用于在Vuex中發(fā)請(qǐng)求

this.$store.dispatch("函數(shù)名","數(shù)據(jù)");
例如
this.$store.dispatch("httpGetData","testItem");

getters對(duì)象,類(lèi)似Vue的計(jì)算屬性

this.$store.getters.函數(shù)名;
例如
//沒(méi)入?yún)r(shí)
this.$store.getters.getterFun1;
//有入?yún)r(shí)
this.$store.getters.getterFun2(123);

modules對(duì)象,類(lèi)似將需要使用的store模塊化分開(kāi),每個(gè)modules對(duì)象對(duì)應(yīng)一個(gè)模塊

//獲取modules對(duì)象中的state數(shù)據(jù)
this.$store.state.modules對(duì)象名.state值;
例如
this.$store.state.ModuleA.name
//使用modules對(duì)象中的mutations的函數(shù)
this.$store.commit("函數(shù)名","入?yún)?shù)據(jù)");
例如
this.$store.commit("setFun","itemabc");
//這里需要注意,如果modules模塊中與外部(不是modules對(duì)象模塊)的mutations對(duì)象中有相同名字的函數(shù)時(shí),則相同名字的函調(diào)用時(shí)都會(huì)執(zhí)行

到此這篇關(guān)于淺談Vue的組件間傳值(包括Vuex)的文章就介紹到這了,更多相關(guān)Vue 組件間傳值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue中Table組件Select的勾選和取消勾選事件詳解

    Vue中Table組件Select的勾選和取消勾選事件詳解

    這篇文章主要為大家詳細(xì)介紹了Vue中Table組件Select的勾選和取消勾選事件詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • Vue3中emit傳值的具體使用

    Vue3中emit傳值的具體使用

    Emit是Vue3中另一種常見(jiàn)的組件間傳值方式,它通過(guò)在子組件中觸發(fā)事件并將數(shù)據(jù)通過(guò)事件參數(shù)傳遞給父組件來(lái)實(shí)現(xiàn)數(shù)據(jù)傳遞,本文就來(lái)介紹一下Vue3 emit傳值,感興趣的可以了解一下
    2023-12-12
  • 使用use注冊(cè)Vue全局組件和全局指令的方法

    使用use注冊(cè)Vue全局組件和全局指令的方法

    下面小編就為大家分享一篇使用use注冊(cè)Vue全局組件和全局指令的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • 關(guān)于vue中的時(shí)間格式轉(zhuǎn)化問(wèn)題

    關(guān)于vue中的時(shí)間格式轉(zhuǎn)化問(wèn)題

    這篇文章主要介紹了關(guān)于vue中的時(shí)間格式轉(zhuǎn)化問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Vue.js路由組件vue-router使用方法詳解

    Vue.js路由組件vue-router使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Vue.js路由組件vue-router使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 利用vue.js實(shí)現(xiàn)被選中狀態(tài)的改變方法

    利用vue.js實(shí)現(xiàn)被選中狀態(tài)的改變方法

    下面小編就為大家分享一篇利用vue.js實(shí)現(xiàn)被選中狀態(tài)的改變方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • Vue中ref、reactive、toRef、toRefs、$refs的基本用法總結(jié)

    Vue中ref、reactive、toRef、toRefs、$refs的基本用法總結(jié)

    這篇文章主要給大家介紹了關(guān)于Vue中ref、reactive、toRef、toRefs、$refs的基本用法,以及他們之家的區(qū)別,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • 解決vue 綁定對(duì)象內(nèi)點(diǎn)擊事件失效問(wèn)題

    解決vue 綁定對(duì)象內(nèi)點(diǎn)擊事件失效問(wèn)題

    今天小編就為大家分享一篇解決vue 綁定對(duì)象內(nèi)點(diǎn)擊事件失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • vue2.0 子組件改變props值,并向父組件傳值的方法

    vue2.0 子組件改變props值,并向父組件傳值的方法

    下面小編就為大家分享一篇vue2.0 子組件改變props值,并向父組件傳值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • vue中echarts視圖不更新問(wèn)題及解決

    vue中echarts視圖不更新問(wèn)題及解決

    這篇文章主要介紹了vue中echarts視圖不更新問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評(píng)論