使用模块式开发实现简单的导航栏。
1.新建目录,创建项目
1 | // 安装vue |
测试1
2
3
4
5// 启动服务
npm run dev
// 浏览器测试
localhost:8080
2. 代码结构
项目结构:
项目代码主要在src目录下
assets目录:静态资源
components目录:组件
router目录:路由
main.js:主文件
App.vue:主组件
默认主js main.js
1 | import Vue from 'vue' |
默认主组件 App.vue
1 | <template> |
默认router router/index.js
1 | import Vue from 'vue' |
3.实施步骤
导航栏组件
导航页面,位于页面最顶端
- 默认加载 export default
- name: ‘TopBar’属性表示组件名
- template 要用 div 包裹
- style 要加 scoped 本地使用
components/TopBar.vue1
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 class="hello">
<h1>这是一个导航页面</h1>
<h2>{{message}}</h2>
<ul>
<li>
<router-link to="/menu">菜单</router-link>
</li>
<li>
<router-link to="/user">用户</router-link>
</li>
<li>
<router-link to="/about">关于</router-link>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'TopBar',
data () {
return {
message: 'Welcome to TopBar'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
菜单组件
components/Menu.vue1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<template>
<div>
<h1>这是一个Menu页面</h1>
<h2>{{message}}</h2>
</div>
</template>
<script>
export default {
name: 'Menu',
data() {
return {
message: 'Hello Menu'
}
}
}
</script>
<style>
</style>
用户组件
components/User.vue1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<template>
<div>
<h1>这是一个User页面</h1>
<h2>{{message}}</h2>
</div>
</template>
<script>
export default {
name: 'User',
data() {
return {
message: 'Hello User'
}
}
}
</script>
<style>
</style>
关于组件
components/About.vue1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<template>
<div>
<h1>这是一个About页面</h1>
<h2>{{message}}</h2>
</div>
</template>
<script>
export default {
name: 'About',
data() {
return {
message: 'Hello About'
}
}
}
</script>
<style>
</style>
路由
- import 组件,@代表编译目录
- 一定要用 Vue.use(Router) 使用Router
router/index.js1
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
33import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Menu from '@/components/Menu'
import User from '@/components/User'
import About from '@/components/About'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},
{
path: '/menu',
name: 'Menu',
component: Menu
},
{
path: '/user',
name: 'User',
component: User
},
{
path: '/about',
name: 'About',
component: About
}
]
})
App.vue改写
- 模板增加 TopBar 导航
- import TopBar
- 增加 components: TopBar
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<template>
<div id="app">
<TopBar></TopBar>
<img src="./assets/logo.png">
<router-view/>
</div>
</template>
<script>
import TopBar from '@/components/TopBar'
export default {
name: 'App',
components: {
TopBar
}
}
</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>
错误解决
tab缩进错误,这是eslint检查机制问题
文件内忽略错误
1 | 忽略下一行检查 |
具体参考 https://eslint.org/docs/user-guide/configuring
.eslintignore 配置
1 | // 增加以下配置,忽略src目录 eslint 检查 |
config/index.js 配置
1 | // 禁用eslint |
eslint配置(推荐)
1 | rules: { |
使用eslint –init配置