路由

作者: 疯狂的蜗牛Dianna | 来源:发表于2019-09-27 00:34 被阅读0次

路由: 是浏览器URL中的哈希值(# hash)与展示视图内容 之间的对应规则

指路
在web App 中,通过一个页面来展示和管理整个应用的功能

vue中的路由: 是hash 和component的对应关系,一个hash值对应一个组件

基本使用

基本使用
1.安装路由:
npm i vue-router
2.引入路由
3.实例化路由 + 把路由和vue关联起来
详细步骤 :四个步骤(入口 => 规则 => 组件 => 出口)
1.入口 (先用url地址作为入口)
2.设置路由匹配规则
3.注册组件
4.出口
<!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>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script src="./node_modules/vue-router/dist/vue-router.js"></script>
</head>

<body>
    <div id="app">
        <router-view></router-view>
        <!-- <one></one>
        <Two></Two>
        <Three></Three> -->
    </div>
</body>
<script>
    let One = Vue.component('One', {
        template: `<div>组件: One</div>`
    });
    let Two = Vue.component('Two', {
        template: `<div>组件: Two</div>`
    });
    let Three = Vue.component('Three', {
        template: '<div>组件: Three</div>'
    });
    // 实例化路由
    const router = new VueRouter({
        // 设置路由规则
        routes: [{
            path: '/one',
            component: One
        }, {
            path: '/two',
            component: Two
        }, {
            path: '/three',
            component: Three
        }]
    });
    var vm = new Vue({
        el: '#app',
        data: {},
        methods: {},
        router,
    });
</script>

</html>

命名视图 (多出口)

<!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>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script src="./node_modules/vue-router/dist/vue-router.js"></script>
</head>
<style>
    * {
        padding: 0;
        margin: 0;
    }

    .Head {
        font-weight: 400;
        font-size: 40px;
        text-align: center;
        height: 100px;
        line-height: 100px;
        background-color: pink;
    }

    /* 父元素 */
    .father {
        display: flex;
    }

    .Left {
        background-color: red;
        height: 100px;
        flex: 2;
    }

    .Right {
        background-color: blue;
        flex: 8;
        height: 100px;
    }
</style>

<body>
    <div id="app">
        <router-view class="Head" name="Head"></router-view>
        <router-view class="Left" name="Left"></router-view>
        <router-view class="Right" name="Right"></router-view>
    </div>
</body>
<script>
    //3. 组件
    const header = {
        template: `<div>头部</div>`,
    };
    const left = {
        template: `<div>左侧</div>`
    };
    const right = {
        template: `<div>右侧</div>`
    };
    // 2.路由
    const router = new VueRouter({
        routes: [{
            path: '/index',
            components: {
                Head: header,
                Left: left,
                Right: right
            }
        }]
    });
    // 1.实例
    const vm = new Vue({
        el: '#app',
        data: {},
        methods: {},
        router,
    });
</script>

</html>

页面显示

01.png

相关文章

  • thinkphp5学习笔记(三)路由配置

    URL请求的执行流程 路由模式 路由注册 路由规则 路由地址 路由参数 变量规则 路由分组 别名路由 路由绑定

  • larevel 路由索引

    基本路由:路由重定向、视图路由路由参数:必选、可选、正则表达式命名路由路由组:中间件、命名空间、子域名路由、路由前...

  • laravel路由

    2 路由格式 3 路由参数 4 .路由别名 5 .路由群组

  • Vue3: 前端路由的概念和原理

    1、什么是路由 路由(英文:router)就是对应关系。路由分为两大类:① 后端路由② 前端路由 2、后端路由 后...

  • 组件化2.路由框架的设计

    路由框架原理 路由框架是为了实现组件之间的通信 路由框架维护了一个分组的路由表路由表中存放了路由地址和路由信息路由...

  • vue中的路由

    vue中4中路由包含: 1、动态路由2、嵌套路由3、编程式路由4、命名式路由 1、动态路由 2、嵌套路由 3、编程...

  • React-Router知识点

    路由的分类 页面路由 hash 路由 h5路由 react路由 react-router-dom 路由方式 h5路...

  • 路由策略

    路由策略和策略路由 什么是路由策略?路由策略和策略路由有什么区别? 如何配置路由策略? https://blog....

  • Laravel 学习笔记

    路由 文件位置: app/Http/routes.php 基础路由get/post 多请求路由 路由参数 路由别名...

  • React路由

    React路由 一、路由的形式 hash路由 : HashRouter history路由 : BrowserRo...

网友评论

      本文标题:路由

      本文链接:https://www.haomeiwen.com/subject/wjcouctx.html