P粉7544734682023-08-29 12:38:14
addRoute
函数/
(没有它,你将会得到一个“非嵌套路由必须包含一个前导斜杠字符”的错误)$router.push
跳转到新的路由const Foo = { template: '<div>Foo</div>' } const Home = { template: '<div>Home</div>' } const router = new VueRouter({ mode: 'history', routes: [{ path: '/', component: Home }] }) const app = new Vue({ router, el: "#vue-app", methods: { viewComponent: function(path, method) { let tf = `/${path}/${method}`; let newRoute = { path: tf, name: `${path}_${method}`, component: Foo, } this.$router.addRoute(newRoute) this.$router.push({ name: newRoute.name }) }, } });
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script> <script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script> <div id="vue-app"> <a v-on:click="viewComponent('api/contact','get')">ddd</a> <router-view></router-view> </div>