成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Vue.js之路由系統(tǒng)

番茄西紅柿 / 1355人閱讀

摘要:生態(tài)之是什么是的路由系統(tǒng),定位資源的,我們可以不進(jìn)行整頁(yè)刷新去切換頁(yè)面內(nèi)容。

Vue.js生態(tài)之vue-router

vue-router是什么?

vue-router是Vue的路由系統(tǒng),定位資源的,我們可以不進(jìn)行整頁(yè)刷新去切換頁(yè)面內(nèi)容。

vue-router的安裝與基本配置

vue-router.js 可以下載 也可以用cdn,基本配置信息看如下代碼

// html 代碼
<div id="app">
    <div>
        <router-link to="/">首頁(yè)router-link>
        <router-link to="/about">關(guān)于我們router-link>
    div>
    <div>
        <router-view>router-view>
    div>

div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js">script>
<script src="../js/router_demo.js">script>
// js 代碼
var routes = [
    {
        path: "/",
        component: {
            template: `<div><h1>首頁(yè)h1>div>`
        }
    },
    {
        path: "/about",
        component: {
            template: `<div><h1>關(guān)于我們h1>div>`
        }
    }
]

var router = new VueRouter({
    routes: routes,
    // 路由去掉#
    // mode: history,
});

var app = new Vue({
    el: #app,
    router: router,
});

路由的一些方法

路由傳參以及獲取參數(shù)~~

// html 代碼
<div id="app">
    <div>
        <router-link to="/">首頁(yè)router-link>
        <router-link to="/about">關(guān)于我們router-link>
        <router-link to="/user/琴女?age=20">琴女router-link>
        <router-link to="/user/提莫">提莫router-link>
    div>
    <div>
        <router-view>router-view>
    div>
div>
// js 代碼
var routes = [
    {
        path: "/",
        component: {
            template: `<div><h1>首頁(yè)h1>div>`
        }
    },
    {
        path: "/about",
        component: {
            template: `<div><h1>關(guān)于我們h1>div>`
        }
    },
    {
        path: "/user/:name",
        component: {
            template: `<div>
                      <h1>我是:{{$route.params.name}}h1>
                      <h1>我年齡是:{{$route.query.age}}h1>
                    div>`,
        }
    }
]

var router = new VueRouter({
    routes: routes,
});

var app = new Vue({
    el: #app,
    router: router,
});

命名路由~  注意router-link里to一定要v-bind~~

<div id="app">
    <div>
        <router-link to="/">首頁(yè)router-link>
        <router-link :to="{name: about}">關(guān)于我們router-link>
        <router-link to="/user/gaoxin?age=19">gaoxinrouter-link>
    div>
    <div>
        <router-view>router-view>
    div>
div>
// js代碼
let routes = [
        {
            path: /,
            component: {
                template: `<h1>這是主頁(yè)h1>`
            }
        },
        {
            path: "/about",
            name: "about",
            component: {
                template: `<h1>關(guān)于我們h1>`
            }
        },
        {
            path: "/user/:name",
            component: {
                template: `<div>
                            <h1>我是{{$route.params.name}}h1>
                           <h2>我的年齡是{{$route.query.age}}h2>
                            div>
                          `
            }
        }
    ];

    let router = new VueRouter({
        routes: routes,
        mode: "history"
    });

    const app = new Vue({
        el: "#app",
        router: router,
        mounted(){
            console.log(this.$route)
            console.log(this.$router)
        }
    })

子路由

// 添加子路由變化的只有父級(jí)路由 
// 基于上面的例子增加
// js 代碼
{
        path: "/user/:name",
        component: {
            template: `<div>
                      <h1>我是:{{$route.params.name}}h1>
                      <h1>我年齡是:{{$route.query.age}}h1>
                      <router-link to="more" append>更多信息router-link>
                      <router-view>router-view>
                    div>`,
        },
        children: [
            {
            path: "more",
            component: {
                template: `<div>
                      {{$route.params.name}}的詳細(xì)信息
                div>`,
            }
        }
        ]

    },

 手動(dòng)訪問路由,以及傳參~~

