我需要更新url的散列,而不是触发vue路由进入该路由.比如:

router.replace('my/new/path', {silent:true})

这会将url更新为.../#/my/new/path,但路由本身不会路由到该路径.

有没有一种优雅的方式来实现这一点?

推荐答案

Vue路由处于打开或关闭状态,因此您无法"使其无法检测到某些URL".也就是说,如果Vue不需要销毁组件,它会重复使用组件,并允许您为URL添加别名.这允许您将iframe应用程序中的URL作为路由中的别名,并在这段时间内保持同一组件的活动状态,防止iframe重新加载.

// router.js
import Comp1 from "./components/Comp1";
import Comp2 from "./components/Comp2";

export default [
  {
    path: "/route1",
    component: Comp1,
    alias: ["/route2", "/route3", "/route4"]
  },
  {
    path: "/otherroute",
    component: Comp2
  }
];
// main.js
import Vue from "vue";
import App from "./App";
import VueRouter from "vue-router";
import routes from "./router";

Vue.config.productionTip = false;
Vue.use(VueRouter);

const router = new VueRouter({
  routes
});

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  components: { App },
  template: "<App/>"
});

在本例中,route1route2route3route4都将被视为必须加载route1,从而使组件Comp1保持活动状态.

Edit Vue Template

Vue.js相关问答推荐

如何配置nuxt 3,以便当我直接打开用nPM run generate生成的静态页面时,它可以正常工作?

如何根据数据库中的条件在vue中创建类

通过另一个文件调用模块化窗口组件

如何在 Nuxt3 中使用 nuxtjs/auth-next 模块?

Vuetify/Nuxt 生产环境组件在开发环境中无法正常工作

使用 cypress 进行 e2e 测试的 vuejs 期间出现 Webpack 错误

vue js导航到带有问号的url

Vue.js 和观察者模式

使用带有 v-date-picker 的 new Date() 不起作用

Vuetify 置顶工具栏

在 vue.js 中获取当前时间和日期

vuex 中 { dispatch, commit } 的类型是什么?

Vuetify 容器不会填充高度

Vuetify 单选按钮未显示为选中状态

错误:vue-loader 要求 @vue/compiler-sfc 存在于依赖树中

vuetify:以编程方式显示对话框

在手动安装的 vue 组件中使用 vuex

如何将项目推送到 Vuejs 中数据对象的数组中? Vue 似乎没有关注 .push() 方法

如何使用 Vuetify 验证复选框组

将 Vue props与默认值合并