Vue动态路由匹配


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue动态路由匹配 </title>
    <script src="../js/vue.js"></script>
    <script src="../js/vue-router_3.0.2.js"></script>
</head>

<body>
    <div id="app">
        <router-link to="/user/1">User1</router-link>
        <router-link to="/user/2">User2</router-link>
        <router-link to="/user/3">User3</router-link>
        <router-link to="/register">Register</router-link>

        <!-- 路由占位符 -->
        <router-view></router-view>
    </div>

    <script>
        const User = {
            template: '<h1>User组件 -- 用户id为:{{$route.params.id}}</h1>'
        }

        const Register = {
            template: '<h1>Register 组件</h1>'
        }

        //创建路由实例对象
        const router = new VueRouter({
            //所有路由的路由规则
            routes: [
                { path: '/', redirect: '/user' },
                { path: '/user/:id', component: User },
                { path: '/register', component: Register }
            ]
        });

        const vm = new Vue({

            el: '#app',
            data: {

            },
            //挂载路由实例对象
            //router:router
            router
        });
    </script>
</body>

</html>
上一篇:Arouter用法及原理2:源码介绍


下一篇:2021/6/15 django 模板层