路由嵌套

模板抽离

我们已经学习过了Vue模板的另外定义形式,使用。

首页
新闻


然后js里定义路由组件的时候:

// 1. 定义(路由)组件。constHome = { template:'#home'};constNews = { template:'#news'};

路由嵌套

实际应用界面,通常由多层嵌套的组件组合而成。

比如,我们 “首页”组件中,还嵌套着 “登录”和 “注册”组件,那么URL对应就是/home/login和/home/reg。

首页

登录注册

这是访问/home后的模板,其中我们需要把/home/login和/home/reg渲染进来。

完成上面代码后,HTML结构如下图:



登录和注册2个组件

登录界面
注册界面

//定义路由组件constLogin = { template:'#login'};constReg = { template:'#reg'};

3.定义路由

// 2. 定义路由constroutes = [            { path:'/', redirect:'/home'},            {                path:'/home',                component: Home,                children:[                    { path:'/home/login', component: Login},                    { path:'/home/reg', component: Reg}                ]            },            { path:'/news', component: News}        ]


注意我们在home路由配置了它的children。这就是嵌套路由。

4.案例全部代码如下:

homenews

首页

登录注册
新闻
登录界面
注册界面
// 1. 定义(路由)组件。constHome = { template:'#home'};constNews = { template:'#news'};constLogin = { template:'#login'};constReg = { template:'#reg'};// 2. 定义路由constroutes = [ { path:'/', redirect:'/home'}, { path:'/home', component: Home, children:[ { path:'/home/login', component: Login}, { path:'/home/reg', component: Reg} ] }, { path:'/news', component: News} ]// 3. 创建 router 实例,然后传 `routes` 配置constrouter =newVueRouter({ routes// (缩写)相当于 routes: routes})// 4. 创建和挂载根实例。// 记得要通过 router 配置参数注入路由,// 从而让整个应用都有路由功能constapp =newVue({ router }).$mount('#box')// 现在,应用已经启动了!

首页

登录注册
新闻
登录界面
注册界面
// 1. 定义(路由)组件。constHome = { template:'#home'};constNews = { template:'#news'};constLogin = { template:'#login'};constReg = { template:'#reg'};// 2. 定义路由constroutes = [ { path:'/', redirect:'/home'}, { path:'/home', component: Home, children:[ { path:'/home/login', component: Login}, { path:'/home/reg', component: Reg} ] }, { path:'/news', component: News} ]// 3. 创建 router 实例,然后传 `routes` 配置constrouter =newVueRouter({ routes// (缩写)相当于 routes: routes})// 4. 创建和挂载根实例。// 记得要通过 router 配置参数注入路由,// 从而让整个应用都有路由功能constapp =newVue({ router }).$mount('#box')// 现在,应用已经启动了!

首页

登录注册
新闻
登录界面
注册界面
// 1. 定义(路由)组件。constHome = { template:'#home'};constNews = { template:'#news'};constLogin = { template:'#login'};constReg = { template:'#reg'};// 2. 定义路由constroutes = [            { path:'/', redirect:'/home'},            {                path:'/home',                component: Home,                children:[                    { path:'/home/login', component: Login},                    { path:'/home/reg', component: Reg}                ]            },            { path:'/news', component: News}        ]// 3. 创建 router 实例,然后传 `routes` 配置constrouter =newVueRouter({            routes// (缩写)相当于 routes: routes})// 4. 创建和挂载根实例。// 记得要通过 router 配置参数注入路由,// 从而让整个应用都有路由功能constapp =newVue({          router        }).$mount('#box')// 现在,应用已经启动了!

// 1. 定义(路由)组件。

const Home = { template: '#home' };

const News = { template: '#news' };

const Login = { template: '#login' };

const Reg = { template: '#reg' };

// 2. 定义路由

const routes = [

{ path: '/', redirect: '/home' },

{

path: '/home',

component: Home,

children:[

{ path: '/home/login', component: Login},

{ path: '/home/reg', component: Reg}

]

},

{ path: '/news', component: News}

]

// 3. 创建 router 实例,然后传 `routes` 配置

const router = new VueRouter({

routes // (缩写)相当于 routes: routes

})

// 4. 创建和挂载根实例。

// 记得要通过 router 配置参数注入路由,

// 从而让整个应用都有路由功能

const app = new Vue({

router

}).$mount('#box')

// 现在,应用已经启动了!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 参考实例代码:http://jsfiddle.net/yyx990803/L7hscd8h/ http://js...
    树袋熊熊阅读 780评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,860评论 18 139
  • 子路由配置:在父级路由中添加children属性,值为子路由配置当想让某个子路由作为默认项时,path的值为空字符...
    饥人谷_米弥轮阅读 6,055评论 1 1
  • 333 到了2.0以后,有哪些变化? 1.在每个组件模板,不在支持片段代码组件中模板:之前:<template> ...
    magic_pill阅读 266评论 0 0
  • 我想不停的写,不停地写,知道有一天我有能力和心绪去将过往记下,以一种尊严的方式,而不是随地拾起,随意抛弃。在看完别...
    新人素阅读 173评论 0 0