【vue中路由的使用】

什么是路由
后端路由:

对于普通的网站,所有的超链接都是URL地址,所有的URL地址都对应服务器上对应的资源

前端路由:

对于单页面应用程序来说,主要通过URL中的hash(#号)来实现不同页面之间的切换,同时,hash有一个特点:HTTP请求中不会包含hash相关的内容;所以,单页面程序中的页面跳转主要用hash实现;
在单页面应用程序中,这种通过hash改变来切换页面的方式,称作前端路由(区别于后端路由)

路由的基本使用

● 引入js文件,这个js需要放在vue的js后面,自动安装(提供了一个VueRouter的构造方法)
● 创建路由new VueRouter(),接受的参数是一个对象
● 在实例化的对象里配置属性routes:[],这个数组里的对象包含path属性和component属性
● path属性是url的地址,component属性就是显示的组件(传组件的对象)
● 创建的路由需要和vue实例关联一下
● 路由到的组件显示在哪个位置

路由的跳转

● router-link标签可以设置to属性
● 默认是a标签,可以通过tag设置包裹标签
使用浏览器参数的方式传递参数
● 设置路由的时候/路由地址/:参数名
● 获取参数$route.params.参数名

组件的嵌套

● 声明路由的时候设置children,这是children是一个数组,数组里是路由对象
● 这个children的组件就会渲染在它父组件的中

路由重定向

redirect可以进行路由的重定向

选中路由高亮

使用默认的样式
直接设置 router-link-active
自定义样式
配置 linkActiveClass:‘自定义的类名’

代码解析
<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></title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!--  1 引入js文件,这个js需要放在vue的js后面,自动安装(提供了一个VueRouter的构造方法) -->
    <script src="https://unpkg.com/vue-router@3.0.0/dist/vue-router.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        /* 路由高亮 */
        .router-link-active {
            font-size: 40px;
            color: pink;
        }


        .active {
            font-size: 40px;
            color: pink;
        }
    </style>
</head>


<body>
    <div id='app'>
        <!-- 
            1   引入js文件,这个js需要放在vue的js后面,自动安装(提供了一个VueRouter的构造方法)
            2   创建路由new VueRouter(),接受的参数是一个对象
            3   在实例化的对象里配置属性routes:[],这个数组里的对象包含path属性和component属性
            4   path属性是url的地址,component属性就是显示的组件(传组件的对象)
            5   创建的路由需要和vue实例关联一下
            6   路由到的组件显示在哪个位置<router-view></router-view>
         -->
        <!-- 5、预留展示区域 -->
        <router-view></router-view>
        <router-view name="detail"></router-view>
        <router-view name="mine"></router-view>
        <router-link :to="{path:'/detail', query:{courseid:123,id:456} }">去详情1</router-link>


        <router-link :to="{name:'my',params:{ids:789} }">去个人中心</router-link>
    </div>
    <template id="index">
        <div>
            首页
            <a href="#/detail?coreseId=123">去详情a</a>
            <!--    router-link标签可以设置to属性 -->
            <router-link to="/detail">去详情</router-link>
            <!-- 声明式的路由跳转 -->                                                                
            <!-- 路由的跳转  传参 -->
            <router-link :to="{path:'/detail', query:{courseid:123,id:456} }">去详情1</router-link>
            <router-link :to="{name:'my',params:{ids:789} }">去个人中心</router-link>
            <!-- 错误写法 -->
            <!-- <router-link :to="{name:'my' }">去个人中心1</router-link> -->


            <!-- 函数式 -->
            <button @click="toDetail">toDetail</button>
            <button @click="toMine">toMine</button>
        </div>
    </template>


    <!-- 1. 声明路由的时候设置children,这是children是一个数组,数组里是路由对象
         2. 这个children的组件就会渲染在它父组件的<router-view>中
 -->


    <template id="detail">
        <div>
            详情页
            <router-view></router-view>
        </div>
    </template>


    <template id="mine">
        <div>
            个人中心
        </div>
    </template>


    <template id="play">
        <div>
            play
        </div>
    </template>



    <template id="study">
        <div>
            study
        </div>
    </template>


    <script>


        let play = {
            template: '#play'
        }
        let study = {
            template: '#study'
        }
        let index = {
            template: '#index',
            methods: {
                toDetail() {
                    console.log(1);
                    // this.$router.push('/detail')
                    this.$router.push({
                        path: '/detail',
                        query: {
                            id: 12312
                        }
                    })
                },
                toMine() {
                    this.$router.push({
                        name: 'my',
                        params: {
                            ids: 8908
                        }
                    })
                }
            }
        }


        let detail = {
            template: '#detail',
            created() {
                console.log(this);
                console.log(this.$route.query);
                console.log(this.$route.query.courseid);
            }
        }


        let mine = {
            template: '#mine',
            created() {
                console.log(this);
                console.log(this.$route.params.ids);
            }
        }


        // 2、创建路由实例
        const router = new VueRouter({
            // 3、创建映射关系
            routes: [
                // 路由的重定向
                {
                    path: '/',
                    redirect: '/index'
                },
                {
                    path: '/index',
                    // component: index
                    // 命名视图
                    components: {
                        default: index,
                        detail,
                        mine
                    }
                },
                {
                    path: '/detail',
                    component: detail,
                    // 路由嵌套
                    children: [
                        // path路径后不能加 / 
                        {
                            path: 'play',
                            component: play
                        },
                        {
                            path: 'study',
                            component: study
                        }
                    ]
                },
                {
                    path: '/mine/:ids',
                    name: 'my',
                    component: mine
                }
            ],
            // 配置 linkActiveClass: '自定义的类名'
            linkActiveClass: 'active'
        })
        const vm = new Vue({
            // 4、将路由实例挂载到vue实例上
            // router: router,
            router,
            el: '#app',
            data: {
            },
            methods: {
            }
        })
    </script>
</body>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中路由的实现方式有两种:hash模式和history模式。 1. hash模式 在hash模式下,路由信息会保存在URL的hash中,以#号开头。例如:http://localhost:8080/#/home。Vue Router会监听URL的变化,当URL的hash发生变化时,会根据新的hash值切换到对应的组件。 2. history模式 在history模式下,路由信息会保存在浏览器的历史记录中。例如:http://localhost:8080/home。Vue Router会使用HTML5的History API来实现路由切换。在history模式下,URL中不再需要#号,看起来更加清晰。 在Vue使用路由,需要引入Vue Router,然后定义路由规则和对应的组件。最后在Vue实例中挂载Vue Router。 例如: ``` import Vue from 'vue' import Router from 'vue-router' import Home from '@/components/Home' import About from '@/components/About' Vue.use(Router) export default new Router({ mode: 'history', routes: [ { path: '/', name: 'home', component: Home }, { path: '/about', name: 'about', component: About } ] }) ``` 这里定义了两个路由规则,一个是/,对应的组件是Home,另一个是/about,对应的组件是About。最后在Vue实例中挂载Vue Router。 例如: ``` import Vue from 'vue' import App from './App.vue' import router from './router' Vue.config.productionTip = false new Vue({ router, render: h => h(App) }).$mount('#app') ``` 在模板中使用路由链接和路由视图,例如: ``` <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> <router-view></router-view> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值