简体   繁体   English

在应用程序运行时添加到 Vue.js 路由器的路由

[英]Add route to Vue.js router while app is running

I'm currently building an app and I would like to add extensions to this app.我目前正在构建一个应用程序,我想为这个应用程序添加扩展。 These extensions should have their own Vue components and and views (and thus also routes).这些扩展应该有自己的 Vue 组件和视图(因此也有路由)。 I don't want to rebuild the app but instead add the new views and routes dynamically.我不想重建应用程序,而是动态添加新视图和路由。 Is there a good way to do that with Vue 2? Vue 2 有什么好的方法吗?

In the following I added the files that I hope makes this question a bit more comprehensible.在下文中,我添加了我希望使这个问题更容易理解的文件。 The router/index.js contains the basic structure and is added to the main.js file in the regular fashion. router/index.js包含基本结构,并以常规方式添加到main.js文件中。 During the load of the app.vue the new routes should be loaded and appended to the already existing ones.app.vue的加载过程中,新的路由应该被加载并附加到已经存在的路由上。

router路由器

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

main.js main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

App.vue应用程序.vue

<template>
  <div id="app">
    <div id="nav">
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link> |
      <router-link to="/test">Test</router-link>
    </div>
    <router-view/>
  </div>
</template>

<script>
// @ is an alias to /src
import TestView from '@/views/Test.vue'

export default {
  name: 'Home',
    components: {},
    created() {

      <add route to router>({
          component: TestView,
          name: "Test",
          path: "/test"
      })
    }
}
</script>

I used the phrase <add route to router> to demonstrate the way I would like to add the route.我使用短语<add route to router>来演示我想要添加路由的方式。 After the route is added the user should be able to directly navigate to the new view using <router-link to="/test">Test</router-link> .添加路由后,用户应该能够使用<router-link to="/test">Test</router-link>直接导航到新视图。

Any help would be appreciated.任何帮助,将不胜感激。

Use addRoute to add routes at runtime.使用addRoute在运行时添加路由。 Here is the explanation for this method from the docs :以下是文档中对此方法的解释:

Add a new route to the router.向路由器添加新路由。 If the route has a name and there is already an existing one with the same one, it overwrites it.如果路由有名称并且已经存在具有相同名称的路由,则会覆盖它。

Import the router into App.vue to use it:router导入App.vue以使用它:

App.vue应用程序.vue

<script>
import router from './router/index.js';
import TestView from '@/views/Test.vue'

export default {
  created() {
    router.addRoute({
      component: TestView,
      name: "Test",
      path: "/test"
    })
  }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM