Vue框架——基本知识点+示例(4)

01 路由定义

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<!--
1.组件
定义组件
组件嵌套
组件传参

spa最直观的一个页面只有一个html
-->
<style>
/*<!--    默认类 用户点击到当前的内容,自动添加的类-->*/
    .router-link-active{
        color: #ff6600;
    }
</style>
<body>
<div id="app">
    <ul>
<!--        router-link页面跳转 类似于之前的a,最终渲染成了a标签,to代表的是路径-->
        <li>
            <router-link to="/like">
                 猜你喜欢
<!--             <img src="" alt="">如果要操作图片的话,将图片放置在此,通过自动添加的类(.router-link-active)进行操作-->
            </router-link>
        </li>
        <li><router-link to="/type">分类</router-link></li>
    </ul>

<!--    router-view渲染内容-->
    <router-view></router-view>
</div>

<!--定义模板-->
<template id="like">
    <h1>这是猜你喜欢的内容</h1>
</template>

<template id="type">
    <h1>这是分类内容</h1>
</template>

<!--vue.js只包含了基本的功能,如果需要特殊功能,专门引入-->
<script src="js/vue.js"></script>
<script src="js/vue-router.min.js"></script>
<script>
    var like={
        template:'#like',
    }
    var type={
        template:'#type'
    }

//    定义路由
    var router = new VueRouter({
    //    配置路线 为每一个路由配置路线 渲染的内容放置在router-view内部
        routes:[
            {
                path:'/like',
                component:like
            },
            {
                path:'/type',
                component: type
            },
        //    重定向,*所有的,以上都不符合的时候,加载like路由
            {
                path: '/*',
            //    ①直接路由跳转,如果有定义router-link-active类的话,可以直接使用(直接就有这个类了)
            //     redirect:'/like'

            //    ②只是单纯渲染组件,如果有定义router-link-active类的话,不会直接显示出来,要先点击一下,跳转到相应路由才行(点击后才有此类)
                component: like
            }
        ]
    })

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

02 路由嵌套

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <router-view></router-view>
    <ul>
        <li><router-link to="/like">猜你喜欢</router-link></li>
        <li><router-link to="/type">分类</router-link></li>
    </ul>
</div>

<template id="like">
    <h1>这是猜你喜欢的内容</h1>
</template>

<template id="type">
    <div>
        <h1>这是分类的内容</h1>
        <router-link to="/wash">洗衣机</router-link>
        <router-link to="/mobile">手机</router-link>
        <router-view></router-view>
    </div>
</template>

<!--放在子路由中的内容-->
<template id="wash">
    <h1>这是洗衣机内容</h1>
</template>

<template id="mobile">
    <h1>这是手机内容</h1>
</template>

<script src="js/vue.js"></script>
<script src="js/vue-router.min.js"></script>
<script>
    var type={
        template:'#type'
    }
    var like={
        template:'#like'
    }
    var wash={
        template:'#wash'
    }
    var mobile={
        template:'#mobile'
    }

    var router = new VueRouter({
        routes:[
            {
                path:'/like',
                component:like
            },
            {
                path:'/type',
                component: type,
            //    配置子路由,type组件内部一定要有router-view。如果没有的话,则不渲染,也不报错
                children:[
                    {
                        path:'/wash',
                        component:wash
                    },
                    {
                        path: '/mobile',
                        component: mobile
                    }
                ]
            },
            {
                path: '/*',
                redirect:'/like'
            }
        ]
    })

    new Vue({
        el:'#app',
    //    对象的属性和值一样,可以简写
        router,
    })
</script>
</body>
</html>

03 路由嵌套2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <router-view></router-view>
    <ul>
        <li><router-link to="/like">猜你喜欢</router-link></li>
        <li><router-link to="/type">分类</router-link></li>
    </ul>
</div>

<template id="like">
    <h1>这是猜你喜欢的内容</h1>
</template>

<template id="type">
    <div>
        <h1>这是分类的内容</h1>
        <router-link to="/wash">洗衣机</router-link>
        <router-link to="/mobile">手机</router-link>

<!--type里面的子路由要显示,下面的wash和mobile要相应的放在type的children下-->
<!--        <router-view></router-view>-->
    </div>
</template>

<template id="wash">
    <h1>这是洗衣机</h1>
</template>

<template id="mobile">
    <h1>这是手机</h1>
</template>

