引入Vue-router.js 的类库
<script src="../../lib/vue-router-3.0.1.js"></script>
创建一个VueRouter实例,
const router=new VueRouter({
routes:[
{path:"/one',componoent:one},
{path:"two",component:two}
]//路由规则/配置
})
在vue实例中引用路由
new Vue({
el:"#app",
router,
//*es6写法*/
}
在页面中写路由界面
<div id="app">
<div class="left">
<router-link to="/one">one</router-link>
<router-link to="/two">two</router-link>
</div>
<div class="right">
<router-view></router-view>
</div>
</div>
在router-link中写好路径名称
<router-link to="/one">one</router-link>
<router-link to="/two">two</router-link>
打开浏览路由效果
完整的代码:
<html>
<head>
<style>
.right{
border: 1px solid #e6e6e6;
}
</style>
</head>
<body>
<div id="app">
<div class="left">
<router-link to="/one">one</router-link>
<router-link to="/two">two</router-link>
</div>
<div class="right">
<router-view></router-view>
</div>
</div>
</body>
<script src="../../lib/vue-2.4.0.js"></script>
<script src="../../lib/vue-router-3.0.1.js"></script>
<script>
Vue.component('one',"#one")
var one={
template:`<h2>one</h2>`
}
var two={
template:`<h2>two</h2>`
}
const router=new VueRouter({//只能定义一个地址
routes:[
{path:'/one',component:one},
{path:'/two',component:two}
]
})
new Vue({
el:"#app",
router:router,
})
</script>
</html>
欢迎添加小明同学的微信:15005935876一起快乐学前端