【问题标题】:navbar not sending to the right place导航栏没有发送到正确的地方
【发布时间】:2021-05-05 19:48:20
【问题描述】:

所以, 我有一个附加了一些其他页面的登录页面,例如,landing/gothere 将我带到属于 /gothere 的登录部分,我有 /another 这是一个附加页面 问题是当我试图从 /another 到landing/gothere 时,因为我正在使用来自 api 的数据获取着陆信息,当我点击导航栏时,我去了正确的地方,但信息不存在然而,然后 api 加载页面,我在我想去的地方和它带我去的地方之间有这个差距。 在我将所有内容连接到 cms 之前,页面工作正常。 这是我在导航栏中使用的 nuxt 链接

<nuxt-link
              :to="{
                name: 'index',
                hash: '#expertises',
                params: { offset: 100 },
              }"
              class="nav-link"
              >Nos expertises
            </nuxt-link>

PD:我已经尝试过以这种方式使用 this.$route:

if (this.$route.params.hash) location.href = this.$route.params.hash;

将 nuxt 链接更改为:

<nuxt-link
              :to="{
                name: 'index',
                params: { hash: '#offres' },
              }"
              class="nav-link"
              >Nos offres
            </nuxt-link>

但是使用这种方法,导航栏只有在我在一个附加页面中时才有效,如果我在着陆页中,它就不起作用。 提前谢谢你

【问题讨论】:

    标签: javascript html vue.js nuxt.js


    【解决方案1】:

    在 Nuxt 中,您可以等待承诺得到解决,然后将服务器端呈现的应用程序返回给客户端。为了做到这一点,您可以尝试以下asyncData 钩子,就像mounted 钩子一样。

    重要提示:这可以仅在页面组件中使用 - 您的 pages/ 文件夹中的组件

    export default {
      props: ['someProp'],
      ...
      async asyncData() {
         await new Promise(resolve => {
           setTimeout(() => {
             resolve();
           }, 5000)
         });
      }
    }
    

    在这种情况下,我使用了一个简单的 Promise,它在 5 秒后得到解决。如果您现在进入使用它的页面,您将在浏览器选项卡上看到一个微调器加载 5 秒钟,直到解决 Promise。

    您也可以在获取时执行此操作,只需记住等待即可。

    强烈建议查看有关此的 Nuxt 文档 - Nuxt Data Fetching

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多