【问题标题】:view not updated after the url change网址更改后视图未更新
【发布时间】:2021-04-19 13:39:42
【问题描述】:

我正在尝试使用我的搜索输入访问不同用户的用户,我可以在其中键入用户的名称,然后单击任何结果并导航到他们的页面。显示第一个用户页面,但是当我搜索另一个用户并单击用户名时,地址栏中的路线会更新,但窗口中的视图仍显示较早的用户。我的代码做错了什么?

<template>
  <div>
    <custom-component :users="users" @input="onClick"></custom-component>
  </div>
</template>

<script>
export default {
 methods: {
    onClick(id) {
      this.$router.push(`/user/${id}`);
    },
 },
 watch: {
    $route(to, from) {
      if (to.params.id !== from.params.id) {
        this.$router.push(to.fullPath);
      }
    },
  },
};

【问题讨论】:

    标签: vue.js vuejs2 vue-router vuejs3


    【解决方案1】:

    您需要更改手表并再次获取用户列表。无需在手表中再次推送路由器。

    watch: {
      $route(to, from) {
        // react to route changes...
        let self = this;
        self.getUsers(); // For example
      }
    },
    

    【讨论】:

    • getUsers() 函数是什么?在我的代码中看不到这一点
    • 只是一个例子
    • 如何将属性用户传递给子组件。您需要在手表中重复此操作
    • 它仍然没有改变视图。
    • 创建的钩子被调用一次,所以路由改变你需要再次应用代码来改变属性用户
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    相关资源
    最近更新 更多