一、安装路由:npm install vue-router
二、使用步骤
1、定义组件
2、配置路由表
3、将路由器注入到实例中
4、定义路由出口
<!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>Document</title>
</head>
<body>
<div id="app">
<div class="header">头部</div>
<div class="left">
<p>
<ul>
<li><a href="#/foo">Go to Foo</a></li>
<li><a href="#/bar">Go to bar</a></li>
<!-- 使用 router-link 组件来导航,通过传入 `to` 属性指定链接 -->
<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
<li><router-link to="/foo">Go to Foo(router-link)</router-link></li>
<li><router-link to="/bar">Go to Bar(router-link)</router-link></li>
</ul>
</p>
</div>
<div class="main">
<!-- 4、定义路由出口 路由匹配到的组件渲染到这里-->
<router-view></router-view>
</div>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>Vue.config.productionTip = false;</script>
<script src="./node_modules/vue-router/dist/vue-router.js"></script>
<script>
// 1、定义组件
const Foo = {
template: "<div>我是Foo组件</div>"
}
const Bar = {
template: "<div>我是Bar组件</div>"
}
//2、配置路由表:当点击指定url时,显示对应的那个组件
const router=new VueRouter({
routes:[
{path:'/foo',component:Foo},
{path:'/bar',component:Bar}
]
})
new Vue({
el: '#app',
//3、将路由器注入到实例中
router:router
})
</script>
</body>
</html>