vue中父子組件傳值,解決鉤子函數(shù)mounted只運行一次的操作
因為mounted函數(shù)只會在html和模板渲染之后會加載一次,但是在子組件中只有第一次的數(shù)據(jù)顯示是正常的,所以需要再增加一個updated函數(shù),在更新之后就可以重新進行取值加載,完成數(shù)據(jù)的正常顯示。
beforCreate(創(chuàng)建之前)
Created(創(chuàng)建之后)
beforMount(載入之前)
Mounted(載入之后)
beforUpdate(更新之前)
Updated(更新之后)
beforDestroy(銷毀之前)
Destroyed(銷毀之后)
activate(keep-alive組件激活時調(diào)用)
deactivated(keep-alive組件停用時調(diào)用)
errorCaptured(這個組件的作用是接受子孫組件報錯是調(diào)用,三個參數(shù) 錯誤對象、錯誤的組件、錯誤信息)
父組件向
子組件傳值
通過父組件傳值調(diào)用子組件顯示不同的數(shù)據(jù)
父組件 :conponent.vue
子組件:iconponent.vue
父組件
<template>
<div>
<span>父組件</span>
<icomponent-box :id="this.id"></icomponent-box>
</div>
</template>
<script>
//導入子組件
import icomponent from './icomponent.vue';
export default {
data () {
return {
id:12
}
},
components:{ //用來注冊子組件的節(jié)點
"icomponent-box": icomponent
}
}
</script>
<style>
</style>
子組件
<template>
<div>子組件</div>
</template>
<script>
export default {
updated:{
this.getComponents();
},
mounted:{
this.getComponents();
},
data () {
return {
}
},
methods:{
getComponents(){
this.$http.get("api/getcomponents/" + this.id);
}
},
props: ["id"] //接收父組件傳遞過來的id值
}
</script>
補充知識:Vue父子組件傳值,子組件數(shù)據(jù)只更新一次,之后更改父組件的數(shù)據(jù),子組件不再更新
我就廢話不多說了,大家還是直接看代碼吧~
props:{
reportInfo:{
type:Object,
default:()=>{}
}
},
data:function(){
return {
cityName :' ',
reportId :' ' ,
}
}
mounted:function () {
var _this = this;
//初始化獲得數(shù)據(jù),之后在watch中監(jiān)聽更新
_this.cityName = _this.reportInfo.cityName;
_this.reportId = _this.reportInfo.reportId;
},
watch:{
reportInfo(newValue, oldValue) {
var _this = this;
_this.cityName = newValue.cityName;
_this.reportId = newValue.reportId;
}
}
以上這篇vue中父子組件傳值,解決鉤子函數(shù)mounted只運行一次的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue調(diào)用谷歌授權(quán)登錄獲取用戶通訊錄的實現(xiàn)示例
本文主要介紹了vue調(diào)用谷歌授權(quán)登錄獲取用戶通訊錄的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
vue3 開始時間與結(jié)束時間比較驗證(結(jié)束時間需要大于開始時間)
本文通過示例代碼介紹了vue3 開始時間與結(jié)束時間比較驗證(結(jié)束時間需要大于開始時間)的相關(guān)操作,代碼簡單易懂,感興趣的朋友跟隨小編一起看看吧2024-07-07
Vue+FormData+axios實現(xiàn)圖片上傳功能
這篇文章主要為大家學習介紹了Vue如何利用FormData和axios實現(xiàn)圖片上傳功能,本文為大家整理了詳細步驟,感興趣的小伙伴可以了解一下2023-08-08
Vue.js中使用${}實現(xiàn)變量和字符串的拼接方式
這篇文章主要介紹了Vue.js中使用${}實現(xiàn)變量和字符串的拼接方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue?quill-editor?編輯器使用及自定義toobar示例詳解
這篇文章主要介紹了Vue quill-editor編輯器使用及自定義toobar示例詳解,這里講解編輯器quil-editor的知識結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07

