【问题标题】:How to change current route in vue-router如何在 vue-router 中更改当前路由
【发布时间】:2021-02-06 15:53:31
【问题描述】:

这是我的路线; http://localhost:3000/homes/California/buy/,我正在尝试使用 vue-router 将路由更改为http://localhost:3000/homes/California/rent/。我试过这样做;

<router-link
        :to="{ path: `homes/${this.$route.params.search}/rent/` }"
        tag="p"
      >
        Rent
</router-link>

但它会将路径附加到前一个路径。所以我得到http://localhost:3000/homes/California/buy/homes/California/rent/。

我什至认为它可能是this.$route.params.search,所以我替换了路径; homes/Chicago/buy/,还是不行。我该如何解决?

【问题讨论】:

    标签: vue.js url vuejs2 vue-component vue-router


    【解决方案1】:

    您提供给&lt;router-link&gt; 的路径是相对路径。您需要在路径前加上 / 前缀,以便它相对于网站的根目录,而不是当前页面:

    <router-link
            :to="{ path: `/homes/${this.$route.params.search}/rent/` }"
            tag="p"
          >
            Rent
    </router-link>
    

    【讨论】:

      【解决方案2】:

      您需要在homes 之前添加/ 以使其成为绝对路径。请尝试以下代码。

      <router-link
          :to="{ path: `/homes/${this.$route.params.search}/rent/` }"
          tag="p"
        >
          Rent
      </router-link>
      

      【讨论】:

        猜你喜欢
        • 2019-06-18
        • 1970-01-01
        • 2018-03-06
        • 1970-01-01
        • 2018-02-17
        • 2019-10-18
        • 1970-01-01
        • 2019-06-13
        • 2020-08-22
        相关资源
        最近更新 更多