the story:

我在产品页面#/product/7上,在同一页面上,我还有4个与正在查看的产品相似的产品.所有这些产品都有指向其页面的链接:

router-link(:to="{ name: 'product', params: { id: product.id }}" v-text='product.title')

the problem:

当我点击任何产品链接时,url都会改变,但内容保持不变.所以,如果我在#/product/7上点击#/product/8,url只会改变.如果我从/product/:id页浏览并点击一个产品,它会把我带到正确的页面和正确的内容.

enter image description here

正如你在屏幕截图上看到的,当前产品id是15,但内容是id 7中的内容,正如我在购物车中的光滑丝绸衬衫产品上悬停时底部的url所示.

推荐答案

当您更改路径时,您必须更新products变量的数据,因为vue会优化页面重新加载,如果您在同一路径上,则不会重新加载.

您可以采用vue路由docs:

使用这种方法,我们在实际导航到新路由之前获取数据.我们可以在传入组件中的beforeRouteEnter guard中执行数据提取,并且只有在提取完成时才调用next:

export default {
  data () {
    return {
      product: {},
      error: null
    }
  },
  beforeRouteEnter (to, from, next) {
    getProduct(to.params.id, (err, product) => {
      if (err) {
        // display some global error message
        next(false)
      } else {
        next(vm => {
          vm.product = product
        })
      }
    })
  },
  // when route changes and this component is already rendered,
  // the logic will be slightly different.
  watch: {
    $route () {
      this.product = {}
      getProduct(this.$route.params.id, (err, product) => {
        if (err) {
          this.error = err.toString()
        } else {
          this.product = product
        }
      })
    }
  }
}

Vue.js相关问答推荐

将事件监听器绑定到动态元素

检测Vue3/Vue-test-utils中的Vuex还原/Mutations

如何在 Vuetify 2 中的轮播项目上放置 href 属性?

为什么 Pinia/Vuex 比使用服务类的classic 方法更受欢迎?

为什么普通对象在 Vue3 中自动变成响应式的?

Vue Datepicker 不会在渲染时显示默认日期

Vue:将点击事件绑定到动态插入的内容

在 Vue 文件中创建根 Vue 实例

VueJS:为什么在input事件处理程序中触发Input输入事件?

如何从特定索引呈现 v-for

Vuejs中的条件a href

Vuetify 在数据表中插入操作按钮并获取行数据

错误:您可能需要额外的加载器来处理这些加载器的结果.

等待来自 WDS 的更新信号

Vuex:无法读取未定义的属性'$store'

如何在 JEST 测试中模拟全局 Vue.js 变量

$set 不是函数

Vue.js react性如何在幕后工作?

如何访问通用 javascript 模块中的当前路由元字段

如何将props传递给组件的故事?