安装vue-router
步骤1:package.jsom:
"dependencies": {
"vue": "^2.5.2",
"vue-router": "^3.0.1" //只加入这句
},
步骤2:然后npm install
基础路由
main.js (四步骤)
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'
// 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)
import goods from './components/goods/goods'
import rating from './components/rating/rating'
import seller from './components/seller/seller'
Vue.use(VueRouter)
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{ path: '/goods', component: goods },
{path: '/rating', component: rating},
{path: '/seller', component: seller}
]
// 3. 创建 router 实例,然后传 `routes` 配置
const router = new VueRouter({
routes // (缩写)相当于 routes: routes
})
// 4. 创建和挂载根实例。
/* eslint-disable no-new */
var app = new Vue({
el: '#app',
components: { App },
template: '<App/>',
router
})
app.$mount('#app')
APP.vue --HTML写法(加入链接及链接内容位置)
<template>
<div id="app">
<v-header></v-header>
<div class="tab">
<div class="tab-item"><router-link to="/goods">商品</router-link></div>
<div class="tab-item"><router-link to="/rating">评论</router-link></div>
<div class="tab-item"><router-link to="/seller">商家</router-link></div>
</div>
<router-view></router-view>
</div>
</template>
<script type="text/ecmascript-6">
import header from './components/header/header'
export default {
components: {
'v-header': header
}
}
</script>
<style lang="stylus" rel="stylesheet/stylus">
.tab{
display :flex
width: 100%
height:48px
line-height:48px
}
.tab-item{
flex:1;
text-align center
}
</style>
各个组件