Javascript

Vue动态路由匹配

本文主要是介绍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>
这篇关于Vue动态路由匹配的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!