<script src="js/vue.js"></script>
<script src="js/vue-router.min.js"></script>
<script>
    var type={
        template:'#type'
    }
    var like={
        template:'#like'
    }
    var wash={
        template:'#wash'
    }
    var mobile={
        template:'#mobile'
    }

    var router = new VueRouter({
        routes:[
            {
                path:'/like',
                component:like
            },
            {
                path:'/type',
                component: type,
            //    配置子路由,type组件内部一定要有router-view
            //     children:[
            //         {
            //             path:'/wash',
            //             component:wash
            //         },
            //         {
            //             path:'/mobile',
            //             component: mobile
            //         }
            //     ]
            },
            //虽然wash和mobile是子路由,但是配置在第一层路由下面,则内容渲染到第一层的router-view内部
            {
                path:'/wash',
                component:wash
            },
            {
                path: '/mobile',
                component: mobile
            },
            {
                path: '/*',
                redirect:'/like'
            }
        ]
    })

    new Vue({
        el:'#app',
        //    对象的属性和值一样,可以简写
        router,
    })
</script>
</body>
</html>

04 路由传参

路径传参 通过路径传递参数
第一步: :to="‘路径’+传递参数"(router-link中添加)
第二步: 在routes里面 to=’/路径/:参数/:参数’
第三步: 接收参数 this.$route.params

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 <div id="app">
     <h1>这是列表页</h1>
     <ul>
         <li v-for="(item,index) in list">
<!--        路径传参 通过路径传递参数
            第一步: :to="'路径'+传递参数"(router-link中添加)
            第二步: 在routes里面 to='/路径/:参数/:参数'
            第三步: 接收参数 this.$route.params
-->
             <router-link :to="'/datails/'+item.id+'/'+item.name">
                {{item.name}}
             </router-link>
         </li>
     </ul>
     <router-view></router-view>
 </div>
<template id="details">
    <h2>这是{{pro}}商品详情</h2>
</template>

<script src="js/vue.js"></script>
<script src="js/vue-router.min.js"></script>
<script>
    var details={
        template:'#details',
        data(){
            return{
                pro:''
            }
        },
        mounted(){
            console.log(111);
            //接收参数
            console.log(this.$route.params);
        },
          // 监听,当路由发生变化的时候执行
        watch:{
            $route(to,from){
                // ajax请求
                // $.ajax({
                //     data:{
                //         id:this.$router.params.a
                //     }
                // })
              console.log(to.path);
              this.pro=this.$route.params.b
            }
        },
    }

    var router = new VueRouter({
        routes:[
            {
                // '/路径/:参数/:参数'
                // path:'/details/:id/:name',
                path:'/details/:a/:b',
                component:details
            }
        ]
    })

    new Vue({
        el:'#app',
        data:{
            list:[
                {
                    name:'手机',
                    id:1
                },
                {
                    name:'洗衣机',
                    id:2
                }
            ]
        },
        router
    })
</script>
</body>
</html>

05 路由应用

路由传参
第一步:to={name:对应的是配置路线里面的name值,params:{传递数据}}
第二步:配置路线里面增加name值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
<!--    第一层router-view-->
    <router-view></router-view>
</div>

<template id="index">
    <section>
<!--        第二层router-view-->
        <router-view></router-view>
            <ul>
                <li><router-link to="/like">猜你喜欢</router-link></li>
                <li><router-link to="/type">分类</router-link></li>
                <li><router-link to="/menu">必抢清单</router-link></li>
                <li><router-link to="/my">我的</router-link></li>
            </ul>
    </section>
</template>

<template id="like">
    <h1>这是猜你喜欢的内容</h1>
</template>

<template id="type">
    <div>
        <h1>这是分类内容</h1>
<!--        路由传参
        第一步:to={name:对应的是配置路线里面的name值,params:{传递数据}}
        第二步:配置路线里面增加name值
-->

        <router-link v-for="(item,index) in list" :to="{name:'d',params:{id:item.id,name:item.name}}">
            {{item.name}}
        </router-link>

        <!--        第 层 router-view-->
        <router-view></router-view>
    </div>
</template>

<template id="my">
    <h1>这是我的</h1>
</template>

<template id="menu">
    <h1>这是menu</h1>
</template>

<template id="details">
    <h1>这是{{type}}详情页</h1>
</template>

