新建项目时选择自动下载vue-router

修改src/main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import ElementUI from 'element-ui' //新添加
import 'element-ui/lib/theme-chalk/index.css' //新添加,避免后期打包样式不同,要放在import App from './App';之前
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false
Vue.use(ElementUI)//新添加
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
render: h => h(App),
components: {App},
template: '<App/>'
})

修改src/router/index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import Vue from 'vue'
import Router from 'vue-router'
//引入组件
import Student from "../components/Student";
import Teacher from "../components/Teacher";

Vue.use(Router)

export default new Router({
mode: 'history',
//定义路由地址和组件
routes: [
{
path: '/Student',
component: Student
},
{
path: '/Teacher',
component: Teacher
}
]
})

App.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<template>
<div style="text-align: center">
<router-link to="/Student">学生</router-link>
<router-link to="/Teacher">教师</router-link>
<router-view></router-view>
</div>
</template>

<script>
export default {
name: 'App'
}
</script>

<style scoped>

</style>

Student.Vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<template>
<div>
<el-tag type="danger" style="margin-top: 10px">当前是学生表</el-tag>
<el-table :data="students" style="width: 60%;margin-left: 20%;margin-top: 20px" border>
<el-table-column prop="id" label="编号" align="center"></el-table-column>
<el-table-column prop="name" label="姓名" align="center"></el-table-column>
<el-table-column prop="sex" label="性别" align="center"></el-table-column>
</el-table>
</div>
</template>

<script>
export default {
name: 'Student',
data(){
return{
students:[
{
id:1,
name:'小米',
sex:'男'
},
{
id:2,
name:'小花',
sex:'女'
},
{
id:3,
name:'小明',
sex:'男'
},
{
id:4,
name:'雪莉',
sex:'女'
}
]
}
}
}
</script>

<style scoped>

</style>

Teacher.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<template>
<div>
<el-tag type="warning" style="margin-top: 10px">当前是教师表</el-tag>
<el-table :data="teachers" style="width: 60%;margin-left: 20%;margin-top: 20px" border>
<el-table-column prop="id" label="编号" align="center"></el-table-column>
<el-table-column prop="name" label="姓名" align="center"></el-table-column>
<el-table-column prop="sex" label="性别" align="center"></el-table-column>
</el-table>
</div>
</template>

<script>
export default {
name: 'Teacher',
data(){
return{
teachers:[
{
id:1,
name:'Tom',
sex:'男'
},
{
id:2,
name:'Sherry',
sex:'女'
},
{
id:3,
name:'Jack',
sex:'男'
},
{
id:4,
name:'Mary',
sex:'女'
}
]
}
}
}
</script>

<style scoped>

</style>

结果

img