【发布时间】:2017-04-27 18:12:42
【问题描述】:
我正在尝试使用 vue-async-data 在渲染我的 Vue 组件之前异步获取数据,但我没有成功。我没有收到任何错误,但它根本不起作用。
这是我的 main.js 代码:
import Vue from 'vue'
import VueAsyncData from 'vue-async-data'
import router from './router'
import App from './App.vue'
Vue.use(VueAsyncData)
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
})
这是我的 App.vue 代码:
<template>
<div>
{{ msg }}
<navigation wait-for="async-data"></navigation>
</div>
</template>
<script>
import Navigation from './components/Navigation.vue'
export default {
name: 'app',
components: {
Navigation
},
data: function() {
return {
msg: 'not loaded yet...'
}
},
asyncData: function (resolve, reject) {
// load data and call resolve(data)
// or call reject(reason) if something goes wrong
setTimeout(function () {
// this will call `vm.$set('msg', 'hi')` for you
resolve({
msg: 'hi'
})
}, 1000)
}
}
</script>
msg 值在任何时候都不会改变,但组件仍会呈现。
我错过了什么吗?
【问题讨论】:
-
“不适用于 Vue 2.0。” github.com/vuejs/vue-async-data
-
哦,谢谢,没注意到。您是否建议另一种形式来实现我的需要?
-
您似乎在使用
vue-router。您可以使用beforeRouteEnter。 router.vuejs.org/en/advanced/navigation-guards.html -
谢谢,我决定改用这种方法,我在您发送的指南中找到了这种方法:router.vuejs.org/en/advanced/data-fetching.html。
标签: javascript vue.js vuejs2 vue-component