vue中等2秒reload()当前页面

41 阅读1分钟

你可以使用 setTimeout 函数来延时执行 window.location.reload() 方法,实现在2秒后刷新页面的效果。以下是代码示例:

methods: {
  judge() {
    if (this.input === '123' && this.input1 === '123') {
      this.$router.push('/');
    } else {
      this.$message.error('错了哦,这是一条错误消息');
      setTimeout(() => {
        window.location.reload();
      }, 2000);
    }
  }
}

在上述示例中,我们使用 setTimeout 函数来设置一个2秒的延时,然后在回调函数中调用 window.location.reload() 方法来刷新页面。这样,在判断条件不满足时,会显示错误消息,并在2秒后刷新页面。

请注意,这种方法是直接刷新整个页面,而不是仅刷新当前路由。如果你的需求是刷新整个页面,那么使用 window.location.reload() 是合适的。但如果你只想刷新当前路由,而不刷新整个页面,那么可以尝试通过其他方式实现,例如重新加载数据或重新渲染组件等。