简体   繁体   English

(Nuxt) Vue 组件直到页面刷新才显示

[英](Nuxt) Vue component doesn't show up until page refresh

I'm storing nav items in my Vuex store and iterating over them for conditional output, in the form of a Vue/Bulma component, as follows:我将导航项存储在我的 Vuex 商店中,并以 Vue/Bulma 组件的形式对它们进行迭代以获取条件 output,如下所示:

<b-navbar-item
  v-for='(obj, token) in $store.state.nav'
  v-if='privatePage'
  class=nav-link
  tag=NuxtLink
  :to=token
  :key=token
>
  {{obj.text}}
</b-navbar-item>

As shown, it should be output only if the component's privatePage data item resolves to true, which it does:如图所示,只有当组件的privatePage数据项解析为 true 时,它才应该是 output,它确实如此:

export default {
  data: ctx => ({
    privatePage: ctx.$store.state.privateRoutes.includes(ctx.$route.name)
  })
}

The problem I have is when I run the dev server (with ssr: false ) the component doesn't show up initially when I navigate to the page via a NuxtLink tag.我遇到的问题是,当我运行开发服务器(使用ssr: false )时,当我通过NuxtLink标记导航到页面时,组件最初不会显示。 If I navigate to the page manually, or refresh it, the component shows.如果我手动导航到页面或刷新它,组件就会显示。

I've seen this before in Nuxt and am not sure what causes it.我以前在 Nuxt 看到过这种情况,但不确定是什么原因造成的。 Does anyone know?有人知道吗?

recommendation:推荐:

  1. use mapState and other vuex mapping helper to have more readable code:).使用mapState和其他 vuex 映射助手来获得更具可读性的代码:)。

  2. dont use v-for and v-if at the same element不要在同一个元素上使用 v-for 和 v-if

  3. use "nuxt-link" for your tag为您的tag使用"nuxt-link"

  4. use / for to (if your addresses dont have trailing slash)使用/ for to (如果您的地址没有斜杠)

<template v-if='privatePage'>
  <b-navbar-item
    v-for='(obj, token) in nav'
    class=nav-link
    tag="nuxt-link"
    :to="token"  Or "`/${token}`"
    :key="token"
  >
    {{obj.text}}
  </b-navbar-item>
</template>

and in your script:在你的脚本中:

<script>
import {mapState} from 'vuex'

export default{
   data(){
    return {
      privatePage: false
    }
   },
   computed:{
     ...mapState(['privateRoutes','nav'])
   },
   mounted(){
       // it's better to use name as a query or params to the $route
     this.privatePage = this.privateRoutes.includes(this.$route.name)
   }
}

</script>

and finally if it couldn't have help you, I suggest to inspect your page via dev tools and see what is the rendered component in html.最后,如果它不能帮助你,我建议通过dev tools检查你的页面,看看 html 中呈现的组件是什么。 it should be an <a> tag with href property.它应该是带有href属性的<a>标记。 In addition, I think you can add the link address (that work with refresh and not by nuxt link) to your question, because maybe the created href is not true in navbar-item.此外,我认为您可以将链接地址(适用于刷新而不是通过 nuxt 链接)添加到您的问题中,因为在 navbar-item 中创建的 href 可能不正确。

NOTE: token is index of nav array.注意: token是导航数组的索引。 so your url with be for example yourSite.com/1 .so it's what you want?所以你的 url 例如yourSite.com/1 。所以这就是你想要的?

This question has been answered here: https://stackoverflow.com/a/72500720/12747502这个问题已经在这里得到了回答: https://stackoverflow.com/a/72500720/12747502

In addition, the solution to my problem was a commented part of my HTML that was outside the wrapper div.此外,我的问题的解决方案是我的 HTML 的注释部分,它位于包装器 div 之外。

Example:例子:

<template>
  <!-- <div>THIS CREATES THE PROBLEM</div> -->
  <div id='wrapper'> main content here </div>
</template>

Correct way:正确方法:

<template>
  <div id='wrapper'>
    <!-- <div>THIS CREATES THE PROBLEM</div> -->
    main content here
  </div>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM