Vue-Vue-router

Vue-router

  • 通過控制哈希值控制頁面的切換

基本使用

  • 導(dǎo)入

    <script src="js/vue-router.js"></script>
    
  • Vue實例掛載模板的內(nèi)容

    <div id="app">
        <!--
      1.默認情況下router-link最終會被渲染a標簽
      2.可以通過tag屬性更改渲染之后的標簽
      3.通過to屬性奶卓,指定點擊之后跳轉(zhuǎn)的哈希值-->
        <router-link to="/one" tag="button">one</router-link>
        <router-link to="/two" tag="button">two</router-link>
    
        <!--
      1.組件會在router-view出口位置渲染顯示-->
        <router-view></router-view>
    </div>
    
  • 創(chuàng)建組件

    <template id="one">
        <div>
            <p>我是one</p>
        </div>
    </template>
    
    <template id="two">
        <div>
            <p>我是two</p>
        </div>
    </template>
    
  • 進行組件配置

    /*
    1.Vue實例和Vue-router實例的規(guī)則需要掛載相同組件配置,為了減少代碼的冗余度懈费,將組件的配置單獨拿出來 */
    const one = {
        template: "#one"
    }
    
    const two = {
        template: "#two"
    }
    
  • 定義路由規(guī)則

    const routes = [
        /*
        1.一個對象就是一條規(guī)則,在對象里面通過path屬性指定對應(yīng)的地址洋幻,
        2.在component屬性注冊組件 */
        { 
            path: '/one',
            // 綁定組件
            component: one
        },
        { 
            path: '/two', 
            // 綁定組件
            component: two
        },
    
        // 也通過redirect指定默認開始的哈希值
        { path: '/', redirect: '/one'},
    ]
    
  • 創(chuàng)建路由對象

    const router = new VueRouter({
        // 在路由對象上注冊規(guī)則
        routes: routes,
        // 通過linkActiveClass自定義激活狀態(tài)下的類名
        linkActiveClass: "nj-active"
    })
    
  • 在Vue實例上綁定vue-router的實例對象

    new Vue({
        el: '#app',
        // 綁定路由對象
        router: router,
        components:{
            // 綁定組件
            one: one,
            two: two
        },
    });
    

Vue-router數(shù)據(jù)傳遞

<template id="one">
    <div>
        <p>我是one</p>
    </div>
</template>

<template id="two">
    <div>
        <p>我是two</p>
    </div>
