Javascript vue异步数据不工作
在呈现vue组件之前,我尝试使用vue异步数据异步获取数据,但没有成功。我没有得到任何错误,但它根本不工作 下面是我的main.js代码:Javascript vue异步数据不工作,javascript,vue.js,vuejs2,vue-component,Javascript,Vue.js,Vuejs2,Vue Component,在呈现vue组件之前,我尝试使用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,
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 }
})
<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>
从“Vue”导入Vue
从“vue异步数据”导入VueAsyncData
从“./路由器”导入路由器
从“./App.vue”导入应用程序
Vue.use(VueAsyncData)
新Vue({
el:“#应用程序”,
路由器,
模板:“”,
组件:{App}
})
下面是我的App.vue代码:
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 }
})
<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}}
从“./components/Navigation.vue”导入导航
导出默认值{
名称:“应用程序”,
组成部分:{
航行
},
数据:函数(){
返回{
消息:“尚未加载…”
}
},
asyncData:函数(解析、拒绝){
//加载数据和调用解析(数据)
//如果出现问题,请致电reject(reason)
setTimeout(函数(){
//这将为您调用'vm.$set('msg','hi')`
决心({
味精:“嗨”
})
}, 1000)
}
}
msg值在任何时候都不会更改,但仍会渲染组件
我遗漏了什么吗?正如Bert Evans所说,vue异步数据不适用于vue 2.0 我使用vue路由器和创建的函数来实现我需要的功能(如中所建议的)
加载。。。
{{error}}
从“./components/Navigation.vue”导入导航
导出默认值{
名称:“应用程序”,
组成部分:{
航行
},
数据:函数(){
返回{
加载:对,
错误:false,
当前用户:空
}
},
已创建:函数(){
this.fetchUserData()
},
方法:{
fetchUserData:函数(){
这是.http.get('/Account/CurrentUserInfo')。然后(数据=>{
this.currentUser=数据
此参数为0。加载=错误
},回应=>{
此参数为0。加载=错误
this.error=true
});
}
}
}
“不适用于Vue 2.0。”哦,谢谢,我没有注意到这一点。您是否建议使用另一种形式来满足我的需要?看起来您正在使用Vue router
。您可以在路由之前使用。。谢谢,我决定改用这种方法,这是我在您发送的指南中找到的:。