<!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="{name:'user',params:{id:3}}">User</router-link> <router-link to="/register">Register</router-link> <router-view></router-view> <script> const User = { props: ['id', 'uname', 'age'], template: `<h1>{{id+uname+age}}</h1>` } const Register = { template: `<h1>Register组件</h1>` } //创建路由实例对象 const router = new VueRouter({ //所有路由规则 routes: [{ path: '/', redirect: '/user' }, { name: 'user', path: '/user/:id', component: User, props: route => ({ uname: 'ss', age: 20, id: route.params.id }) }, { path: '/register', component: Register }, ] }) const vm = new Vue({ el: '#app', data: {}, //挂在路由对象 router }) </script> </div> </body> </html>