vue2.0 路由 学习笔记

vue2.0 路由:
http://router.vuejs.org/zh-cn/index.html
基本使用:
1.  布局
<router-link to="/home">主页</router-link>
<router-view></router-view>

2. 路由具体写法,重定向{path:'*', redirect:'/home'}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .router-link-active{
            font-size: 20px;
            color: green;
        }
    </style>
</head>
<body>
    <div id="box">
        <div>
            <router-link to="/home">主页</router-link>
            <router-link to="/news">新闻</router-link>
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>
    <script src="//cdn.bootcss.com/vue/2.2.1/vue.min.js"></script>
    <script src="//cdn.bootcss.com/vue-router/2.3.0/vue-router.min.js"></script>
    <script>
        // 组件
        var Home={
            template:"<h3>我是主页</h3>"
        }
        var News={
            template:"<h3>我是新闻</h3>"
        }

        // 配置路由
        var routes=[
            {path:'/home',component:Home},
            {path:'/news',component:News},
            {path:'*',redirect:'/home'}
        ];

        // 生成路由实例
        var router=new VueRouter({
            routes:routes
        })

        // 把路由挂到vue上
        new Vue({
            el:"#box",
            router:router
        })
    </script>
</body>
</html>


------------------------------------------
路由嵌套:
/user/username


const routes=[
   {path:'/home', component:Home},
   {
       path:'/user',
       component:User,
       children:[  //核心
           {path:'username', component:UserDetail}
       ]
   },
   {path:'*', redirect:'/home'}  //404

];

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .router-link-active{
            font-size: 20px;
            color: green;
        }
    </style>
</head>
<body>
    <div id="box">
        <div>
            <router-link to="/home">主页</router-link>
            <router-link to="/user">用户</router-link>
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>
    <script src="//cdn.bootcss.com/vue/2.2.1/vue.min.js"></script>
    <script src="//cdn.bootcss.com/vue-router/2.3.0/vue-router.min.js"></script>
    <script>
        // 组件
        var Home={
            template:"<h3>我是主页</h3>"
        }
        var User={
            template:`
                <div>
                    <h3>我是用户信息</h3>
                    <ul>
                        <li><router-link to="/user/username">某用户</router-link></li>
                    </ul>
                    <div>
                        <router-view></router-view>
                    </div>
                </div>
            `
        }
        var UserDetail={
            template:'<div>我是某用户</div>'
        }

        // 配置路由
        var routes=[
            {path:'/home',component:Home},
            {
                path:'/user',
                component:User,
                children:[
                    {path:'username',component:UserDetail}
                ]
            },
            {path:'*',redirect:'/home'}
        ];

        // 生成路由实例
        var router=new VueRouter({
            routes:routes
        })

        // 把路由挂到vue上
        new Vue({
            el:"#box",
            router:router
        })
    </script>
</body>
</html>

------------------------------------------

路由+参数:

/user/strive/age/10

:id
:username

:age

路由实例方法:
router.push({path:'home'});  //直接添加一个路由,表现切换路由,本质往历史记录里面添加一个
router.replace({path:'news'}) //替换路由,不会往历史记录里面添加

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .router-link-active{
            font-size: 20px;
            color: green;
        }
    </style>
</head>
<body>
    <div id="box">
        <input type="button" name="" value="添加一个路由" @click="push">
        <input type="button" name="" value="替换一个路由" @click="replace">
        <div>
            <router-link to="/home">主页</router-link>
            <router-link to="/user">用户</router-link>
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>
    <script src="//cdn.bootcss.com/vue/2.2.1/vue.min.js"></script>
    <script src="//cdn.bootcss.com/vue-router/2.3.0/vue-router.min.js"></script>
    <script>
        // 组件
        var Home={
            template:"<h3>我是主页</h3>"
        }
        var User={
            template:`
                <div>
                    <h3>我是用户信息</h3>
                    <ul>
                        <li>
                            <router-link to="/user/strive/age/10">
                                Strive
                            </router-link>
                        </li>
                        <li>
                            <router-link to="/user/blue/age/40">
                                Blue
                            </router-link>
                        </li>
                        <li>
                            <router-link to="/user/eric/age/70">
                                Eric
                            </router-link>
                        </li>
                    </ul>
                    <div>
                        <router-view></router-view>
                    </div>
                </div>
            `
        }
        var UserDetail={
            template:'<div>{{$route.params}}</div>'
        }

        // 配置路由
        var routes=[
            {path:'/home',component:Home},
            {
                path:'/user',
                component:User,
                children:[
                    {
                        path:':username/age/:age',
                        component:UserDetail
                    }
                ]
            },
            {path:'*',redirect:'/home'}
        ];

        // 生成路由实例
        var router=new VueRouter({
            routes:routes
        })

        // 把路由挂到vue上
        new Vue({
            router:router,
            methods:{
                push(){
                    router.push({path:'/home'})
                },
                replace(){
                    router.push({path:'/user'})
                }
            }
        }).$mount("#box")
    </script>
</body>
</html>

------------------------------------------

路由跳转配合 animate.css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link href="//cdn.bootcss.com/animate.css/3.5.2/animate.min.css" rel="stylesheet">
    <style>
        .router-link-active{
            font-size: 20px;
            color: green;
        }
    </style>
</head>
<body>
    <div id="box">
        <input type="button" name="" value="添加一个路由" @click="push">
        <input type="button" name="" value="替换一个路由" @click="replace">
        <div>
            <router-link to="/home">主页</router-link>
            <router-link to="/user">用户</router-link>
        </div>
        <div>
            <transition name="" mode="" enter-active-class="animated bounceInLeft" leave-active-class="animated bounceOutRight">
                <router-view></router-view>
            </transition>
        </div>
    </div>
    <script src="//cdn.bootcss.com/vue/2.2.1/vue.min.js"></script>
    <script src="//cdn.bootcss.com/vue-router/2.3.0/vue-router.min.js"></script>
    <script>
        // 组件
        var Home={
            template:"<h3>我是主页</h3>"
        }
        var User={
            template:`
                <div>
                    <h3>我是用户信息</h3>
                    <ul>
                        <li>
                            <router-link to="/user/strive/age/10">
                                Strive
                            </router-link>
                        </li>
                        <li>
                            <router-link to="/user/blue/age/40">
                                Blue
                            </router-link>
                        </li>
                        <li>
                            <router-link to="/user/eric/age/70">
                                Eric
                            </router-link>
                        </li>
                    </ul>
                    <div>
                        <router-view></router-view>
                    </div>
                </div>
            `
        }
        var UserDetail={
            template:'<div>{{$route.params}}</div>'
        }

        // 配置路由
        var routes=[
            {path:'/home',component:Home},
            {
                path:'/user',
                component:User,
                children:[
                    {
                        path:':username/age/:age',
                        component:UserDetail
                    }
                ]
            },
            {path:'*',redirect:'/home'}
        ];

        // 生成路由实例
        var router=new VueRouter({
            routes:routes
        })

        // 把路由挂到vue上
        new Vue({
            router:router,
            methods:{
                push(){
                    router.push({path:'/home'})
                },
                replace(){
                    router.push({path:'/user'})
                }
            }
        }).$mount("#box")
    </script>
</body>
</html>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值