我目前正在使用WordPress REST和vue路由器来在一个小页面站点上的页面之间进行转换。但是,当我使用REST对服务器进行AJAX调用时,数据会加载,但只在页面已经呈现之后。
vue-路由器文档提供了关于如何在导航到每个路由之前和之后加载数据的深入了解,但我想知道如何在初始页面加载时加载所有路由和页面数据,避免每次路由被激活时都需要加载数据。
注意,我正在将数据加载到acf属性中,然后使用this.$parent.acfs访问.vue文件组件中的数据。
main.js路由器代码:
const router = new VueRouter({
routes: [
{ path: '/', component: Home },
{ path: '/about', component: About },
{ path: '/tickets', component: Tickets },
{ path: '/sponsors', component: Sponsors },
],
hashbang: false
});
exports.router = router;
const app = new Vue({
router,
data: {
acfs: ''
},
created() {
$.ajax({
url: 'http://localhost/placeholder/wp-json/acf/v2/page/2',
type: 'GET',
success: function(response) {
console.log(response);
this.acfs = response.acf;
// this.backgroundImage = response.acf.background_image.url
}.bind(this)
})
}
}).$mount('#app')Home.vue组件代码:
export default {
name: 'about',
data () {
return {
acf: this.$parent.acfs,
}
},
}有什么想法吗?
https://stackoverflow.com/questions/41572974
复制相似问题