<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script type="text/javascript" src="./js/vue.js"></script> <script src="./js/vuerouter.js"></script> </head> <body> <div id="app"> <router-link to="/user/1">User</router-link> <router-link to="/user/2">User</router-link> <router-link to="/user/3">User</router-link> <router-link to="/register">Register</router-link> <router-view></router-view> <script> const User = { template: `<h1>User组件--用户为:{{$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 }) </script> </div> </body> </html>