我目前正在使用WordPress REST API和vue路由在一个小型单页网站上的页面之间进行转换.然而,当我使用REST API对服务器进行AJAX调用时,数据会加载,但只有在页面已经呈现之后.

vue-router documentation提供了有关如何在导航到每个路由之前和之后加载数据的详细信息,但我想知道如何在初始页面加载时加载所有路由和页面数据,避免了每次激活路由时加载数据的需要.

注意,我正在将数据加载到acf属性中,然后使用this.$parent.acfs.vue文件组件中访问它.

main.js Router Code:

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 Component Code:

export default {
    name: 'about',
    data () {
        return {
            acf: this.$parent.acfs,
        } 
    },
}

有什么 idea 吗?

推荐答案

好吧,我终于弄明白了.我所做的只是在我的main.js文件中调用一个同步ajax请求,在该文件中我的根vue实例被实例化,并为请求的数据分配一个数据属性,如下所示:

main.js

let acfData;

$.ajax({
    async: false,
    url: 'http://localhost/placeholder/wp-json/acf/v2/page/2',
    type: 'GET',
    success: function(response) {
        console.log(response.acf);
        acfData = response.acf;
    }.bind(this)
})  

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: acfData 
    },
    created() {

    }
}).$mount('#app')

从这里,我可以在每个单独的.vue个文件/组件中使用拉取的数据,如下所示:

export default {
    name: 'app',
    data () {
    return {
        acf: this.$parent.acfs,
    }
},

最后,我使用以下方法在同一个.vue模板中呈现数据:

<template>
  <transition
      name="home"
      v-on:enter="enter"
      v-on:leave="leave"
      v-bind:css="false"
      mode="out-in"
    >
    <div class="full-height-container background-image home" v-bind:style="{backgroundImage: 'url(' + this.acf.home_background_image.url + ')'}">
      <div class="content-container">
        <h1 class="white bold home-title">{{ acf.home_title }}</h1>
        <h2 class="white home-subtitle">{{ acf.home_subtitle }}</h2>
        <div class="button-block">
          <a href="#/about"><button class="white home-button-1">{{ acf.link_title_1 }}</button></a>
          <a href="#/tickets"><button class="white home-button-2">{{ acf.link_title_2 }}</button></a>
        </div>
      </div>
    </div>
  </transition>
</template>

需要带走的最重要的信息是,所有ACF数据在一开始只被调用一次,而每次使用beforeRouteEnter (to, from, next)之类的数据访问一条路由.因此,我能够根据需要获得平滑的页面过渡.

希望这能帮助遇到同样问题的人.

Vue.js相关问答推荐

如何在Vue 3中将动态特性从主零部件同步到子零部件

vue3 输入标签给出错误 Vue.use 不是函数

当用户在嵌套路由中时激活链接

在 Ajax 函数中访问 Vue.js 组件属性

在Vue cli项目中,Laravel api app放在哪里以及如何在vue js中调用api?

避免在运行时向 Vue 实例或其根 $data 添加响应式属性

Vue:如何将 store 与组件一起使用?

如何在 vue.js 中删除类

使用 Vue Router 设置页面标题

如何从我的 Vuetify 数据表中正确更新 Vuex 状态

NuxtServerInit 在 Vuex 模块模式下不起作用 - Nuxt.js

Vue更改宽度和内容

如何在 VUE 应用中使用 sass?

VS代码中的红点是什么意思

在手动安装的 vue 组件中使用 vuex

使用props向组件添加类名

重新加载页面正在 vue 组件中的插槽上闪烁内容

偶数行格式化

使用 vuex-persistedstate 仅使一个模块持久化

v-bind:class 的 Vue.js 计算(computed)属性