【问题标题】:Gridsome mounted method is only running on page reloadGridsome挂载方法仅在页面重新加载时运行
【发布时间】:2022-01-09 21:53:07
【问题描述】:

我正在使用 vue 挂载的生命周期方法来获取数据。数据存储在 algolia 中。我使用搜索 api 连接并获取它。仅当我刷新站点时才加载数据。它不在页面导航上运行。

methods: {
    async fetchInventory(data = {}) {
        try {
            this.isLoading = true;
            const result = await index.search("", {hitsPerPage: 12});

            this.auctions = result.hits;

            this.totalItems = result.nbHits;
            this.totalPages = result.nbPages;
            this.isLoading = false;
        } catch (error) {
            this.isLoading = false;
            console.log(error);
        }
    },
},
mounted() {
    this.fetchInventory();
}

【问题讨论】:

    标签: vue.js algolia gridsome


    【解决方案1】:

    如果这是客户端渲染,您可能需要等到 nextTick 或使用早/晚挂钩:

    mounted() {
      this.$nextTick(function () {
        // Code that will run only after the
        // entire view has been rendered
      })
    }
    

    如果渲染服务器端,可能需要使用 beforeCreate 或 created 钩子。

    另外,页面导航是如何完成的?如果您使用的是导航 API 或可能对解决问题至关重要的库

    【讨论】:

    • 它仍然没有获取数据。当我刷新页面而不是页面导航时,它仅作为请求显示在 chrome 开发工具网络选项卡中。
    • 我只是渲染客户端并使用 Gridsome g-link 在页面之间导航。
    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 2016-08-18
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2021-11-28
    相关资源
    最近更新 更多