推送新路由时,Nuxt 静态不加载获取的状态

问题描述

我正在使用此处描述的 nuxt 生成完整的静态网络应用https://nuxtjs.org/blog/going-full-static/#crazy-fast-static-applications

我也有一个小博客要加载为静态站点,所以我使用 fetch 钩子从 api 加载数据。

async fetch() {
  this.posts = await fetch(`${this.baseApi}/posts`).then(res => res.json())
},

当我生成(npm run generate)时,获取的状态是在dist/assets/static内部正确生成的,所以直接访问/blog时,状态正确加载,数据显示正确。 但是,当我在主页中并使用

访问博客时
this.$router.push

或一个

<nuxt-link to="/blog">Blog</nuxt-link>

获取的状态没有被加载,我必须再次调用 api,或者在 this.$fetch() 钩子中再调用一次 mounted()

我已经添加了

watch: {
  '$route.query': '$fetch'
}

进入首页

我需要在使用导航时正确加载获取的状态我还缺少什么?

澄清

我没有遇到 fetch 钩子本身的任何问题,而是导航没有检索目标路线的状态。 甚至 HTML 也在那里 我需要页面来获取目标路由的状态,当路由改变时,因为vue模板依赖它,所以如果没有加载,ui不会显示任何东西,我被迫调用fetch钩子手动

为了更清晰,这是我在直接访问 /blog 时的 devtools 的屏幕截图,注意 state.js 是如何正确检索的(它包含所有呈现的内容)

State correctly fetched when directly accessing

下面是我的 devtools 访问 / 时的截图,然后使用 nuxt-link 或 this.$router.push 访问博客(结果相同)

State not fetched after navigation

静态截图:

Static state.js of /blog

Blog.vue

<template>
  <b-container class="container blog">
    <b-row>
      <b-col lg="12" md="12" sm="12" cols="12" class="logo-col">
        <SbLogoSingle />
      </b-col>
    </b-row>
    <b-row v-if="$fetchState.pending" class="text-center">
      <b-spinner style="margin: auto"></b-spinner>
    </b-row>
    <b-row v-else>
      <b-col
        v-for="(post,idx) in posts.data"
        :key="idx"
        lg="4"
        md="4"
        sm="6"
        cols="12"
        class="blog-post-col"
      >
        <b-card
          v-if="post !== undefined"
          no-body
          class="shadow-lg blog-post-card"
          :img-src="post.media.url"
          img-top
        >
          <b-card-body class="text-left">
            <b-card-title>{{ replaceSlugByString(post.slug) }}</b-card-title>
            <b-card-text
              class="post-short-description"
              v-html="post.localizations[0].shortDescription"
            ></b-card-text>
          </b-card-body>
          <template #footer>
            <div class="text-left">
              <b-button class="apply-btn read-more-btn" @click="openBlogPost(idx)">Read more</b-button>
            </div>
          </template>
        </b-card>
      </b-col>
    </b-row>
  </b-container>
</template>

<script>
import { mapState } from 'vuex'

export default {
  data() {
    return {
      slug: 'test',posts: {},currentPage: 1,perPage: 12,pageIndex: 1,totalPages: 1,}
  },async fetch() {
    const response = await fetch(`${this.baseApi}/StaticPage`)
    const fetchedPosts = await response.json()

    this.posts = fetchedPosts
    // this.posts = await fetch(`${this.baseApi}/StaticPage`).then(res =>res.json())
  },computed: {
    ...mapState('modules/settings',['baseApi']),},beforeMount() {
    this.$fetch() // i want to remove this because the pages are statically generated correctly,I'm only adding it to refresh the state. which can be retrieved as a separate js file when accessing the route directly
  },methods: {
    openBlogPost(idx) {
      const pageObject = this.posts.data[idx]
      this.$router.push({
        name: `blog-slug`,params: {
          slug: pageObject.slug,page: pageObject,})
    },replaceSlugByString(slug) {
      return slug.replaceAll('-',' ')
    },}
</script>

这里是 slug.vue 的 pastebin

https://pastebin.com/DmJa9Mm1

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)