【问题标题】:when I use \nuxt\strapi\ nuxt/apollo,it always told Cannot read property 'content' of undefined当我使用 \nuxt\strapi\ nuxt/apollo 时,它总是告诉Cannot read property 'content' of undefined
【发布时间】:2020-04-11 19:42:26
【问题描述】:

当我使用\nuxt\strapi\nuxt/apollo 时,它总是告诉Cannot read property 'content' of undefined。 如果我刷新页面就可以了!

为什么?

我该怎么办?

这是我的代码

<template>
  <div>
    <div class="about-content">
      <div id="zwqkl" v-html="$md.render(aboutuses[0].content)"></div>
    </div>
  </div>
</template>
<script>
import aboutusQuery from '~/apollo/queries/aboutus/aboutus'

export default {
  name: 'about-us',
  data() {
    return {
      aboutuses: []
    }
  },
  apollo: {
    aboutuses: {
      query: aboutusQuery
    }
  }
}
</script>

【问题讨论】:

    标签: nuxt.js


    【解决方案1】:

    这回答了你的问题:Nuxt JS Apollo data only available after page refresh

    关键是使用“$loadingKey”属性。我相应地更新了您的代码。

    <template>
      <div>
        <div v-if="!loading" class="about-content">
          <div id="zwqkl" v-html="$md.render(aboutuses[0].content)"></div>
        </div>
      </div>
    </template>
    
    <script>
    import aboutusQuery from '~/apollo/queries/aboutus/aboutus'
    
    export default {
      name: 'about-us',
      data() {
        return {
          loading: 0,
          aboutuses: []
        }
      },
      apollo: {
        $loadingKey: 'loading',
        aboutuses: {
          query: aboutusQuery
        }
      }
    }
    </script>
    

    【讨论】:

      【解决方案2】:

      你可以尝试预取你的结果吗?

      apollo: {
        prefetch: true,
        aboutuses: {
          query: aboutusQuery
        }
      }
      

      【讨论】:

      • 我试过了,还是不行。我想这应该是NUXT渲染的生命周期问题。你有其他解决方案吗?
      猜你喜欢
      • 1970-01-01
      • 2019-12-05
      • 2022-11-20
      • 2021-09-25
      • 2021-06-25
      • 2020-01-21
      • 2023-02-22
      • 2017-04-24
      • 2023-03-15
      相关资源
      最近更新 更多