我不想在更改nuxt上的动态参数时恢复页面

发布于 2025-01-22 11:23:59 字数 591 浏览 3 评论 0原文

当我在页面上时,我有一个带有两个动态参数的简单页面,更改URL参数fetch方法将再次运行。我不想要这种行为。

Fetch方法:

async fetch() {
    await this.getFlightResult();
}

GetResult方法:

async getResult() {

    this.$router.push({
        path: `/air/${originCity?.id}-${destinationCity?.id}/${originCity?.name}-${destinationCity?.name}`,
    });
    await this.getFlightResult();
}

GetResultMethod:

async getFlightResult(){
   const { data } = await this.$axios.get(`/api/v1/flights')
}

在更改URL参数运行Fetch方法和GetResult方法时,我有此代码,这会导致这种情况。

I have a simple page with two dynamic params when I'm on the page and changing the URL params fetch method will run again. I don't want this behavior.

fetch method:

async fetch() {
    await this.getFlightResult();
}

getResult Method:

async getResult() {

    this.$router.push({
        path: `/air/${originCity?.id}-${destinationCity?.id}/${originCity?.name}-${destinationCity?.name}`,
    });
    await this.getFlightResult();
}

getResultMethod:

async getFlightResult(){
   const { data } = await this.$axios.get(`/api/v1/flights')
}

I have this code when changing the URL params run fetch method and getResult method together and it causes this happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2025-01-29 11:23:59

您可以在不更改路径参数的情况下更改查询参数,因此,不重新呈现页面:

this.$router.push({query: { myFirstQuery: 'foo', mySecondQuery: 'bar'}})

然后,您可以使用以下方式观看查询:

watch: {
    '$route.query': {
      immediate: true,
      handler(newVal){
        // updating your values here
      }
    }
  }

You can change the query parameters without changing the path parameter and so, not re-rendering the page:

this.$router.push({query: { myFirstQuery: 'foo', mySecondQuery: 'bar'}})

You can then watch the queries with for example :

watch: {
    '$route.query': {
      immediate: true,
      handler(newVal){
        // updating your values here
      }
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文