路由

理解: 一個路由(route)就是一組映射關(guān)系(key - value),多個路由需要路由器(router)進行管理。

前端路由:key是路徑,value是組件。

1.基本使用

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

應(yīng)用插件:Vue.use(VueRouter)

編寫router配置項:

//引入VueRouter

importVueRouterfrom'vue-router'

//引入Luyou 組件

importAboutfrom'../components/About'

importHomefrom'../components/Home'

//創(chuàng)建router實例對象,去管理一組一組的路由規(guī)則

constrouter=newVueRouter({

routes:[

    {

        path:'/about',

        component:About

    },

    {

        path:'/home',

        component:Home

    }

]

})

//暴露router

exportdefaultrouter

實現(xiàn)切換(active-class可配置高亮樣式)

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

指定展示位置

<router-view></router-view>

2.幾個注意點

路由組件通常存放在pages文件夾,一般組件通常存放在components文件夾。

通過切換,“隱藏”了的路由組件,默認(rèn)是被銷毀掉的,需要的時候再去掛載。

每個組件都有自己的$route屬性,里面存儲著自己的路由信息。

整個應(yīng)用只有一個router,可以通過組件的$router屬性獲取到。

3.多級路由(多級路由)

配置路由規(guī)則,使用children配置項:

routes:[

{

    path:'/about',

    component:About,

},

{

    path:'/home',

    component:Home,

    children:[//通過children配置子級路由

        {

            path:'news',//此處一定不要寫:/news

            component:News

        },

        {

            path:'message',//此處一定不要寫:/message

            component:Message

        }

    ]

}

]

跳轉(zhuǎn)(要寫完整路徑):

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

4.路由的query參數(shù)

傳遞參數(shù)

<router-link:to="/home/message/detail?id=666&title=你好">跳轉(zhuǎn)</router-link>

<router-link

:to="{

    path:'/home/message/detail',

    query:{

    id:666,

title:'你好'

    }

}"

跳轉(zhuǎn)</router-link>

接收參數(shù):

$route.query.id

$route.query.title

5.命名路由

作用:可以簡化路由的跳轉(zhuǎn)。

如何使用

給路由命名:

{

path:'/demo',

component:Demo,

children:[

    {

        path:'test',

        component:Test,

        children:[

            {

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

                path:'welcome',

                component:Hello,

            }

        ]

    }

]

}

簡化跳轉(zhuǎn):

<router-linkto="/demo/test/welcome">跳轉(zhuǎn)</router-link>

<router-link:to="{name:'hello'}">跳轉(zhuǎn)</router-link>

<router-link

:to="{

    name:'hello',

    query:{

    id:666,

title:'你好'

    }

}"

跳轉(zhuǎn)</router-link>

6.路由的params參數(shù)

配置路由,聲明接收params參數(shù)

{

path:'/home',

component:Home,

children:[

    {

        path:'news',

        component:News

    },

    {

        component:Message,

        children:[

            {

                name:'xiangqing',

                path:'detail/:id/:title',//使用占位符聲明接收params參數(shù)

                component:Detail

            }

        ]

    }

]

}

傳遞參數(shù)

<router-link:to="/home/message/detail/666/你好">跳轉(zhuǎn)</router-link>

<router-link

:to="{

    name:'xiangqing',

    params:{

    id:666,

title:'你好'

    }

}"

跳轉(zhuǎn)</router-link>

特別注意:路由攜帶params參數(shù)時,若使用to的對象寫法,則不能使用path配置項,必須使用name配置!

接收參數(shù):

$route.params.id

$route.params.title

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、相關(guān)理解 1.路由就是一組key-value的對應(yīng)關(guān)系 多個路由,需要經(jīng)過路由器的管理 一個路...
    丁曉杰_2021強化班閱讀 391評論 0 0
  • Vue Router 是Vue.js[http://cn.vuejs.org/]官方的路由管理器。它和 Vue.j...
    SY閱讀 770評論 0 0
  • SPA單頁應(yīng)用 傳統(tǒng)的項目大多使用多頁面結(jié)構(gòu),需要切換內(nèi)容的時候我們往往會進行單個html文件的跳轉(zhuǎn),這個時候受網(wǎng)...
    視覺派Pie閱讀 12,186評論 1 55
  • 在傳統(tǒng)的 Web 開發(fā)過程中,當(dāng)你需要實現(xiàn)多個站內(nèi)頁面時,以前你需要寫很多個 html 頁面,然后通過 a 標(biāo)簽來...
    硅谷干貨閱讀 2,555評論 0 1
  • 一、什么是路由? 路由是通過互聯(lián)的網(wǎng)絡(luò)把信息從源地址傳輸?shù)侥康牡刂返幕顒?路由中有個非常重要的概念叫路由表,本質(zhì)上...
    接下來的冬天閱讀 501評論 0 0

友情鏈接更多精彩內(nèi)容