目录结构:
index.html文件内容
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title></title> </head> <body> <div id="app"> <div> <router-link to="/">首页</router-link> <router-link to="/about">关于我们</router-link> </div> <div> <router-view></router-view> </div> </div> <!-- 注意js导入顺序,自己定义的要在下面 --> <script src="../js/vue.js"></script> <script src="../js/vue-router.js"></script> <script src="app.js"></script> </body> </html>
app.js文件
var routes = [ { path:'/', component:{ template:` <div> <h1>首页</h1> </div> ` , }, }, { path:'/about', component:{ template:` <div> <h1>关于我们</h1> </div> ` , }, } ]; var router = new VueRouter({ routes:routes, }); new Vue({ el:'#app', router: router, });