vue 2.0路由之路由嵌套示例详解
前言
vue一个重要的方面就是路由,下面是自己写的一个路由的例子分享给大家供大家参考学习,下面来看看详细的介绍。
方法如下:
1、引入依赖库就不必再说
2、创建组件
两种写法
第一种:间接
Home
{{msg}}
varAbout=Vue.extend({
template:'#about'
});
第二种:直接
varOut=Vue.extend({
template:'Out
Thisisthetutorialoutvue-router.
'
});
3、创建router实例,传'routes'路由映射配置
varrouter=newVueRouter({
routes:[
{path:'/路径',component:组件名},
{path:'/',component:组件名},//设置默认路径
{path:"*",component:Home}//路径不存在
]
});
4、创建和挂载根实例。记得要通过router配置参数注入路由,从而让整个应用都有路由功能
varvm=newVue({
router:router
}).$mount('#app');
整体的demo
helloworld GotoHome GotoAbout GotoOut
Home
{{msg}}
about
Thisisthetutorialaboutvue-router.
Out
Thisisthetutorialoutvue-router.