003-路由

Vue-router

vue-router 官网: https://router.vuejs.org/zh/guide/

更多的前端项目偏向于单页面应用程序,基于MVVM模式,单页面应用程序跳转不卡顿,切换流畅等;

基础知识点包括:路由跳转、路由跳转传参、嵌套路由、多视图、路由守卫等;

vue-router 在项目的引入包含两种方式:

  1. 通过 js 引入
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    
  2. 通过模块化,npm install vue-router

简单使用

<!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>
    <div id="app">

        <!-- 路由跳转 -->
        <router-link to="/foo">Go to Foo</router-link>
        <router-link to="/bar">Go to Bar</router-link>  

        <!-- 跳转引起的刷新 -->
        <router-view></router-view>

    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

    <script>
        const Foo = {
            template: `
                <div>Foo组件</div>
            `
        }

        const Bar = {
            template: `
                <div>Bar组件</div>
            `
        }

        const routes = [{
                path: '/',
                redirect: '/foo'
            }, {
                path: '/foo',
                component: Foo
            },
            {
                path: '/bar',
                component: Bar
            }
        ]

        const router = new VueRouter({
            routes
        })

        var app = new Vue({
            el: '#app',
            router,
            data: {

            },
        });
    </script>
</body>

</html>

路由跳转

  1. 通过 router-link 进行页面跳转;

    <router-link to="/foo">Go to Foo</router-link>	<!-- 路由跳转至 /foo -->
    <router-link to="/bar">Go to Bar</router-link>	<!-- 路由跳转至 /bar -->
    
  2. 通过 this.$router.push() 控制页面跳转;

    const Foo = {
        template: `
            <div>
                <div>Foo组件</div>
                <button @click="go()">跳转至bar</button>	
            </div>
        `,	// 点击按钮进行页面跳转
        methods: {
            go() {
                this.$router.push('/bar');  // 通过 this.$router.push() 进行路由跳转
            },
        }
    }
    

路由跳转传参

  1. 不修改路由表进行传参
    以 ‘/bar?param=val’ 形式给 bar 组件传参,bar 组件以 this.$route.query 接收

    // Foo 组件
    const Foo = {
         template: `
             <div>
                 <div>Foo组件</div>
                 <button @click="go()">跳转至bar</button>
             </div>
         `,
         methods: {
             go() {
                 this.$router.push('/bar?id=123');
             },
         }
     }
    
     // Bar 组件
     const Bar = {
         template: `
             <div>Bar组件</div>
         `,
         created(){
             console.log(this.$route.query); // {id: 123}
         }
     }
    
     // 路由表
     const routes = [{
             path: '/',
             redirect: '/foo'
         }, {
             path: '/foo',
             component: Foo
         },
         {
             path: '/bar',
             component: Bar
         }
     ]
    
  2. 修改路由表传参
    路由表修改为 ‘/bar/:id’, 以 ‘/bar/123’ 形式传参, 以 this.$route.params 接收

    // Foo 组件
    const Foo = {
        template: `
            <div>
                <div>Foo组件</div>
                <button @click="go()">跳转至bar</button>
            </div>
        `,
        methods: {
            go() {
                this.$router.push('/bar/123');
            },
        }
    }
    
    // Bar 组件
    const Bar = {
        template: `
            <div>Bar组件</div>
        `,
        created(){
            console.log(this.$route.params); // {id: 123}
        }
    }
    
    // 路由表
    const routes = [{
            path: '/',
            redirect: '/foo'
        }, {
            path: '/foo',
            component: Foo
        },
        {
            path: '/bar/:id',
            component: Bar
        }
    ]
    

嵌套路由

嵌套路由,即在 router-view 的组件中,又嵌套了 router-view。也就是说子路由的概念;
在这里插入图片描述
Foo 组件 与 Bar 组件之间的路由切换,为父路由切换;
在 Bar 组件中,又有 Bar 的子组件 son1 组件 与 son2 组件的切换,称为子路由切换;
二者共同构成嵌套路由;

// Foo 组件
const Foo = {
    template: `
        <div>
            <div>Foo组件</div>
            <button @click="go()">跳转至bar</button>
        </div>
    `,
    methods: {
        go() {
            this.$router.push('/bar/son2');
        },
    }
}

// Bar 组件	
const Bar = {
    template: `
        <div style="margin-left: 40px;">
            <div>Bar组件</div>
            <router-link to="/bar/son1">son1</router-link>
            <router-link to="/bar/son2">son2</router-link>

            <router-view></router-view>
        </div>
    `
}

const son1Component = {
    template: '<div>son1Component</div>'
}

const son2Component = {
    template: '<div>son2Component</div>'
}

// 路由表
const routes = [{
        path: '/',
        redirect: '/foo'
    }, {
        path: '/foo',
        component: Foo
    },
    {
        path: '/bar',
        component: Bar,
        children: [{    // 嵌套路由
                path: '',
                redirect: 'son1'
            },
            {
                path: 'son1',
                component: son1Component
            },
            {
                path: 'son2',
                component: son2Component
            }
        ]
    },
]

多视图