// 基于上面例子追加
// html 代碼
<div id="app">
    <div>
        <router-link to="/">首頁(yè)router-link>
        <router-link to="/about">關(guān)于我們router-link>
        <router-link to="/user/琴女?age=20">琴女router-link>
        <router-link to="/user/提莫">提莫router-link>
        // 添加一個(gè)button按鈕
        <button @click="on_click">旅游button>
    div>
    <div>
        <router-view>router-view>
    div>
div>    
// js 代碼
// 注意路由name的使用 這是在原例子追加
var app = new Vue({
    el: #app,
    router: router,
    methods: {
        on_click: function () {
            setTimeout(function () {
                this.$router.push(/about)
                setTimeout(function () {
                    this.$router.push({name: "user", params:{name: "琴女"},query:{age: 20}})
                }, 2000)
            }, 2000)
        }
    }
});

命名路由視圖 router-view

當(dāng)我們只有一個(gè)的時(shí)候~所有內(nèi)容都展示在這一個(gè)面板里面~

如果是content 和 footer 就需要同時(shí)顯示并且不同區(qū)域~這就需要對(duì)視圖進(jìn)行命名~

命名視圖路由

// html 代碼
<div id="app">
    <div>
        <router-link to="/">首頁(yè)router-link>
        
    div>
    <div>
        <router-view name="content" class="content-view">router-view>
        <router-view name="footer" class="footer-view">router-view>
    div>

div>
// js 中的主要代碼
var routes = [
    {
        path: "/",
        components: {
            content: {
                template: `<div><h1>首頁(yè)h1>div>`,
            },
            footer: {
                template: `<div><h1>關(guān)于我們h1>div>`,
            }
        }
    },
]

錯(cuò)誤路由的重定向

let routes = [
     {
            path: "**",
            redirect: "/"
        }   
]

$route以及$router的區(qū)別~~

  -- $route為當(dāng)前router調(diào)轉(zhuǎn)對(duì)象,里面可以獲取name, path, query, params等~

  -- $router為VueRouter實(shí)例,有$router.push方法等~~

路由的鉤子

路由的生命周期就是從一個(gè)路由跳轉(zhuǎn)到另一路由整個(gè)過程,下面介紹兩個(gè)鉤子~

router.beforeEach()   router.afterEach()  詳情請(qǐng)看代碼

路由鉤子

// html 代碼
<div id="app">
    <router-link to="/">首頁(yè)router-link>
    <router-link to="/login">登錄router-link>
    <router-link to="/user">用戶管理router-link>
    <div>
        <router-view>router-view>
    div>
div>
//  js 代碼
var routes = [
    {
        path: "/",
        component: {
            template: "<h1>首頁(yè)h1>"
        }
    },
    {
        path: "/login",
        component: {
            template: "<h1>登錄h1>"
        }
    },
    {
        path: "/user",
        component: {
            template: "<h1>用戶管理h1>"
        }
    }
];
var router = new VueRouter({
    routes: routes
});

router.beforeEach(function (to,from,next) {
    // console.log(to)
    // console.log(from)
    // console.log(next)
    // next(false)
    if(to.path=="/user"){
        next("/login")
    }
    else {
        next();
    }
});
router.afterEach(function (to, from) {
    console.log(to)
    console.log(from)
});

var app = new Vue({
    el: #app,
    router: router
});

next參數(shù)配置

next:function  一定要調(diào)用這個(gè)方法來resolve這個(gè)鉤子函數(shù)。
        執(zhí)行效果依賴next方法的調(diào)用參數(shù)
        next() 什么都不做繼續(xù)執(zhí)行到調(diào)轉(zhuǎn)的路由
        next(false) 中斷當(dāng)前導(dǎo)航 沒有跳轉(zhuǎn) 也沒有反應(yīng)
        next("/")  參數(shù)是路徑 調(diào)轉(zhuǎn)到該路徑
        next(error)  如果next參數(shù)是一個(gè)Error實(shí)例 導(dǎo)航終止該錯(cuò)誤
                    會(huì)傳遞給router.onError()注冊(cè)過的回調(diào)中

如果/user下面還有子路由的情況下會(huì)怎么樣呢~????

