中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

VueRouter代碼筆記

發布時間:2020-06-19 18:55:29 來源:網絡 閱讀:212 作者:windcharger 欄目:web開發

VueRouter

VueRouter基本使用

<!-- 引入對應的模塊 -->
<script src="./static/vue.min.js"></script>
<script src="./static/vue-router.js"></script>

<div id="app">
    <div>
        <!-- 第三步:結合router-link和router-view調用 -->
        <router-link to="/">首頁</router-link>
        <router-link to="/login">登錄</router-link>
        <router-link to="/register">注冊</router-link>
        <router-view></router-view>
    </div>
</div>

<script>
    // Vue.use(VueRouter);   // 這個主要是在vue-cli會用到,這里可以不用填

        let Register = { template: '<div><h2>這是注冊頁面</h2></div>'
        let Login = { template: '<div><h2>這是登錄頁面</h2></div>'
        let Home = { template: '<div><h2>這是主頁面</h2></div>'

    // 第一步:實例化router對象路徑和頁面進行綁定對應關系
    let router = new VueRouter({
      routes: [
        {  path: '/',  component: Home,  },
        { path: '/login',  component: Login, },
        { path: '/register', component: Register },
      ]
    })
    new Vue({
      el: "#app",
    // 第二步:vue根實例中注冊router對象
      router: router,
    })
  </script>

命名路由(name)

<div id="app">
        <div>
            <router-link :to="{ name: 'home'}">主頁</router-link>
            <router-link :to="{ name: 'login'}">登錄</router-link>
            <router-link :to="{ name: 'register'}">注冊</router-link>
            <router-view></router-view>
        </div>
</div>
<script>
        let Register = { template: '<div><h2>這是注冊頁面</h2></div>'
        let Login = { template: '<div><h2>這是登錄頁面</h2></div>'
        let Home = { template: '<div><h2>這是主頁面</h2></div>'

    let router = new VueRouter({
      routes: [
        {  name: 'home', path: '/',  component: Home,  },
        { name: 'login',  path: '/login',  component: Login, },
        { name: 'register', path: '/register', component: Register },
      ]
    })
    new Vue({
      el: "#app",
      router: router,
    })
  </script>

路由參數

<div id="app">
    <div>
        <router-link :to="{ name: 'home' }">主頁面</router-link>
        <router-link :to="{ name: 'userparams', params: { userId: 1} }">用戶參數params</router-link>
        <router-link :to="{ name: 'userquery', query: { userId: 2} }">用戶參數 query</router-link>
        <router-view></router-view>
    </div>
</div>
<script>
    Vue.use(VueRouter);

    let Home = { template:'<div>這是主頁面</div>' }
    let UserParams = { template:'<div>這是用戶params</div>' }
    let UserQuery = { template:'<div>這是用戶query</div>' }

    let router = new VueRouter({
        routes: [
            {
                name: 'home',
                path: '/',
                component: Home,
            },
            {
                // 路由的參數: /user/1 /user/2
                name: "userparams",
                path: '/user/:userId',
                component: UserParams
            },
            {
                // 路由參數: /user?wenqipeng=111
                name: 'userquery',
                path: '/user',
                component: UserQuery
            },
        ]
    })

    new Vue({
        el: "#app",
        router: router
    })
</script>

子路由

<div id="app">
    <div>
        <router-link to="index">首頁</router-link>
        <router-link to="courses">課程頁面</router-link>
        <router-view></router-view>
    </div>
</div>
<script>
    let Index = { template: '<div><h2>這是首頁</h2></div>' }
    let Lightcourses = { template: '<div><h2>這是Lightcourses</h2></div>' }
    let Degreecourses = { template: '<div><h2>這是Degreecourses</h2></div>' }
    let Courses = { 
        template: `
            <div>
                <h2>這是課程頁</h2>
                <router-link to="lightcourses">輕客</router-link>
                <router-link to="degreecourses">學位課</router-link>
                <router-view></router-view>
            </div>'
        `
    }

    let router = new VueRouter({
        routes: [
            {
                name: 'index',
                path: '/index',
                component: Index,
            },
            {
                name: 'courses',
                path: '/courses',
                component: Courses,
                children: [   <!-- 路由 -->
                    {
                        name: 'lightcourses',
                        path: 'lightcourses',
                        component: Lightcourses
                    },
                    {
                        name: 'degreecourses',
                        path: 'degreecourses',
                        component: Degreecourses
                    },
                ]
            },
        ]
    })
    new Vue({
        el: "#app",
        router: router
    })
</script>

路由重定向 (redirect)

<div id="app">
    <div>
        <router-link to="/">首頁</router-link>
        <router-link to="/login">登錄</router-link>
        <router-link to="/pay">支付</router-link>

        <router-view></router-view>
    </div>
</div>

<script>

    let Index = { template: '<div><h2>這是首頁</h2></div>'}
    let Login = { template: '<div><h2>這是登錄頁面</h2></div>'}
    let Pay = { template: '<div><h2>這是支付頁面</h2></div>'}

    let router = new VueRouter({
        routes: [
            {
                path: '/',
                component: Index
            },
            {
                path: '/login',
                component: Login
            },
            {
                path: '/pay',
                // component: Pay,
                redirect: '/login'    <!-- 路由重定向 -->
            },
        ]
    })
    new Vue({
        el: "#app",
        router: router
    })

</script>

路由之鉤子函數 (router.beforeEach)

<div id="app">
            <router-link :to="{ name: 'home' }">首頁</router-link>
            <router-link :to="{ name: 'login' }">登錄頁面</router-link>
            <router-link :to="{ name: 'pay' }">支付</router-link>
            <router-view></router-view>
    </div>

    <script>
            let Home = { template: '<div><h2>這是首頁頁面</h2></div>' }
            let Login = { template: '<div><h2>這是登錄頁面</h2></div>' }
            let Pay = { template: '<div><h2>這是支付頁面</h2></div>' }
            let router = new VueRouter({
                    routes: [ 
                            { name: 'home', path: '/', component: Home, },
                            { name: 'login', path: '/login', component: Login, },
                            { name: 'pay', path: '/pay', meta: { required_login: true }, component: Pay, },
                    ]
            });

            // 通過router對象的beforeEach(function(to, from, next))
            router.beforeEach(function (to, from, next) {
                 console.log("to: ", to);
                 console.log("from: ", from);
                 console.log("next: ", next);
                 if ( to.meta.required_login ) {
                         next('/login');
                 } else {
                         next();
                 }
            });

            new Vue({
                    el: "#app",
                    router: router,
            })
    </script>
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

长乐市| 乌苏市| 洪江市| 商南县| 乐东| 鄂州市| 新乡县| 蛟河市| 图木舒克市| 花垣县| 遂平县| 新和县| 周宁县| 兴和县| 鹿泉市| 邳州市| 阿克| 阿拉善右旗| 富平县| 巨鹿县| 土默特左旗| 漳浦县| 威远县| 天全县| 荔波县| 常宁市| 商丘市| 望江县| 廉江市| 喀喇沁旗| 突泉县| 缙云县| 固镇县| 德兴市| 蓬莱市| 固安县| 安阳市| 石嘴山市| 新安县| 勃利县| 商丘市|