多视图:即同一个路由切换下,多个 router-view 进行切换。

<div id="app">

   <!-- 路由跳转 -->
    <router-link to="/foo">Go to Foo</router-link>
    <router-link to="/bar">Go to Bar</router-link>

    <!-- 跳转引起的刷新,多视图 -->
    <router-view></router-view>
    <router-view name="footer"></router-view>

</div>
 // Foo 组件
const Foo = {
    template: `<div>Foo组件</div>`
}

// Bar 组件
const Bar = {
    template: `<div>Bar组件</div>`
}

const footerComponent = {
    template: '<div>footerComponent</div>'
}

// 路由表
const routes = [{
        path: '/',
        redirect: '/bar'
    },
    {
        path: '/foo',
        component: Foo
    },
    {
        path: '/bar',
        components: {   // 多视图
            'footer': footerComponent,
            'default': Bar
        }
    },
]

路由守卫

监听路由变化,进行权限判断、页面滚动、动画加载等功能;

const router = new VueRouter({
    routes	// 路由表
})

// 页面滚动、动画加载、权限判断等
router.beforeEach((to, from, next) => {
    //console.log(to);
    next();
})
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统中有一个非常常用的路由检测工具,名为traceroute。它可以帮助我们检测信息从本地计算机到互联网另一端的主机所走的路径。traceroute通过发送小的数据包到目的设备直到其返回,来测量其需要多长时间。一条路径上的每个设备traceroute要测3次。输出结果中包括每次测试的时间(ms)和设备的名称(如有的话)及其IP地址。 使用traceroute命令非常简单,只需要在终端中输入traceroute加上目标主机的IP地址或域名即可。例如,我们可以使用以下命令来检测到百度服务器的路由路径: ``` traceroute www.baidu.com ``` 执行该命令后,我们将会看到类似以下的输出结果: ``` traceroute to www.a.shifen.com (14.215.177.38), 30 hops max, 60 byte packets 1 _gateway (192.168.1.1) 1.013 ms 1.012 ms 1.010 ms 2 10.10.10.1 (10.10.10.1) 1.009 ms 1.008 ms 1.006 ms 3 61.148.158.1 (61.148.158.1) 5.005 ms 5.003 ms 5.001 ms 4 61.148.158.9 (61.148.158.9) 5.000 ms 4.998 ms 4.996 ms 5 61.148.158.10 (61.148.158.10) 4.994 ms 4.992 ms 4.990 ms 6 202.97.33.221 (202.97.33.221) 5.986 ms 5.984 ms 5.982 ms 7 202.97.33.222 (202.97.33.222) 5.980 ms 5.978 ms 5.976 ms 8 202.97.58.238 (202.97.58.238) 5.972 ms 5.970 ms 5.968 ms 9 202.97.58.237 (202.97.58.237) 5.966 ms 5.964 ms 5.962 ms 10 202.97.94.54 (202.97.94.54) 5.956 ms 5.954 ms 5.952 ms 11 202.97.94.53 (202.97.94.53) 5.946 ms 5.944 ms 5.942 ms 12 202.97.94.50 (202.97.94.50) 5.936 ms 5.934 ms 5.932 ms 13 202.97.94.49 (202.97.94.49) 5.926 ms 5.924 ms 5.922 ms 14 202.97.94.46 (202.97.94.46) 5.912 ms 5.910 ms 5.908 ms 15 202.97.94.45 (202.97.94.45) 5.902 ms 5.900 ms 5.898 ms 16 202.97.94.42 (202.97.94.42) 5.888 ms 5.886 ms 5.884 ms 17 202.97.94.41 (202.97.94.41) 5.874 ms 5.872 ms 5.870 ms 18 202.97.94.38 (202.97.94.38) 5.860 ms 5.858 ms 5.856 ms 19 202.97.94.37 (202.97.94.37) 5.846 ms 5.844 ms 5.842 ms 20 202.97.94.34 (202.97.94.34) 5.828 ms 5.826 ms 5.824 ms 21 202.97.94.33 (202.97.94.33) 5.810 ms 5.808 ms 5.806 ms 22 202.97.94.30 (202.97.94.30) 5.792 ms 5.790 ms 5.788 ms 23 202.97.94.29 (202.97.94.29) 5.774 ms 5.772 ms 5.770 ms 24 202.97.94.26 (202.97.94.26) 5.752 ms 5.750 ms 5.748 ms 25 202.97.94.25 (202.97.94.25) 5.732 ms 5.730 ms 5.728 ms 26 202.97.94.22 (202.97.94.22) 5.710 ms 5.708 ms 5.706 ms 27 202.97.94.21 (202.97.94.21) 5.688 ms 5.686 ms 5.684 ms 28 202.97.94.18 (202.97.94.18) 5.662 ms 5.660 ms 5.658 ms 29 202.97.94.17 (202.97.94.17) 5.636 ms 5.634 ms 5.632 ms 30 * * * ``` 输出结果中,第一行显示了目标主机的IP地址和最大跳数,后面的每一行则显示了每个路由器的IP地址和名称,以及到达该路由器所需要的时间。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值