<script src="js/vue.js"></script>
<script src="js/vue-router.min.js"></script>
<script>
    var index={
        template:'#index'
    }
    var like={
        template:'#like'
    }
    var type={
        template:'#type',
        data(){
            return{
                list:[
                    // {id: 1, name: "手机"}
                    {
                        name:'手机',
                        id:1
                    },
                    {
                        name:'洗衣机',
                        id:2
                    }
                ]
            }
        }
    }
    var my={
        template:'#my'
    }
    var details={
        template:'#details',
        data(){
            return{
                type:''
            }
        },
        mounted(){
            console.log(this.$route.params);
            // 打印结果:{id: 1, name: "手机"}
            this.type=this.$route.params.name;
        },
        watch:{
            $route(to,from){
                console.log(111);
            }
        }
    }
    var menu={
        template:'#menu'
    }

    var router = new VueRouter({
        routes:[
            {
                // path:'/',
                path:'/like',
                component:index,
                children:[
                    {
                        path: '/like',
                        component: like
                    },
                    {
                        path: '/type',
                        component: type,

                        //渲染到第二层router-view,写在同一个页面中
                        // children:[
                        //     {
                        //         path:'/details',
                        //         name:'d',
                        //         component:details
                        //     }
                        // ]
                    },
                    {
                        path: '/menu',
                        component: menu
                    }
                ],
                redirect:'/like'
            },
            //写在第一层的router-view中,跳转到一个新的页面
            {
                path: '/details',
                name:'d',
                component: details
            },
            {
                path: '/my',
                component: my
            },
            {
                path: '/*',
                redirect: '/like'
            }
        ]
    })

    new Vue({
        el:'#app',
        router
    })


</script>
</body>
</html>

06 push和replace、go

1.返回,数值可以自定义:
-1返回上一个页面 -2返回上两个页面
this.$router.go(-2);

2.push在历史记录新增一条
this.$router.push(’/like’);

3.替换当前路径(去到替换的路径页面)
this.$router.replace(’/my’);

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <router-view></router-view>
</div>

<template id="index">
    <section>
        <router-view></router-view>
        <ul>
            <li><router-link to="/like">猜你喜欢</router-link></li>
            <li><router-link to="/type">分类</router-link></li>
            <li><router-link to="menu">必抢清单</router-link></li>
            <li><router-link to="my">我的</router-link></li>
        </ul>
    </section>
</template>

<template id="like">
    <h1>这是[猜你喜欢]的内容</h1>
</template>

<template id="type">
    <div>
        <h1>这是[分类]的内容</h1>
<!--        路由传参
        ①:to={name:对应的是配置路线里面的name值,params:{传递参数}}
        ②配置路线里面增加的name值
-->
        <router-link v-for="(item,index) in list" :to="{name:'d',params:{name:item.name,id:item.id}}">
            {{item.name}}
        </router-link>
        <router-view></router-view>
    </div>
</template>

<template id="menu">
    <h1>这是[menu]内容</h1>
</template>

<template id="my">
    <h1>这是[我的]内容</h1>
</template>

<template id="details">
    <h1><button @click="go()">返回</button>这是{{type}}详情页</h1>
</template>

<script src="js/vue.js"></script>
<script src="js/vue-router.min.js"></script>
<script>
    var index={
        template:'#index'
    }
    var like = {
        template:'#like'
    }
    var type = {
        template:'#type',
        //添加数据到type中
        data(){
            return{
                list:[
                    {
                        name:'手机',
                        id:1
                    },
                    {
                        name:'洗衣机',
                        id:2
                    }
                ]
            }
        }
    }
    var menu = {
        template:'#menu'
    }
    var my = {
        template:'#my'
    }
    var details = {
        template:'#details',
    //    添加数据到details中
        data(){
            return{
                type:''
            }
        },
        mounted(){
            console.log(this.$route.params);
            this.type=this.$route.params.name;
        },
        watch:{
            $route(to,from){
                console.log(to);
                console.log(from);
                console.log(111);
            }
        },
        methods:{
            go(){
            //    返回,数值可以自定义:-1返回上一个页面 -2返回上两个页面
            //     this.$router.go(-2);

            //    push在历史记录新增一条
                this.$router.push('/like');
                // console.log(this.$router);

            //    替换当前路径(去到替换的路径页面)
            //     this.$router.replace('/my');
            }
        }
    }

    //定义路由
    var router = new VueRouter({
        //配置路线
        routes:[
            {
                path:'/like',
                component:index,
                children:[
                    {
                        path:'/like',
                        component:like
                    },
                    {
                        path: '/type',
                        component: type
                    },
                    {
                        path:'/menu',
                        component: menu
                    }
                ],
                redirect:'/like'
            },
            //与index同级,渲染到第一层的router-view中
            {
                path: '/details',
                //第二步:配置路线里增加的name值
                name:'d',
                component: details
            },
            {
                path: '/my',
                component: my
            },
            {
                path:'/*',
                redirect: '/like'
            }
        ]
    })

    new Vue({
        el:'#app',
        //相当于router:router,因为同名所以可以缩写
        router
    })
</script>
</body>
</html>

07 另一种跳转实现方式

a.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    订单结算
</body>
</html>

b.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    表单页面
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<!--a的target和iframe的name值联系-->
<a href="a.html" target="box">结算页面</a>
<a href="b.html" target="box">表单页面</a>
<iframe src="a.html" frameborder="0" name="box"></iframe>
</body>
</html>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值