VUE3傳值相關(guān)的6種方法總結(jié)
1.父傳子(props)
VUE3父傳子
1.將fatherToChild的值傳遞到index子組件之中并且在父組件中操作按鈕子組件數(shù)據(jù)會跟隨變化
<div class="bgc fed">
<div class="father">
<p>我是父組件</p>
<p>
<button @click="changeFather">更改父組件數(shù)值</button>
</p>
</div>
<index :fatherToChild="fatherToChild" />
</div>2.引入vue的import
import { defineComponent, ref, reactive, toRefs } from "vue";3.const 所要傳遞的數(shù)據(jù)并且寫上父組件的按鈕方法
const state = reactive({
fatherToChild: "fatherToChild",
});
const changeFather = () => {
state.fatherToChild = "changeFather";
};
return {
...toRefs(state),
changeFather,
};如圖所示為一開始傳遞fatherToChild在點擊按鈕之后傳遞數(shù)值變?yōu)閏hangeFather
4.在子組件之中接收所傳遞的props
export default {
props: {
fatherToChild: String,
},
setup(props) {
return {
props,
};
},
};如果不確定傳值為什么可cons一下傳遞的props在控制臺觀察
在子組件中引用為
<p>父組件給子組件傳值:{{ props.fatherToChild }}</p>效果如圖所示


可以觀察到點擊父組件后子組件數(shù)值跟隨變化
2.子傳父組件方法和值(emit)
同VUE2一樣子組件沒有辦法直接更改父組件方法,選用context.emit進(jìn)行方法傳遞
1.子組件之中寫方法觸發(fā)context,emit
setup(props, context) {
const childChangeFather = () => {
context.emit("childUseFather", "childUseFather");
};
return {
props,
childChangeFather,
};
},context.emit(‘方法名’,數(shù)值)
2.父組件之中接收方法后調(diào)用更改數(shù)值
const childUseFather = (val) => {
state.fatherToChild = val;
}; <index
:fatherToChild="fatherToChild"
@childUseFather="childUseFather" />效果如圖所示
點擊前

點擊后

3.子傳父(v-model)
如果子傳父所更改的值恰好跟父組件相同可用v-model
1.父組件之中
<index v-model:fatherToChild="fatherToChild" />
不再需要使用@和:
2.子組件之中
const modelChange = () => {
context.emit("update:fatherToChild", "modelChangeFather");
};使用update方法,context.emit("update:方法名","傳值")
效果如圖所示
點擊前

點擊后

同時父組件更改數(shù)值子組件也會同樣跟隨變化

4.父組件調(diào)用子組件方法(ref)
就是使用 ref 來獲取dom 然后操作里面的參數(shù)和方法。
父組件之中
<p><button @click="fatherUse">ref父組件調(diào)用子組件方法</button></p>
<index ref="child" /> const child = ref();
const fatherUse = () => {
child.value.fatherUseChild("fatherChangeChild");
};在組件上綁定ref,起名為child在調(diào)用時使用child.value.方法名(“傳值”)
子組件之中
const state = reactive({
children: "children",
});
// 父組件調(diào)用子組件方法并且進(jìn)行傳值更改
const fatherUseChild = (val) => {
console.log("調(diào)用成功");
state.children = val;
};接收到傳值val,觸發(fā)方法給children重新賦值
效果如圖所示

點擊最后一個按鈕父組件調(diào)用時

可以觀察到子組件之中子組件的值變化為fatherChangeChild
5.VUEX
文件目錄結(jié)構(gòu)如圖所示

index.js之中
import { createStore } from 'vuex'
import {state} from './state'
import {mutations} from './mutations'
import {actions} from './actions'
export default createStore({
state,
mutations,
actions,
modules: {
}
})注意如果引入的時候沒寫{}在后續(xù)會導(dǎo)致store.state.字段找不到,需要store.state.state.字段才能查到數(shù)據(jù),只有好不好用就不知道了= =
首先在state之中聲明一個count
const state = {
count: 0,
}
export { state }然后在頁面之中進(jìn)行引用時
<template>
<div class="bgc fed">
<div class="father">
<p>我是父組件</p>
<p>vuex中的數(shù)據(jù):{{ store.state.count }}</p>
<child />
</div>
</div>
</template>
<script>
import child from "@/views/loginComponents/child.vue";
import { defineComponent } from "vue";
import { useStore } from "vuex";
export default defineComponent({
components: {
child,
},
name: "index",
setup() {
const store = useStore();
return {
store,
};
},
});
</script>
<style scoped>
.father {
height: 600px;
width: 200px;
background: rgb(255, 199, 146);
margin: 0 auto;
}
</style>可以通過console一下store來觀察數(shù)據(jù)是什么
如果想要對數(shù)據(jù)進(jìn)行更改需要在mutations文件中進(jìn)行操作
const mutations = {
addCount(state, payload) {
state.count += payload
},
changeCount(state, payload) {
state.count = payload
}
}
export { mutations }如圖為遞增和值替換兩種方法
const changeState = () => {
store.commit("addCount", 1);
};使用的時候直接store.commit即可
效果如圖所示

每次點擊遞增1
如果想要進(jìn)行異步操作的話 在actions文件中調(diào)用mutations方法來實現(xiàn)
const actions = {
asyncAddStoreCount(store, payload) { // 第一個參數(shù)是vuex固定的參數(shù),不需要手動去傳遞
store.commit("addCount", payload)
},
}
export { actions }使用的時候
const asyncChangeStoreCount = () => {
setTimeout(() => {
// asyncAddStoreCount是mutations中的方法,2是傳遞過去的數(shù)據(jù)
// 異步改變vuex用dispatch方法,這里用setTimeout模擬異步操作
store.dispatch("asyncAddStoreCount", 2);
}, 1000);
};用一個計時器模擬一下
效果圖如圖所示

點擊父

可以發(fā)現(xiàn)三個組件之中數(shù)據(jù)都跟著發(fā)生了變化,點擊子孫組件同樣。
在父組件之中點擊異步后點擊更改,可以發(fā)現(xiàn)數(shù)據(jù)先加了1兩秒之后又加了2
總結(jié)
到此這篇關(guān)于VUE3傳值相關(guān)的6種方法總結(jié)的文章就介紹到這了,更多相關(guān)VUE3傳值方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?內(nèi)置組件?component?的用法示例詳解
這篇文章主要介紹了vue內(nèi)置組件component的用法,本文給大家介紹了component內(nèi)置組件切換方法,通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
vue 路由視圖 router-view嵌套跳轉(zhuǎn)的實現(xiàn)
這篇文章主要介紹了vue 路由視圖 router-view嵌套跳轉(zhuǎn),主要實現(xiàn)的內(nèi)容有制作一個登錄頁面,跳轉(zhuǎn)到首頁,首頁包含菜單欄、頂部導(dǎo)航欄、主體,標(biāo)準(zhǔn)的后臺網(wǎng)頁格式,菜單點擊顯示不同的頁面,感興趣的小伙伴請參考下面文章內(nèi)容2021-09-09
深入探究Vue2響應(yīng)式原理的實現(xiàn)及存在的缺陷
Vue的響應(yīng)式數(shù)據(jù)機(jī)制是其核心特性之一,它能夠自動追蹤數(shù)據(jù)的變化,并實時更新相關(guān)的視圖,然而,Vue2中的響應(yīng)式數(shù)據(jù)機(jī)制并非完美無缺,本文將探討Vue2響應(yīng)式原理及其存在的缺陷2023-08-08

