vue中的data,computed,methods,created,mounted用法及說明
vue中的data,computed,methods,created,mounted
介紹一下vue的基本結(jié)構(gòu)中各分類的含義以及他們的執(zhí)行順序
含義及執(zhí)行順序
1.首先created是最先執(zhí)行的,它是一個(gè)生命周期,也叫鉤子函數(shù),用于頁面還沒加載完畢時(shí)發(fā)送請(qǐng)求,初始化data里的值。
2.data一般用來存放vue中的變量,通過return將數(shù)據(jù)返回到created中
3.methods用來寫相關(guān)模塊的函數(shù)以及功能的實(shí)現(xiàn),格式為async+方法名,通常通過發(fā)送請(qǐng)求請(qǐng)求到后臺(tái)數(shù)據(jù)
4.computed是界于created和mounted之間發(fā)生的,此時(shí)正是頁面進(jìn)行渲染的時(shí)候
5.mounted是加載完dom操作后才會(huì)觸發(fā),它和created一樣只會(huì)執(zhí)行一次,此時(shí)頁面已經(jīng)渲染完成,通常用于存放需要渲染的內(nèi)容
下面通過代碼段來簡單描述
export default{
? ? data(){
? ? ? datas:[],
//這里的data是一個(gè)空數(shù)組用來存放從后端獲取到的數(shù)據(jù)
}
? ? methods:{
? ? ? ?async getDatas(){
? ? ? ? let res = await get("/index/carousel/findAll");//發(fā)送請(qǐng)求
? ? ? ? this.datas = res.data;//將請(qǐng)求到的數(shù)據(jù)返回到空數(shù)組里
}
}
? ? created(){
? ? ?this.getDatas()//生命周期 還沒加載時(shí)發(fā)送請(qǐng)求
}
}不一定要每個(gè)都用到,根據(jù)自身需求而應(yīng)用
vue2頁面和mixins中相同的data created mounted computed watch methods優(yōu)先級(jí)
data
頁面中定義的 data 會(huì)覆蓋 mixins 中同名的 data

created、mounted
先執(zhí)行 mixins 中生命周期函數(shù),再執(zhí)行頁面中的生命周期函數(shù)。

watch
先執(zhí)行 mixins 中的 watch 監(jiān)聽,再執(zhí)行頁面中同名的 watch 監(jiān)聽。

computed
頁面中定義的 computed 屬性會(huì)覆蓋 mixins 中同名的 computed 屬性。

methods
頁面中定義的 methods 覆蓋mixins中同名的 methods 。

演示代碼如下
頁面 html 代碼
<template>
<div>
<el-input v-model="value"></el-input>
<el-button type="primary" @click="onSubmit">submit</el-button>
<div>{{ text }}</div>
</div>
</template>頁面 js 代碼
export default {
components: {
DetailsTab,
DataTab,
},
mixins: [testMixin],
data() {
return {
pageData: 'this is page data',
commonData: 'this is common data in page',
value: '',
}
},
watch:{
value() {
console.log('page watch value');
},
},
computed: {
text() {
const text = `The input value is ${this.value}, this text is from page computed`;
console.log(text);
return text;
},
},
created() {
console.log('page created');
},
mounted() {
console.log('page mounted');
console.log(this.pageData);
console.log(this.commonData);
console.log(this.mixinsData);
},
methods: {
onSubmit() {
console.log('page methods');
},
},
}mixins 代碼
export default {
data() {
return {
mixinsData: 'this is mixins data',
commonData: 'this is common data in mixins',
}
},
watch:{
value() {
console.log('mixins watch value');
},
},
computed: {
text() {
const text = `The input value is ${this.value}, this text is from mixins computed`;
console.log(text);
return text;
},
},
created() {
console.log('mixins created');
},
mounted() {
console.log('mixins mounted');
},
methods: {
onSubmit() {
console.log('mixins methods');
},
},
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli 使用axios的操作方法及整合axios的多種方法
這篇文章主要介紹了vue-cli 使用axios的操作方法及整合axios的多種方法,vue-cli整合axios的多種方法,小編一一給大家列出來了,大家根據(jù)自身需要選擇,需要的朋友可以參考下2018-09-09
Vue.set()實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)響應(yīng)的方法
這篇文章主要介紹了Vue.set()實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)響應(yīng)的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02
vue項(xiàng)目中如何將數(shù)據(jù)導(dǎo)出為word文檔
這篇文章主要介紹了vue項(xiàng)目中如何將數(shù)據(jù)導(dǎo)出為word文檔問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Vue全局自適應(yīng)大小:使用postcss-pxtorem方式
本文介紹了如何在Vue項(xiàng)目中使用postcss-pxtorem插件實(shí)現(xiàn)響應(yīng)式設(shè)計(jì),postcss-pxtorem可以自動(dòng)將CSS文件中的px單位轉(zhuǎn)換為rem單位,從而實(shí)現(xiàn)更好的自適應(yīng)布局,通過配置postcss-pxtorem插件,可以在構(gòu)建時(shí)自動(dòng)完成轉(zhuǎn)換,無需手動(dòng)修改代碼2025-01-01
vue 實(shí)現(xiàn)拖拽動(dòng)態(tài)生成組件的需求
這篇文章主要介紹了vue 如何實(shí)現(xiàn)拖拽動(dòng)態(tài)生成組件的需求,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-05-05
詳解vuex結(jié)合localstorage動(dòng)態(tài)監(jiān)聽storage的變化
這篇文章主要介紹了詳解vuex結(jié)合localstorage動(dòng)態(tài)監(jiān)聽storage的變化,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
前端低代碼form-generator實(shí)現(xiàn)及新增自定義組件詳解
這篇文章主要給大家介紹了關(guān)于前端低代碼form-generator實(shí)現(xiàn)及新增自定義組件的相關(guān)資料,form-generator是一個(gè)開源的表單生成器,可以幫助我們快速構(gòu)建各種表單頁面,需要的朋友可以參考下2023-11-11