</template>
  • 通過url傳遞參數(shù)

    <div id="app">
        <router-link to="/one" tag="button">one</router-link>
        <!--可通過url后面以key=value的形式進行傳遞數(shù)據(jù)(get請求傳遞參數(shù)的方式)-->
        <router-link to="/two?name=lnj&age=33" tag="button">two</router-link>
    
        <router-view></router-view>
    </div>
    
    const one = {
        template: "#one",
    }
    const two = {
        template: "#two",
        // 通過生命周期方法created函數(shù)中拿到數(shù)據(jù)
        created: function () {
            // 如果通過url方式傳參,則通過路由的實例對象的query拿到數(shù)據(jù)
            console.log(this.$route.query.name);
            console.log(this.$route.query.age);
        }
    }
    
    const routes = [
        { path: '/', redirect: '/one'},
        { path: '/one, component: one },
        { path: '/two', component: two },
    ]
    
    const router = new VueRouter({
        routes: routes,
        linkActiveClass: "nj-active"
    })
    
    
    new Vue({
        el: '#app',
        router: router,
        components:{
            one: one,
            two: two
        },
    });
    
  • 通過在路由規(guī)則的path屬性上留坑傳遞參數(shù)

    <div id="app">
        <!--在頁面跳轉(zhuǎn)就可以傳遞key1和key2的value值-->
        <router-link to="/one/zs/66" tag="button">one</router-link>
        <router-link to="/two" tag="button">two</router-link>
    
        <router-view></router-view>
    </div>
    
    const one = {
        template: "#one",
        created: function () {
            // 如果通過路由規(guī)則上挖坑則需要使用params獲取數(shù)據(jù)
            console.log(this.$route.params.name);
            console.log(this.$route.params.age);
        }
    
    }
    const two = {
        template: "#two",
    }
    
    const routes = [
        { path: '/', redirect: '/one'},
        /*
        1.在路由規(guī)則的path屬性上留坑
        格式:
        path: '/哈希地址/:key1/:key2' */
        { path: '/one/:name/:age', component: one },
        { path: '/two', component: two },
    ]
    
    const router = new VueRouter({
        routes: routes,
        linkActiveClass: "nj-active"
    })
    
    
    new Vue({
        el: '#app',
        router: router,
        components:{
            one: one,
            two: two
        },
    });
    

嵌套路由

<template id="one">
    <div>
        <p>我是one</p>
        <!--路由的子界面切換需要在該父組件中router-link的to屬性队秩,繼續(xù)綁定延申的哈希地址-->
        <router-link to="/one/onesub1" tag="button">onesub1</router-link>
        <router-link to="/one/onesub2" tag="button">onesub2</router-link>
        <router-view></router-view>
    </div>
</template>

<template id="onesub1">
    <div>
        <p>我是one子界面1</p>
    </div>
</template>

<template id="onesub2">
    <div>
        <p>我是one子界面2</p>
    </div>
</template>

<template id="two">
    <div>
        <p>我是two</p>
    </div>
</template>
<div id="app">
    <router-link to="/one" tag="button">one</router-link>
    <router-link to="/two" tag="button">two</router-link>
    <router-view></router-view>
</div>
const onesub1 = {
    template: "#onesub1",
}
const onesub2 = {
    template: "#onesub2",
}
const one = {
    template: "#one",
    // 在父組件中繼續(xù)掛載子組件
    component: {
        onesub1: onesub1,
        onesub2: onesub2
    }
}

const two = {
    template: "#two",
}

const routes = [
    { path: '/', redirect: '/one'},
    {
        path: '/one',
        component: one,
        // 在父組件的路由規(guī)則中笑旺,通過children屬性繼續(xù)配置子組件切換的路由規(guī)則
        children: [
            // 在進行子組件的路由規(guī)則配置時,地址可不需要加上【/】
            { path: "onesub1", component: onesub1 },
            { path: "onesub2", component: onesub2 }

        ]
    },
    { path: '/two', component: two, },
]

const router = new VueRouter({
    routes: routes,
    linkActiveClass: "nj-active"
})


new Vue({
    el: '#app',
    router: router,
    components:{
        one: one,
        two: two
    },
});

路由出口的命名

  • 一個路由出口會在對應(yīng)的路由地址上顯示一個組件馍资,多個出口就會顯示多個組件

  • 可以指定出口名字和相應(yīng)的路由組件

    <div id="app">
        <!--通過name屬性給路由出口綁定名字-->
        <router-view name="name1"></router-view>
        <router-view name="name2"></router-view>
    </div>
    
    <template id="one">
        <div>
            <p>我是one</p>
        </div>
    </template>
    
    <template id="two">
        <div>
            <p>我是two</p>
        </div>
    </template>
    
    const one = {
        template: "#one"
    }
    
    const two = {
        template: "#two"
    }
    
    const routes = [
        { 
            path: '/',
            /* 
            1.在同一路由地址上筒主,對路由出口根據(jù)不同的名稱顯示不同的組件,需要將component屬性變成components
            2. components綁定一個對象鸟蟹,對象以keyvalue的形式指定在對應(yīng)key的對應(yīng)的組件*/
            components: {
                name1: one,
                name2: two
            }
        },
    ]
    
    const router = new VueRouter({
        routes: routes,
        linkActiveClass: "nj-active"
    })
    
    new Vue({
        el: '#app',
        router: router,
        components:{
            one: one,
            two: two
        },
    });
    
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末乌妙,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子戏锹,更是在濱河造成了極大的恐慌冠胯,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锦针,死亡現(xiàn)場離奇詭異荠察,居然都是意外死亡,警方通過查閱死者的電腦和手機奈搜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門悉盆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人馋吗,你說我怎么就攤上這事焕盟。” “怎么了宏粤?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵脚翘,是天一觀的道長。 經(jīng)常有香客問我绍哎,道長来农,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任崇堰,我火速辦了婚禮沃于,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘海诲。我一直安慰自己繁莹,他們只是感情好,可當我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布特幔。 她就那樣靜靜地躺著咨演,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蚯斯。 梳的紋絲不亂的頭發(fā)上雪标,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天零院,我揣著相機與錄音,去河邊找鬼村刨。 笑死,一個胖子當著我的面吹牛撰茎,可吹牛的內(nèi)容都是我干的嵌牺。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼龄糊,長吁一口氣:“原來是場噩夢啊……” “哼逆粹!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起炫惩,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤僻弹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后他嚷,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蹋绽,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年筋蓖,在試婚紗的時候發(fā)現(xiàn)自己被綠了卸耘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡粘咖,死狀恐怖蚣抗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情瓮下,我是刑警寧澤翰铡,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站讽坏,受9級特大地震影響锭魔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜震缭,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一赂毯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拣宰,春花似錦党涕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至晌该,卻和暖如春肥荔,著一層夾襖步出監(jiān)牢的瞬間绿渣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工燕耿, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留中符,地道東北人。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓誉帅,卻偏偏與公主長得像淀散,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蚜锨,可洞房花燭夜當晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Vue Router 是Vue.js[http://cn.vuejs.org/]官方的路由管理器档插。它和 Vue.j...
    SY閱讀 584評論 0 0
  • 一、概念介紹 Vue.js和React.js分別是目前國內(nèi)和國外最火的前端框架亚再,框架跟類庫/插件不同郭膛,框架是一套完...
    劉遠舟閱讀 1,061評論 0 0
  • 1.插槽 1.1匿名插槽 <!DOCTYPE html> 52-Vue組件-匿名插槽 ...
    煤球快到碗里來閱讀 608評論 0 0
  • vue筆記 一.vue實例 vue的生命周期 beforeCreate(創(chuàng)建前), created(創(chuàng)建后), b...
    秋殤1002閱讀 1,055評論 0 1
  • Vue Vue是一個前端js框架,由尤雨溪開發(fā)氛悬,是個人項目 Vue近幾年來特別的受關(guān)注则剃,三年前的時候angular...
    hcySam閱讀 282評論 0 0