路由配置错误,需要指定路径
P粉113938880
2023-08-28 18:32:26
<p>我想要添加动态路由并在所有动态路由中使用相同的组件。我尝试了以下代码来渲染组件,但是我得到了一个错误,错误信息如下:</p>
<blockquote>
<p>[vue-router] “path”在路由配置中是必需的。</p>
</blockquote>
<p>添加动态路由并显示相同组件的正确方法是什么?</p>
<p>
<pre class="brush:js;toolbar:false;">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) {
debugger;
let tf = `${path}/${method}`;
let newRoute = {
path: tf,
name: `${path}_${method}`,
components: {
Foo
},
}
this.$router.addRoute([newRoute])
},
}
});</pre>
<pre class="brush:html;toolbar:false;"><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></pre>
</p>
addRoute
函数/
(没有它,你将会得到一个“非嵌套路由必须包含一个前导斜杠字符”的错误)$router.push
跳转到新的路由