// 匹配子路由 改一下匹配方法就可以~
// js 改動(dòng)代碼
router.beforeEach(function (to,from,next) {
    // console.log(to)
    // console.log(from)
    // console.log(next)
    // next(false)
    if(to.matched.some(function (item) {
            return item.path == "/post"
        })){
        next("/login")
    }
    else {
        next();
    }
});
// 元數(shù)據(jù)配置 改動(dòng)代碼
// html 部分
 {
        path: "/user",
        meta: {
            required_login: true,
        },
        component: {
            template: `
                <div>
                <h1>用戶管理h1>
                <router-link to="vip" append>viprouter-link>
                <router-view>router-view>
                div>
                `
        },
        children: [{
            path: "vip",
            meta: {
              required_login: true,
            },
            component: {
                template: <h1>VIPh1>
            }
        }]
    }
// js 部分
router.beforeEach(function (to,from,next) {
    // console.log(to)
    // console.log(from)
    // console.log(next)
    // next(false)
    if(to.meta.required_login){
        next("/login")
    }
    else {
        next();
    }
});

 

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/1530.html

相關(guān)文章

  • 利用K8S技術(shù)棧打造個(gè)人私有云(連載:私有云客戶端打造)

    摘要:前端技術(shù)棧還是非常龐大的,為了能夠借助已經(jīng)存在的輪子來造出一輛車,所以我選擇了進(jìn)行實(shí)踐。狀態(tài)的管理的狀態(tài)管理依靠完成,用其來管理的所有組件狀態(tài)。私有云客戶端打造主頁(yè)面首先是主頁(yè)面,可以打開任何一個(gè)云主機(jī)系統(tǒng)的頁(yè)面看,基本類似。 showImg(https://segmentfault.com/img/remote/1460000013930354); 【利用K8S技術(shù)棧打造個(gè)人私有...

    Jingbin_ 評(píng)論0 收藏0
  • 利用K8S技術(shù)棧打造個(gè)人私有云(連載:私有云客戶端打造)

    摘要:前端技術(shù)棧還是非常龐大的,為了能夠借助已經(jīng)存在的輪子來造出一輛車,所以我選擇了進(jìn)行實(shí)踐。狀態(tài)的管理的狀態(tài)管理依靠完成,用其來管理的所有組件狀態(tài)。私有云客戶端打造主頁(yè)面首先是主頁(yè)面,可以打開任何一個(gè)云主機(jī)系統(tǒng)的頁(yè)面看,基本類似。 showImg(https://segmentfault.com/img/remote/1460000013930354); 【利用K8S技術(shù)棧打造個(gè)人私有...

    shiina 評(píng)論0 收藏0
  • Vue 2.x 實(shí)戰(zhàn)后臺(tái)管理系統(tǒng)開發(fā)(二)

    摘要:導(dǎo)語(yǔ)承接上文實(shí)戰(zhàn)之后臺(tái)管理系統(tǒng)開發(fā)一在上一篇文章中,我詳細(xì)敘述了如何創(chuàng)建項(xiàng)目框架和引入各種后臺(tái)常用插件,做好這些準(zhǔn)備工作后,我們就可以著手進(jìn)行頁(yè)面的開發(fā)了。如果傳入的數(shù)據(jù)不符合規(guī)格,會(huì)發(fā)出警告。 1. 導(dǎo)語(yǔ) 承接上文:Vue 2.x 實(shí)戰(zhàn)之后臺(tái)管理系統(tǒng)開發(fā)(一) 在上一篇文章中,我詳細(xì)敘述了如何創(chuàng)建項(xiàng)目框架和引入各種后臺(tái)常用插件,做好這些準(zhǔn)備工作后,我們就可以著手進(jìn)行頁(yè)面的開發(fā)了。在開...

    Ilikewhite 評(píng)論0 收藏0
  • Vue學(xué)習(xí)筆記vue-cli項(xiàng)目搭建及解析

    摘要:樣式通過標(biāo)簽包裹,默認(rèn)是影響全局的,如需定義作用域只在該組件下起作用,需在標(biāo)簽上加,如要引入外部文件,首先需給項(xiàng)目安裝依賴包,打開,進(jìn)入項(xiàng)目目錄,輸入回車。 showImg(https://segmentfault.com/img/remote/1460000013235090); (一)安裝node.js 首先需要安裝node環(huán)境,可以直接到中文官網(wǎng)http://nodejs.cn/...

    Caizhenhao 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<