Vue.component('sidebar',
{template:'<p><router-link to="/">Go to Foo</router-link><router-link to="/bar">Go to Bar</router-link></p>'}
)
var sidebar = new Vue({el: '#sidebar'})
// 加载router
$('head').append('<script src="https://cdn.bootcss.com/vue-router/2.6.0/vue-router.js"></script>')
//之后再执行VueRouter
Although this is feasible, an error that the component is not registered will be reported before vue-router is executed. I would like to ask you if there is a better dynamic implementation method
As shown above, it is necessary to dynamically generate side routes, and then execute vue-router to parse, but an error will occur. I cannot understand why the error occurred
The reason is that vue-router will automatically parse router-link, even if there is no VueRouter instance
First of all, the second parameter of Vue.component is a configuration object. Your way of writing it does not even comply with JS syntax.
Secondly, the template configuration should be a string of HTML code, so change it to:
Update
(Reference: https://router.vuejs.org/en/e...)
In view of the situation where you mentioned the introduction, the code is modified as follows:
First introduce Vue and Vue-router in the following order
Then add the following JS
Then you can use the
Vue.component()
statement. At this time, because theVue-Router
component is registered,<router-link>
can be recognized.