為vue中的data賦值computed計(jì)算屬性后,出現(xiàn)undefined原因及解決
為vue中的data賦值computed計(jì)算屬性后,出現(xiàn)undefined原因
今天遇到一個(gè)問(wèn)題,當(dāng)我給data數(shù)據(jù)初始化一個(gè)computed計(jì)算屬性時(shí),在頁(yè)面上竟然沒(méi)有顯示,放代碼
<template>
<div class="container">
<div>num1:{{num1}}</div>
<div>num2:{{num2}}</div>
<div>data中的數(shù)據(jù){{sum}}</div>
</div>
</template>
<script>
export default {
data() {
return {
num1:2,
num2:10,
sum:this.result
};
},
computed:{
result(){
return this.num1+this.num2
}
}
};
</script>
console.log(this.data)一下,結(jié)果是undefined

查了一下資料,原來(lái)是跟Vue組件數(shù)據(jù)初始化順序有關(guān),我們可以看一下源碼new Vue的執(zhí)行順序

往下找,可以找到在這個(gè)initMixin函數(shù)里,定義了_init的方法
function initMixin (Vue) {
Vue.prototype._init = function (options) { //在這里定義了_init方法
var vm = this;
// a uid
vm._uid = uid$3++;
var startTag, endTag;
/* istanbul ignore if */
if (config.performance && mark) {
startTag = "vue-perf-start:" + (vm._uid);
endTag = "vue-perf-end:" + (vm._uid);
mark(startTag);
}
// a flag to avoid this being observed
vm._isVue = true;
// merge options
if (options && options._isComponent) {
// optimize internal component instantiation
// since dynamic options merging is pretty slow, and none of the
// internal component options needs special treatment.
initInternalComponent(vm, options);
} else {
vm.$options = mergeOptions(
resolveConstructorOptions(vm.constructor),
options || {},
vm
);
}
/* istanbul ignore else */
{
initProxy(vm);
}
// expose real self
vm._self = vm;
initLifecycle(vm);
initEvents(vm);
initRender(vm);
callHook(vm, 'beforeCreate');
initInjections(vm); // resolve injections before data/props
initState(vm);
initProvide(vm); // resolve provide after data/props
callHook(vm, 'created');
/* istanbul ignore if */
if (config.performance && mark) {
vm._name = formatComponentName(vm, false);
mark(endTag);
measure(("vue " + (vm._name) + " init"), startTag, endTag);
}
if (vm.$options.el) {
vm.$mount(vm.$options.el);
}
};
}其中,聚焦在這里

看來(lái)initState(vm)是關(guān)鍵的部分了,在initState函數(shù)里
function initState (vm) {
vm._watchers = [];
var opts = vm.$options;
if (opts.props) { initProps(vm, opts.props); }//初始化props
if (opts.methods) { initMethods(vm, opts.methods); }//初始化methods
if (opts.data) {
initData(vm);
} else {
observe(vm._data = {}, true /* asRootData */);
} //初始化data
if (opts.computed) { initComputed(vm, opts.computed); }//初始化computed
if (opts.watch && opts.watch !== nativeWatch) {
initWatch(vm, opts.watch);//初始化watch
}
}從initState(vm)函數(shù)可以看出
初始化數(shù)據(jù)的順序如下
propsmethodsdatacomputedwatch
computed的初始化在data的后邊!
這也就證實(shí)了為什么為data賦值計(jì)算屬性computed時(shí),data打印出來(lái)是undefined
回到上邊的例子
<template>
<div class="container">
<div>num1:{{num1}}</div>
<div>num2:{{num2}}</div>
<div>data中的數(shù)據(jù){{sum}}</div>
</div>
</template>
<script>
export default {
data() {
return {
num1:2,
num2:10,
sum:this.result
};
},
computed:{
result(){
return this.num1+this.num2
}
}
};
</script>因?yàn)?code>computed初始化順序是在data后邊。
當(dāng)sum初始化的時(shí)候,result還沒(méi)有初始化,所以也就輸出undefined的結(jié)果
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vue中的methods、computed計(jì)算屬性和watch監(jiān)聽(tīng)屬性的使用和區(qū)別解析
- Vue中computed(計(jì)算屬性)和watch(監(jiān)聽(tīng)屬性)的用法及區(qū)別說(shuō)明
- vue計(jì)算屬性computed--getter和setter用法
- vue計(jì)算屬性computed方法內(nèi)傳參方式
- vue正確使用watch監(jiān)聽(tīng)屬性變化方式
- Vue中的?watch監(jiān)聽(tīng)屬性詳情
- Vue3計(jì)算屬性computed和監(jiān)聽(tīng)屬性watch區(qū)別解析
相關(guān)文章
詳解如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的 vuex
本篇文章主要介紹了如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的 vuex,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
vue created鉤子函數(shù)與mounted鉤子函數(shù)的用法區(qū)別
這篇文章主要介紹了vue created鉤子函數(shù)與mounted鉤子函數(shù)的用法區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
vue.js 子組件無(wú)法獲取父組件store值的解決方式
今天小編就為大家分享一篇vue.js 子組件無(wú)法獲取父組件store值的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue使用Echarts設(shè)置數(shù)據(jù)無(wú)效問(wèn)題記錄及解決方法
這篇文章主要介紹了vue使用Echarts設(shè)置數(shù)據(jù)無(wú)效問(wèn)題記錄,本文通過(guò)場(chǎng)景分析給大家分享解決方法,需要的朋友可以參考下2022-08-08
Vue首屏加載出現(xiàn)白屏問(wèn)題的優(yōu)化實(shí)戰(zhàn)
在實(shí)際開(kāi)發(fā)中,Vue?應(yīng)用首次加載時(shí)經(jīng)常出現(xiàn)白屏問(wèn)題,這嚴(yán)重影響了用戶(hù)體驗(yàn),本文將從多個(gè)角度出發(fā),提供全面的解決方案,大家可以根據(jù)需要進(jìn)行選擇2025-05-05
Vue3實(shí)現(xiàn)預(yù)覽PDF文件的多種方式(超簡(jiǎn)單)
在Vue項(xiàng)目中實(shí)現(xiàn)PDF文件預(yù)覽是許多開(kāi)發(fā)者可能會(huì)遇到的需求,尤其是在開(kāi)發(fā)海外后臺(tái)管理系統(tǒng)時(shí),由于某些用戶(hù)上傳的文件格式為PDF,而Vue本身并不直接支持PDF文件的預(yù)覽功能,這就需要借助一些第三方的插件或者工具來(lái)完成,下面詳細(xì)地介紹幾種在Vue3中實(shí)現(xiàn)PDF文件預(yù)覽的方法2025-03-03
教你用vue實(shí)現(xiàn)一個(gè)有趣的圍繞圓弧動(dòng)畫(huà)效果
最近做的兩個(gè)項(xiàng)目都是關(guān)于vue的,做完整理一下,這篇文章主要給大家介紹了關(guān)于如何用vue實(shí)現(xiàn)一個(gè)有趣的圍繞圓弧動(dòng)畫(huà)效果的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

