我正在構建一個SPA,滾動導航正在填充基於部分組件的菜單項。Vue組件數據沒有從道具更新
在我Home.vue我導入scrollNav和部分這樣的:
<template>
<div class="front-page">
<scroll-nav v-if="scrollNavShown" @select="changeSection" :active-section="activeItem" :items="sections"></scroll-nav>
<fp-sections @loaded="buildNav" :active="activeItem"></fp-sections>
</div>
</template>
<script>
import scrollNav from '.././components/scrollNav.vue'
import fpSections from './fpSections.vue'
export default {
data() {
return {
scrollNavShown: true,
activeItem: 'sectionOne',
scrollPosition: 0,
sections: []
}
},
methods: {
buildNav(sections) {
this.sections = sections;
console.log(this.sections)
},
changeSection(e) {
this.activeItem = e
},
},
components: {
scrollNav,
fpSections
}
}
</script>
this.sections
最初是空的,因爲我填充此數組從fpSections.vue各個部分數據:
<template>
<div class="fp-sections">
<keep-alive>
<transition
@enter="enter"
@leave="leave"
:css="false"
>
<component :is="activeSection"></component>
</transition>
</keep-alive>
</div>
</template>
<script>
import sectionOne from './sections/sectionOne.vue'
import sectionTwo from './sections/sectionTwo.vue'
import sectionThree from './sections/sectionThree.vue'
export default {
components: {
sectionOne,
sectionTwo,
sectionThree
},
props: {
active: String
},
data() {
return {
activeSection: this.active,
sections: []
}
},
mounted() {
this.buildNav();
},
methods: {
buildNav() {
let _components = this.$options.components;
for(let prop in _components) {
if(!_components[prop].hasOwnProperty('data')) continue;
this.sections.push({
title: _components[prop].data().title,
name: _components[prop].data().name
})
}
this.$emit('loaded', this.sections)
},
enter(el) {
twm.to(el, .2, {
autoAlpha : 1
})
},
leave(el, done) {
twm.to(el, .2, {
autoAlpha : 0
})
}
}
}
</script>
的buildNav
方法循環通過各個組件的數據,並將其推到範圍this.sections
陣列然後將其發射回Home.vue
回到Home.vue this.sections
中填充了從fpSections.vue發出的數據並作爲道具傳回給它。
當我檢查Vue devtools時,道具正確傳遞,但數據不會更新。
我在這裏錯過了什麼?數據應該在父權利更新時對道具做出反應?
I我提交了這個問題並用計算出的道具解決了這個問題之後,稍微玩了一會兒:) – kmaar