路由

理解: 一个路由(route)就是一组映射关系(key - value),多个路由需要路由器(router)进行管理。

前端路由:key是路径,value是组件。

1.基本使用

安装vue-router,命令:npm i vue-router

应用插件:Vue.use(VueRouter)

编写router配置项:

//引入VueRouter

importVueRouterfrom'vue-router'

//引入Luyou 组件

importAboutfrom'../components/About'

importHomefrom'../components/Home'

//创建router实例对象,去管理一组一组的路由规则

constrouter=newVueRouter({

routes:[

    {

        path:'/about',

        component:About

    },

    {

        path:'/home',

        component:Home

    }

]

})

//暴露router

exportdefaultrouter

实现切换(active-class可配置高亮样式)

<router-linkactive-class="active"to="/about">About</router-link>

指定展示位置

<router-view></router-view>

2.几个注意点

路由组件通常存放在pages文件夹,一般组件通常存放在components文件夹。

通过切换,“隐藏”了的路由组件,默认是被销毁掉的,需要的时候再去挂载。

每个组件都有自己的$route属性,里面存储着自己的路由信息。

整个应用只有一个router,可以通过组件的$router属性获取到。

3.多级路由(多级路由)

配置路由规则,使用children配置项:

routes:[

{

    path:'/about',

    component:About,

},

{

    path:'/home',

    component:Home,

    children:[//通过children配置子级路由

        {

            path:'news',//此处一定不要写:/news

            component:News

        },

        {

            path:'message',//此处一定不要写:/message

            component:Message

        }

    ]

}

]

跳转(要写完整路径):

<router-linkto="/home/news">News</router-link>

4.路由的query参数

传递参数

<router-link:to="/home/message/detail?id=666&title=你好">跳转</router-link>

<router-link

:to="{

    path:'/home/message/detail',

    query:{

    id:666,

title:'你好'

    }

}"

跳转</router-link>

接收参数:

$route.query.id

$route.query.title

5.命名路由

作用:可以简化路由的跳转。

如何使用

给路由命名:

{

path:'/demo',

component:Demo,

children:[

    {

        path:'test',

        component:Test,

        children:[

            {

name:'hello'//给路由命名

                path:'welcome',

                component:Hello,

            }

        ]

    }

]

}

简化跳转:

<router-linkto="/demo/test/welcome">跳转</router-link>

<router-link:to="{name:'hello'}">跳转</router-link>

<router-link

:to="{

    name:'hello',

    query:{

    id:666,

title:'你好'

    }

}"

跳转</router-link>

6.路由的params参数

配置路由,声明接收params参数

{

path:'/home',

component:Home,

children:[

    {

        path:'news',

        component:News

    },

    {

        component:Message,

        children:[

            {

                name:'xiangqing',

                path:'detail/:id/:title',//使用占位符声明接收params参数

                component:Detail

            }

        ]

    }

]

}

传递参数

<router-link:to="/home/message/detail/666/你好">跳转</router-link>

<router-link

:to="{

    name:'xiangqing',

    params:{

    id:666,

title:'你好'

    }

}"

跳转</router-link>

特别注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置!

接收参数:

$route.params.id

$route.params.title

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、相关理解 1.路由就是一组key-value的对应关系 多个路由,需要经过路由器的管理 一个路...
    丁晓杰_2021强化班阅读 2,817评论 0 0
  • Vue Router 是Vue.js[http://cn.vuejs.org/]官方的路由管理器。它和 Vue.j...
    SY阅读 3,994评论 0 0
  • SPA单页应用 传统的项目大多使用多页面结构,需要切换内容的时候我们往往会进行单个html文件的跳转,这个时候受网...
    视觉派Pie阅读 14,124评论 1 55
  • 在传统的 Web 开发过程中,当你需要实现多个站内页面时,以前你需要写很多个 html 页面,然后通过 a 标签来...
    硅谷干货阅读 7,124评论 0 1
  • 一、什么是路由? 路由是通过互联的网络把信息从源地址传输到目的地址的活动 路由中有个非常重要的概念叫路由表,本质上...
    接下来的冬天阅读 3,240评论 0 0

友情链接更多精彩内容