vue-router: 嵌套路由

模板抽离

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

    <!-- 模板抽离出来 -->
    <template id="home">
        <div>首页</div>
    </template>

    <template id="news">
        <div>新闻</div>
    </template>

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

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

路由嵌套

实际应用界面,通常由多层嵌套的组件组合而成。
比如,我们 “首页”组件中,还嵌套着 “登录”和 “注册”组件,那么URL对应就是/home/login/home/reg

    <template id="home">
        <!-- 注意:组件只能有一个根元素,所以我们包装到这个div中 -->
        <div>
            <h2>首页</h2>
             <router-link to="/home/login">登录</router-link>
            <router-link to="/home/reg">注册</router-link>
            <!-- 路由匹配到的组件将渲染在这里 -->
            <router-view></router-view>
        </div>
    </template>

这是访问/home后的模板,其中我们需要把/home/login/home/reg渲染进来。
完成上面代码后,HTML结构如下图:
这里写图片描述

  1. 登录和注册2个组件
    <template id="login">
        <div>登录界面</div>
    </template>
    <template id="reg">
        <div>注册界面</div>
    </template>
//定义路由组件
const Login = { template: '#login' };
        const Reg = { template: '#reg' };

3.定义路由

        // 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}
        ]

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

4.案例全部代码如下:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script src="http://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body> 
    <div id="box">
        <p>
            <router-link to="/home">home</router-link>
            <router-link to="/news">news</router-link>
        </p>
          <router-view></router-view>
    </div>

    <!-- 模板抽离出来 -->
    <template id="home">
        <!-- 注意:组件只能有一个根元素,所以我们包装到这个div中 -->
        <div>
            <h2>首页</h2>
             <router-link to="/home/login">登录</router-link>
            <router-link to="/home/reg">注册</router-link>
            <!-- 路由匹配到的组件将渲染在这里 -->
            <router-view></router-view>
        </div>
    </template>

    <template id="news">
        <div>新闻</div>
    </template>

    <template id="login">
        <div>登录界面</div>
    </template>
    <template id="reg">
        <div>注册界面</div>
    </template>

    <script type="text/javascript">
        // 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')

        // 现在,应用已经启动了!
    </script>
</body>
</html>

这里写图片描述

  • 19
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue-RouterVue.js官方提供的路由管理插件,可以实现单页面应用(SPA)中的路由切换。在Vue-Router中,我们可以通过嵌套路由来管理复杂的页面结构和页面间的跳转逻辑。 路由嵌套是指在一个路由组件中嵌套其他子路由组件,形成一个父子关系的层级结构。通过路由嵌套,我们可以更好地管理页面的细节和逻辑。 在Vue-Router中,我们首先需要定义路由的层级结构。我们可以在路由配置中使用`children`属性来定义嵌套路由。例如: ```js const router = new VueRouter({ routes: [ { path: '/home', component: Home, children: [ { path: 'about', component: About }, { path: 'contact', component: Contact } ] } ] }); ``` 在上面的代码中,我们定义了一个名为`home`的父路由,它有两个子路由`about`和`contact`。当用户访问`/home/about`时,会渲染`About`组件,而当用户访问`/home/contact`时,会渲染`Contact`组件。 在嵌套路由中,父路由的组件中需要使用`<router-view>`标签来显示子路由对应的组件。在上面的例子中,我们需要在`Home`组件中添加`<router-view>`标签。 ```html <template> <div> <h1>Home</h1> <router-view></router-view> </div> </template> ``` 当用户访问`/home`时,会渲染`Home`组件,并且`<router-view>`标签中会显示子路由对应的组件。 通过嵌套路由,我们可以更好地管理页面的结构和逻辑,将页面分成多个模块,并且可以在子路由中访问父路由的数据和方法。我们可以在路由跳转时传递参数,通过使用`$route.params`来获取参数。 总结来说,Vue-Router路由嵌套可以实现复杂的页面结构和页面间的跳转逻辑,通过定义父子关系的路由层级结构,使用`<router-view>`标签来显示子组件,并且可以通过路由传递参数来实现页面间的数据共享。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值