VUE路由(个人备忘)

一。路由基本使用

安装模块 npm install vue-router
引入

router = new VueRouter({ 
routes: [  //路由是一个对象数组  path 哈希  component对应组件
     {  //正常路由
       path: '/', 
       component: AppHome
       }, 
     {  //嵌套路由
                path: '/news',
                component: News,
                children: [
                    {
                        // 当匹配到 /news/sport时,
                        // 组件 Sport 会被渲染在 News 组件中的 <router-view> 组件中
                        path: '/news/sport',
                        component: Sport,
                        children: [
                            {
                                path: '/news/sport/detail/:id' , // :id 是路径变量,占位符
                                component: SportDetail
                            }
                        ]
                    },
                    {
                        // 简写方式 ,等价于 /news/tech 路径, 前面不要有 / , 有 / 就是根路径 啦
                        path: 'tech',
                        component: Tech,
                        children: [
                            {
                                path: '/news/tech/detail/:id',
                                component: TechDetail
                            }
                        ]
                    },
                    {
                        // 默认选择的路径 : /news 后面没有子路径时, redirect 就是重写向到指定路                径 下,即体育
                        path: '',
                        redirect: '/news/sport'
                    }
                ]
            },{path: '/about', component: About} 
    
 ] })

路由出口

```javascript
<router-view></router-view>

二.个人理解
1.组件在路由中配置后,不用在实例中加载(components)
2.配置好路由后,路由出口可放在任意组件中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值