工具:idea 想要详细了解Vue的话,视频链接:https://www.bilibili.com/video/BV18E411a7mC 博客链接:https://blog.csdn.net/DDDDeng_/article/details/107475920
Vue Router是Vue.js官方的路由管理器(路径跳转)。它和Vue.js的核心深度集成,让构建单页面应用变得易如反掌。包含的功能有:
- 嵌套的路由/视图表
- 模块化的、基于组件的路由配置
- 路由参数、查询、通配符
- 基于Vue.js过渡系统的视图过渡效果
- 细粒度的导航控制
- 带有自动激活的CSS class的链接
- HTML5历史模式或hash模式,在IE9中自动降级
- 自定义的滚动条行为
1.安装 基于第一个vue-cli进行测试学习;先查看node_modules中是否存vue-router ?vue-router 是一个插件包,所以我们还是需要用 npm/cnpm 来进行安装的。
打开这个项目: 
然后打开terminal:输入命令
npm install vue-router --save-dev
#这个-save-dev就是说会保存到dev文件里面

运行完成之后: 在src目录下找到main.js文件打开,然后添加一行import:
import Vue from 'vue'
import App from './App'
import VueRouter from "vue-router";
Vue.config.productionTip = false
Vue.use(VueRouter);
new Vue({
el: '#app',
components: { App },
template: '<App/>'
})
然后先运行起来: 
在src下的components目录下新建一个内容Content.vue:
<template>
<h1>内容页</h1>
</template>
<script>
export default {
name: "Content"
}
</script>
<!--scoped:-作用域-->
<style scoped>
</style>
在src下创建一个router文件夹,然后在router文件夹下面创建一个index.js文件,因为官方推荐用的就是index,所以在这里也是新建的index.js文件 在components里面创建一个Main.vue
Index.js:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Content from "../components/Content";
import Main from "../components/Main";
Vue.use(VueRouter);
export default new VueRouter({
routes: [
{
path: '/content',
name: 'content',
components: Content
},
{
path: '/main',
name: 'main',
components: Main
},
]
});
app.vue:
<template>
<div id="app">
<h1>vue-router</h1>
<!--跳转标签-->
<router-link to="/main">首页</router-link>
<router-link to="/content">内容页</router-link> <!--展示模板内容-->
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Main.vue:
<template>
<h1>首页</h1>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped></style>
效果:

|