router的安装
路由用来实现页面跳转。
1.在cmd中进入你的项目项,即项目创建的目录中输入npm install vue-router --save-dev
即也可以在idea的Terminal中输入此命令,但是得是管理员得身份
2.可能会安装错误,可以根据提示输入命令修理
router的使用
1.删除项目创建的默认组件
2.创建自己的组件content.vue
<template>
<h1>内容页</h1>
</template>
<script>
export default {
name: "content"
}
</script>
<style scoped>
</style>
3.在src目录下创建文件夹router,并配置index.js
import Vue from 'vue'
//导入路由
import VueRouter from 'vue-router'
//导入组件
import Content from '../components/Content'
// 安装路由
Vue.use(VueRouter);
// 配置导出路由
export default new VueRouter({
routes:[
{
// 路由路径
path:'/content',
//名字
name:'content',
//跳转的组件
component:Content
}
]
});
4.配置路由在main.js
import Vue from 'vue'
import App from './App'
//导入路由,自动扫描index.js文件
import router from './router'
Vue.config.productionTip = false
new Vue({
el: '#app',
// 配置路由
router,
components: { App },
template: '<App/>'
})
5.展示app.vue
<template>
<div id="app">
//添加跳转
<router-link to="/mian">首页</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>
注意:如果我们项加入其他组件,只需要创建一个组件xxx.vue,在index.js的配置导出路由中添加一个对象,app.vue添加一个路由跳转即可。