【问题标题】:Nuxt JS Apollo data only available after page refreshNuxt JS Apollo 数据仅在页面刷新后可用
【发布时间】:2019-08-08 08:16:27
【问题描述】:

我正在 Nuxt 中使用 Apollo 获取一些数据。不知何故,当导航到该页面时,我收到一个错误

Cannot read property 'image' of undefined

当我刷新页面时,一切正常。

我发现一些人有类似问题,但似乎没有解决方案适合我:/

这是我现在的模板文件:

/products/_slug.vue

<template>
  <section class="container">
    <div class="top">
      <img :src="product.image.url"/>
      <h1>{{ product.name }}</h1>
    </div>
  </section>
</template>

<script>
import gql from 'graphql-tag'

export default {
  apollo: {
    product: {
      query: gql`
        query Product($slug: String!) {
          product(filter: { slug: { eq: $slug } }) {
            slug
            name
            image {
              url
            }
          }
        }
      `,
      prefetch({ route }) {
        return {
          slug: route.params.slug
        }
      },
      variables() {
        return {
          slug: this.$route.params.slug
        }
      }
    }
  }
}
</script>

除非我刷新页面,否则 $apolloData 基本上是空的。任何想法将不胜感激

编辑 又近了一步(我认为)。以前,第一次导航到页面时,所有内容(image.url 和名称)都将是未定义的。

我补充说:

data() {
    return {
      product: []
    };
  }

在我的导出顶部,现在至少始终定义名称,所以如果我删除图像,一切都会按预期工作。只是 image.url 一直未定义。

我注意到的一件事(不确定有多相关)是这个问题只发生在使用 , 如果我使用普通的 a 标签它可以工作,但当然会带走 vue 的魔力。

EDIT-2 因此,如果我将 Nuxt 降级到 1.0.0 版本,一切都会正常

【问题讨论】:

    标签: apollo nuxt.js vue-apollo


    【解决方案1】:

    我也偶然发现了这个问题,发现它隐藏在 Vue Apollo 文档中。

    虽然与OP的回复非常相似,但似乎官方的方式是使用“$loadingKey”属性。

    文档中的内容非常混乱,因为发生了很多事情。 https://vue-apollo.netlify.com/guide/apollo/queries.html#loading-state

    <template>
      <main
        v-if="!loading"
        class="my-8 mb-4"
      >
        <div class="w-3/4 mx-auto mb-16">
          <h2 class="mx-auto text-4xl text-center heading-underline">
            {{ page.title }}
          </h2>
          <div
            class="content"
            v-html="page.content.html"
          ></div>
        </div>
      </main>
    </template>
        
    <script>
    import { page } from "~/graphql/page";
        
    export default {
      name: 'AboutPage',
    
      data: () => ({
        loading: 0
      }),
        
      apollo: {
        $loadingKey: 'loading',
        page: {
          query: page,
          variables: {
            slug: "about"
          }
        },
      }
    }
    </script>
    

    如果您需要在 vue 中使用响应式属性,例如 slug,您可以使用以下方法。

    <template>
      <main
        v-if="!loading"
        class="my-8 mb-4"
      >
        <div class="w-3/4 mx-auto mb-16">
          <h2 class="mx-auto text-4xl text-center heading-underline">
            {{ page.title }}
          </h2>
          <div
            class="content"
            v-html="page.content.html"
          ></div>
        </div>
      </main>
    </template>
        
    <script>
    import { page } from "~/graphql/page";
        
    export default {
      name: 'AboutPage',
        
      data: () => ({
        loading: 0
      }),
        
      apollo: {
        $loadingKey: 'loading',
        page: {
          query: page,
          variables() {
            return {
              slug: this.$route.params.slug
            }
          }
        },
      }
    }
    </script>
    

    【讨论】:

    • 这对我有用。首先我尝试设置$loadingKey: 'loading',但没有成功。但是将loading: 0 添加到数据中修复了它。我正在使用 nuxt apollo。
    • 对此非常感谢 - 我的查询有问题,它返回一个值数组,每当我刷新页面时它都会返回 undefined。 $loadingKeyv-if 声明就像一个魅力
    • 非常感谢您的回答!它为我节省了数小时或研究以及很多挫败感!
    【解决方案2】:

    要解决此问题,请将 v-if="!$apollo.loading" 添加到您打算在其中使用响应式道具的 HTML 容器中。

    【讨论】:

      【解决方案3】:

      我认为这只是页面加载时间的问题。

      如果您有多个产品,您应该迭代产品,或者在产品容器上使用 v-if="product != null",这将仅在从 GraphQL 获取数据后呈现。

      这样,您将仅在真正获取 HTML 时在 HTML 中使用该对象,并避免读取未定义的属性。

      【讨论】:

      • 我已经尝试过你的想法,但不知何故它就像它不等待数据(无论如何应该是 ssr'd 和预取)我想这只是增加了另一个问题;)虽然取得了一点进展如果您在主要问题中查看我的编辑评论
      • 您对 SSR 的看法是正确的。但是,你使用的是这样的东西:github.com/nuxt-community/apollo-module 吗? Nuxt 方面还是全部在 Vue 中?
      • 是的,我正在使用@nuxt/apollo 模块。我基本上遵循了这个例子:github.com/ErikCH/HeadlessCMSDatoCMS,即使我只是克隆它并更改凭据和查询等,它对我不起作用,所以我不知道我做错了什么。
      • 它可能无法正常工作,因为例如您有不同的数据架构。您能详细说明实际错误吗?
      • 当我的查询中未设置为可空值的任何元素未设置或返回为空时,我遇到了同样的问题。但我注意到这在开发中并不是生产中的问题。我还想声明,我正在使用“jw-vue-pagination”,因此我面临着同样的问题,如果上述适用与否,我将无法承受,但仍然只在生产中。
      猜你喜欢
      • 2019-11-27
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多