Vue中父子組件的值傳遞與方法傳遞
一.Vue中父組件向子組件傳值
利用v-bind向子組件傳值,子組件中利用props接受
<div id="app">
<father></father>
</div>
<template id="father">
<div>
<!--注意點(diǎn): 組件是可以使用自己的數(shù)據(jù)的-->
<p>{{name}}</p>
<p>{{age}}</p>
<!--這里將父組件的name通過parentname傳遞給了子組件-->
<son :parentname="name" :abc="age"></son>
</div>
</template>
<template id="son">
<div>
<!--這里通過parentname使用了父組件傳遞過來的數(shù)據(jù)-->
<p>{{parentname}}</p>
<p>{{abc}}</p>
</div>
</template>
<script>
// 父組件
Vue.component("father", {
template: "#father",
data: function(){
return {
name: "wqd",
age: 21
}
},
// 子組件
components: {
"son": {
template: "#son",
//這里通過parentname接收了父組件傳遞過來的數(shù)據(jù)
props: ["parentname", "abc"]
}
}
});
// 這里就是MVVM中的View Model
let vue = new Vue({
el: '#app',
// 這里就是MVVM中的Model
data: {
},
});
</script>
二.Vue中父組件向子組件傳遞方法
父組件利用v-on傳值,子組件this.$emit來接收
<div id="app">
<father></father>
</div>
<template id="father">
<div>
<button @click="say">我是按鈕</button>
<!--這里通過parentsay將父組件的say方法傳遞給了子組件-->
<son @parentsay="say"></son>
</div>
</template>
<template id="son">
<div>
<button @click="sonFn">我是按鈕</button>
</div>
</template>
<script>
// 父組件
Vue.component("father", {
template: "#father",
methods: {
say(){
console.log("helloworld")
}
},
// 子組件
components: {
"son": {
template: "#son",
/*
注意點(diǎn): 和傳遞數(shù)據(jù)不同, 如果傳遞的是方法, 那么在子組件中不需要接收,需要在子組件自定義的方法中通this.$emit("自定義接收的名稱")的方法來觸發(fā)父組件傳遞過來的方法
*/
// props: ["parentsay"]
methods: {
sonFn(){
this.$emit("parentsay");
}
}
}
}
});
// 這里就是MVVM中的View Model
let vue = new Vue({
el: '#app',
// 這里就是MVVM中的Model
data: {
},
});
</script>
三.Vue中子組件向父組件傳值
this.$emit中第一個(gè)參數(shù)為接收父組件傳遞的方法,第二個(gè)參數(shù)即為向父組件傳遞的值
<div id="app">
<father></father>
</div>
<template id="father">
<div>
<button @click="say">我是按鈕</button>
<!--這里通過parentsay將父組件的say方法傳遞給了子組件-->
<son @parentsay="say"></son>
</div>
</template>
<template id="son">
<div>
<button @click="sonFn">我是按鈕</button>
</div>
</template>
<script>
// 父組件
Vue.component("father", {
template: "#father",
methods: {
//data用來接受子組件傳遞的值
say(data){
console.log(data);
}
},
// 子組件
components: {
"son": {
template: "#son",
methods: {
sonFn(){
// 第一個(gè)參數(shù): 需要調(diào)用的函數(shù)名稱
// 后續(xù)的參數(shù): 給調(diào)用的函數(shù)傳遞的參數(shù)
this.$emit("parentsay", "你好");
}
}
}
}
});
// 這里就是MVVM中的View Model
let vue = new Vue({
el: '#app',
// 這里就是MVVM中的Model
data: {
},
});
</script>
到此這篇關(guān)于Vue中父子組件的值傳遞與方法傳遞的文章就介紹到這了,更多相關(guān)Vue父子組件傳遞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue父子組件的互相傳值和調(diào)用
- vue 使用 v-model 雙向綁定父子組件的值遇見的問題及解決方案
- vue中父子組件的參數(shù)傳遞和應(yīng)用示例
- Vue父子組件傳值的一些坑
- 詳解vue父子組件狀態(tài)同步的最佳方式
- Vue 組件的掛載與父子組件的傳值實(shí)例
- vue中組件通信詳解(父子組件, 爺孫組件, 兄弟組件)
- vue中父子組件傳值,解決鉤子函數(shù)mounted只運(yùn)行一次的操作
- 快速了解Vue父子組件傳值以及父調(diào)子方法、子調(diào)父方法
- vue父子組件間引用之$parent、$children
- vue.js使用v-model實(shí)現(xiàn)父子組件間的雙向通信示例
- vuejs中父子組件之間通信方法實(shí)例詳解
- Vue組件通信中非父子組件傳值知識點(diǎn)總結(jié)
- Vue 使用Props屬性實(shí)現(xiàn)父子組件的動(dòng)態(tài)傳值詳解
- vue父子組件的通信方法(實(shí)例詳解)
- vue-父子組件和ref實(shí)例詳解
- vue父子組件通信的高級用法示例
- Vue中的父子組件通訊以及使用sync同步父子組件數(shù)據(jù)
相關(guān)文章
vue-cli3項(xiàng)目打包后自動(dòng)化部署到服務(wù)器的方法
這篇文章主要介紹了vue-cli3項(xiàng)目打包后自動(dòng)化部署到服務(wù)器的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Vue?import?from省略后綴/加載文件夾的方法/實(shí)例詳解
本文介紹Vue在import時(shí)省略后綴以及import文件夾的方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
iView-admin 動(dòng)態(tài)路由問題的解決方法
這篇文章主要介紹了iView-admin 動(dòng)態(tài)路由問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10

