简体   繁体   English

导航栏没有发送到正确的地方

[英]navbar not sending to the right place

So, I have a landing page that has some other pages attached, for example landing/gothere takes me to the landing seccion that belongs to /gothere and I have /another which is an attached page The problem is when I'm trying to go from /another to landing/gothere because I'm fetching the landing info using data from an api and when the I click on the navbar I go to the right place but the info is not there yet, then the api loads the page and I have this gap between the place I want to be and the place it takes me.所以,我有一个附加了一些其他页面的登录页面,例如,landing/gothere 将我带到属于 /gothere 的登录部分,我有 /another 这是一个附加页面问题是当我尝试 go从/another到landing/gothere,因为我正在使用来自api的数据获取着陆信息,当我点击导航栏时,我将go放到正确的位置,但信息还没有加载,然后Z8A5DA52ED126447A8A83AZ70加载页面在我想去的地方和它带我去的地方之间有这个差距。 Before I connected everything to the cms the page was working correctly.在我将所有内容连接到 cms 之前,页面工作正常。 This is the nuxt link that I am using in the navbar这是我在导航栏中使用的 nuxt 链接

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

PD: I already tried to use this.$route this way: PD:我已经尝试过使用 this.$route 这样的方式:

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

changing the nuxt link to:将 nuxt 链接更改为:

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

But with this method the navbar only works if I am in one attached page, if I'm in the landing it does not work.但是使用这种方法,导航栏只有在我在一个附加页面中时才有效,如果我在登陆中,它就不起作用。 Thank you in advance先感谢您

In Nuxt you can wait for a promise to get resolved and after then, return the server-side rendered app to the client.在 Nuxt 中,您可以等待 promise 得到解决,然后将服务器端呈现的应用程序返回给客户端。 In order to do this you can try the following asyncData hook which is just like mounted hook.为了做到这一点,你可以尝试下面的asyncData钩子,就像mounted的钩子一样。

IMPORTANT : This can be used ONLY in page components - the ones in your pages/ folder重要提示:这只能在页面组件中使用 - 您的pages/文件夹中的组件

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

In this case I've used a simple Promise which gets resolved after 5 seconds.在这种情况下,我使用了一个简单的 Promise,它在 5 秒后得到解决。 If you now go on the page that uses this, you will see on the browser tab a spinner loading for 5 seconds until the Promise is resolved.如果您现在在使用它的页面上使用 go,您将在浏览器选项卡上看到一个微调器加载 5 秒,直到 Promise 得到解决。

You can do this when fetching as well, just remember to await it.您也可以在获取时执行此操作,只需记住等待它。

Highly suggest to check the Nuxt docs on this - Nuxt Data Fetching强烈建议在此查看 Nuxt 文档 - Nuxt Data Fetching

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

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