admin管理员组文章数量:1289868
I have created a function "addroute()" to add routes dynamically,but it didn't work(the router does not changed). Is this a right way to use addRoutes method? I have learned this from tutorial. If not then give me a correct example,Thanks!
...
const Bar={
template:'#panels',
data(){
return {title:'badss'}
}
};
const Foo={
template:"#panels",
data(){
return {title:'hell'}
}
};
const router = new VueRouter({
routes:[
{path:'/foo',ponent:Foo},
{path:'/bar',ponent:Bar}
]
});
new Vue({
el:'#root',
router:router,
data:{
stats:stats
},
methods: {
//
},
});
function addroute(){//watch here
router.addRoutes([{path:'/ioio',ponent:{template:'#panels'}}])
}
setInterval("addroute()", 2000)//watch here
...
I have created a function "addroute()" to add routes dynamically,but it didn't work(the router does not changed). Is this a right way to use addRoutes method? I have learned this from tutorial. If not then give me a correct example,Thanks!
...
const Bar={
template:'#panels',
data(){
return {title:'badss'}
}
};
const Foo={
template:"#panels",
data(){
return {title:'hell'}
}
};
const router = new VueRouter({
routes:[
{path:'/foo',ponent:Foo},
{path:'/bar',ponent:Bar}
]
});
new Vue({
el:'#root',
router:router,
data:{
stats:stats
},
methods: {
//
},
});
function addroute(){//watch here
router.addRoutes([{path:'/ioio',ponent:{template:'#panels'}}])
}
setInterval("addroute()", 2000)//watch here
...
Share
Improve this question
edited Sep 21, 2017 at 12:13
Jaimesh
8514 gold badges25 silver badges41 bronze badges
asked Sep 21, 2017 at 10:02
Lauda WangLauda Wang
8873 gold badges11 silver badges19 bronze badges
1
- Are you looking for setTimeout()? – Deepak Commented Sep 21, 2017 at 11:02
1 Answer
Reset to default 7The router instance that you are trying to change is already added to the Vue instance that you have. Changing that router does not update the Vue instance. What you want to do is call the router instance that you have added to the Vue instance. A plete working example can be found at: Add routes in vue-router
So you access the router inside a method in the Vue instance using this.$router
. You then add the desired route that you want to add using the .addRoutes
functionality.
In the fiddle, you can see that I have added the ioio link in the HTML already, but it does not work yet (clicking it will result in the <span>Default</span>
being added to the <router-view></router-view>
). When clicking on the button, you are adding a new route. I have added the this.$router.push('/ioio');
to force an update on the <router-view></router-view>
after adding the route. If you remove this line of the code, the <router-view></router-view>
will display the last shown element (which is desirable in most cases), and clicking on the ioio-button again will show the newly created route.
Hope this helps!
本文标签: javascriptHow to use addroutes method in VuerouterStack Overflow
版权声明:本文标题:javascript - How to use addroutes method in Vue-router? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741488331a2381506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论