一、项目初始化

  创建webpack模板项目如下所示:

MacBook-Pro:PycharmProjects hqs$ vue init webpack luffy_project

? Project name luffy_project
? Project description A Vue.js project
? Author hqs
? Vue build standalone
? Install vue-router? Yes
? Use ESLint to lint your code? No
? Set up unit tests No
? Setup e2e tests with Nightwatch? No
? Should we run `npm install` for you after the project has been created? (recommended) npm vue-cli · Generated "luffy_project".

  根据提示启动项目:

$ cd luffy_project/
$ npm run dev

  由于在初始化时选择了vue-router,因此会自动创建/src/router/index.js文件。

  删除Helloworld组件相关信息后,index.js文件内容如下所示:

import Vue from 'vue'
import Router from 'vue-router'
// @绝对路径 检索到 ...src/ // 如果Router当做局部模块使用一定要Vue.use(Router)
// 以后在组件中,可以通过this.$router 获取Router实例化对象
// 路由信息对象 this.$routes 获取路由配置信息
Vue.use(Router) // 配置路由规则
export default new Router({
routes: [
{
'path': '/'
}
]
})

二、基于ElementUI框架实现导航栏

1、elementUI——适合Vue的UI框架

  elementUI是一个UI库,它不依赖于vue,但确是当前和vue配合做项目开发的一个比较好的UI框架。

(1)npm安装

  推荐使用 npm 的方式安装,能更好地和 webpack 打包工具配合使用。

$ npm i element-ui -S

(2)CDN

  目前可以通过 unpkg.com/element-ui 获取到最新版本的资源,在页面上引入 js 和 css 文件即可开始使用。

<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

  使用CND引入 Element 需要在链接地址上锁定版本,以免将来 Element 升级时受到非兼容性更新的影响。锁定版本的方法请查看 unpkg.com

2、引入 Element

  在项目中可以引入整个Element,或者是根据需要仅引入部分组件。

(1)完整引入

  在 main.js 中写入如下内容:

import Vue from 'vue'
import App from './App'
import router from './router'
// elementUI导入
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css' // 注意样式文件需要单独引入
// 调用插件
Vue.use(ElementUI); Vue.config.productionTip = false; /* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
});

  以上代码便完成了 Element 的完整引入。

  尝试在App.vue使用elementui的Button按钮:

<template>
<div id="app">
<!-- 导航区域 -->
<el-button type="info">信息按钮</el-button> <router-view/>
</div>
</template> <script>
export default {
name: 'App'
}
</script>

  显示效果:

  

(2)按需引入

  借助 babel-plugin-component,可以只引入需要的组件,以达到减小项目体积的目的。

  首先安装babel-plugin-component:

$ npm install babel-plugin-component -D

  然后将.babelrc文件修改如下:

{
"presets": [["es2015", { "modules": false }]],
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
}

  如果只希望引入部分组件,如Buttion何Select,那么需要在 main.js 中写如下内容:

import Vue from 'vue';
import { Button, Select } from 'element-ui';
import App from './App.vue'; Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
/* 或写为
* Vue.use(Button)
* Vue.use(Select)
*/ new Vue({
el: '#app',
render: h => h(App)
});

3、导航栏实现

  首先创建/src/components/Common/LuffyHeader.vue文件:

<template>
<!-- element-ui -->
<el-container>
<el-header height = '80px' >
<div class="header">
<div class="nav-left">
<img src="https://www.luffycity.com/static/img/head-logo.a7cedf3.svg" alt="">
</div>
<div class="nav-center">
<ul>
<li>
<a href="#">
导航链接
</a>
</li>
</ul>
</div>
<div class="nav-right">
<span>登录</span>
&nbsp;| &nbsp;
<span>注册</span>
</div>
</div>
</el-header>
</el-container>
</template> <script>
export default {
name: 'LuffyHeader',
data(){
return {
}
},
};
</script>

  再创建/static/global/global.css文件:

* {
padding:;
margin:;
} body {
font-size: 14px;
color: #4a4a4a;
font-family: PingFangSC-Light; /*苹果设计的一款全新的中文系统字体,该字体支持苹果的动态字体调节技术*/
} ul {
list-style: none;
} a {
text-decoration: none;
}

  在main.js中引入全局样式文件:

import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css' // 样式需要单独引入
import '../static/global/global.css'
import '../static/global/gt.js' // 调用插件
Vue.use(ElementUI); Vue.config.productionTip = false

  最后在App.vue中引入和使用组件:

<template>
<div id="app">
<!-- 导航区域 -->
<LuffyHeader/>
<router-view/>
</div>
</template> <script>
import LuffyHeader from '@/components/Common/LuffyHeader'
export default {
name: 'App',
components:{
LuffyHeader
}
}
</script>

  显示效果如下所示:

  

三、导航栏路由跳转

1、组件创建和路由配置编写

  添加“首页”、“免费课程”、“轻课”、“学位课”四大组件,因此创建如下文件:

src/components/Home/Home.vue
src/components/Course/Course.vue
src/components/LightCourse/LightCourse.vue
src/components/Micro/Micro.vue

  在src/router/index.js中引入组件,配置路由规则:

import Vue from 'vue'
import Router from 'vue-router'
// @绝对路径 检索到 ...src/ // 如果Router当做局部模块使用一定要Vue.use(Router)
// 以后在组件中,可以通过this.$router 获取Router实例化对象
// 路由信息对象 this.$routes 获取路由配置信息
import Home from '@/components/Home/Home'
import Course from '@/components/Course/Course'
import LightCourse from '@/components/LightCourse/LightCourse'
import Micro from '@/components/Micro/Micro' Vue.use(Router) // 配置路由规则
export default new Router({
routes: [
{
path: '/',
redirect: '/home' // 访问/,直接跳转到/home路径
},
{
path: '/home',
name: 'Home',
component: Home
},
{
path: '/course',
name: 'Course',
component: Course
},
{
path: '/home/light-course',
name: 'LightCourse',
component: LightCourse
},
{
path: '/micro',
name: 'Micro',
component: Micro
}
]
})

2、导航链接编写

  修改 LuffyHeader.vue页面,编写导航链接:

<template>
<!-- element-ui -->
<el-container>
<el-header height = '80px' >
<div class="header">
<div class="nav-left">
<img src="https://www.luffycity.com/static/img/head-logo.a7cedf3.svg" alt="">
</div>
<div class="nav-center">
<ul>
<li v-for="(list, index) in headerList" :key="list.id">
<a href="#">
{{ list.title }}
</a>
</li>
</ul>
</div>
<div class="nav-right">
<span>登录</span>
&nbsp;| &nbsp;
<span>注册</span>
</div>
</div>
</el-header>
</el-container>
</template> <script>
export default {
name: 'LuffyHeader',
data() {
return {
headerList: [
{id: '1', name: 'Home', title: '首页'},
{id: '2', name: 'Course', title: '免费课程'},
{id: '3', name: 'LightCourse', title: '轻课'},
{id: '4', name: 'Micro', title: '学位课程'}
],
isShow: false
}
}
}
</script>

  编写headerList列表及列表中的导航对象,在 导航栏中遍历对象获取对应信息,显示在页面效果如下所示:

  

3、router-link路由跳转

  经过上面的编写,虽然导航栏已经可以正常显示,但是a标签是不会做自动跳转的。 需要使用 router-link 进一步改写LuffyHeader.vue,使得路由跳转得以渲染对应组件:

<template>
<!-- element-ui -->
<el-container>
<el-header height = '80px' >
<div class="header">
<div class="nav-left">
<img src="https://www.luffycity.com/static/img/head-logo.a7cedf3.svg" alt="">
</div>
<div class="nav-center">
<ul>
<li v-for="(list, index) in headerList" :key="list.id">
<router-link :to="{name:list.name}">
{{ list.title }}
</router-link>
</li>
</ul>
</div>
<div class="nav-right">
<span>登录</span>
&nbsp;| &nbsp;
<span>注册</span>
</div>
</div>
</el-header>
</el-container>
</template> <script>
export default {
name: 'LuffyHeader',
data() {
return {
headerList: [
{id: '1', name: 'Home', title: '首页'},
{id: '2', name: 'Course', title: '免费课程'},
{id: '3', name: 'LightCourse', title: '轻课'},
{id: '4', name: 'Micro', title: '学位课程'}
],
isShow: false
}
}
}
</script>

  使用to='{name:list.name}'设置命令路由,这样点击a标签就可以跳转了。显示效果如下所示:

  

  可以看到虽然点击了轻课,但是和其他导航项样式没有任何分别,需要设置路由active样式完成优化。

4、linkActiveClass设置路由的active样式

  linkActiveClass 全局配置 <router-link> 的默认“激活 class 类名”。

  active-class 设置 链接激活时使用的 CSS 类名。默认值可以通过路由的构造选项 linkActiveClass 来全局配置。

(1)在路由配置linkActiveClass

  在 src/router/index.js 中做如下配置:

import Vue from 'vue'
import Router from 'vue-router'
// @绝对路径 检索到 ...src/ // 如果Router当做局部模块使用一定要Vue.use(Router)
// 以后在组件中,可以通过this.$router 获取Router实例化对象
// 路由信息对象 this.$routes 获取路由配置信息
import Home from '@/components/Home/Home'
import Course from '@/components/Course/Course'
import LightCourse from '@/components/LightCourse/LightCourse'
import Micro from '@/components/Micro/Micro' Vue.use(Router) // 配置路由规则
export default new Router({
linkActiveClass: 'is-active',
routes: [
{
path: '/',
redirect: '/home' // 访问/,直接跳转到/home路径
},
......
{
path: '/micro',
name: 'Micro',
component: Micro
}
]
})

(2)在LuffyHeader.vue中配置路由active样式

<template>
......省略
</template> <script>
......省略
</script> <style lang="css" scoped>
.nav-center ul li a.is-active{
color: #4a4a4a;
border-bottom: 4px solid #ffc210;
}
</style>

(3)显示效果

  

5、hash模式切换为 history 模式

  vue-router 默认 hash 模式——使用URL的hash来模拟一个完整的URL,于是当URL改变时,页面不会重新加载。比如http://www.abc.com/#/index,hash值为#/indexhash模式的特点在于hash出现在url中,但是不会被包括在HTTP请求中,对后端没有影响,不会重新加载页面。

  如果不想要这种显示比较丑的hash,可以用路由的 history模式,这种模式充分利用 history.pushState API来完成URL跳转而无需重新加载页面。

(1)路由修改为history模式

  修改 src/router/index.js 文件如下所示:

import Vue from 'vue'
import Router from 'vue-router'
// @绝对路径 检索到 ...src/ // 如果Router当做局部模块使用一定要Vue.use(Router)
// 以后在组件中,可以通过this.$router 获取Router实例化对象
// 路由信息对象 this.$routes 获取路由配置信息
import Home from '@/components/Home/Home'
import Course from '@/components/Course/Course'
import LightCourse from '@/components/LightCourse/LightCourse'
import Micro from '@/components/Micro/Micro' Vue.use(Router) // 配置路由规则
export default new Router({
linkActiveClass: 'is-active',
mode: 'history', // 改为history模式
routes: [
{
path: '/',
redirect: '/home' // 访问/,直接跳转到/home路径
},
.....
]
})

  使用history模式时,url就像正常url,例如http://yoursite.com/user/id,这样比较美观。

  显示效果如下所示:

  

(2)后端配置

   但是要用好这种模式,需要后台配置支持。vue的应用是单页客户端应用,如果后台没有正确的配置,用户在浏览器访问http://yoursite.com/user/id 就会返回404,这样就不好了。

  因此要在服务端增加一个覆盖所有情况的候选资源:如果 URL 匹配不到任何静态资源,则应该返回同一个 index.html 页面,这个页面就是app依赖的页面。

  后端配置示例:https://router.vuejs.org/zh/guide/essentials/history-mode.html#%E5%90%8E%E7%AB%AF%E9%85%8D%E7%BD%AE%E4%BE%8B%E5%AD%90

前端Vue项目——初始化及导航栏的更多相关文章

  1. Vue.js-创建Vue项目(Vue项目初始化)并不是用Webstrom创建,只是用Webstrom打开

    我犯的错误:作为vue小白,并不知道还要单独去创建初始的vue项目,于是自己在webstrom中建了一个Empty Project, 在其中新增了一个js文件,就开始import Vue from “ ...

  2. vue项目初始化时npm run dev报错webpack-dev-server解决方法

    vue项目初始化时npm run dev报错webpack-dev-server解决方法 原因:这是新版webpack存在的BUG,卸载现有的新版本webpack,装老版本就好webpack-dev- ...

  3. 团队开发前端VUE项目代码规范

    团队开发前端VUE项目代码规范 2018年09月22日 20:18:11 我的小英短 阅读数 1658   一.规范目的: 统一编码风格,命名规范,注释要求,在团队协作中输出可读性强,易维护,风格一致 ...

  4. 前端Vue项目——登录页面实现

    一.geetest滑动验证 geetest官方文档地址:https://docs.geetest.com/ 产品——极速验证:基于深度学习的人机识别应用.极验「行为验证」是一项可以帮助你的网站与APP ...

  5. vue 项目初始化、mock数据以及安装less

    vue 创建一个项目 1.首先建立一个空文件夹,然后将这个文件夹要放到码云或者其他代码管理平台. 例如码云: 在码云上建立一个项目,然后在控制台进入这文件夹执行 git clone 地址是码云上创建的 ...

  6. [iOS微博项目 - 1.2] - 导航栏搜索框

    A.导航栏搜索框 1.需求 在“发现”页面,在顶部导航栏NavigationBar上添加一个搜索框 左端带有“放大镜”图标 github: https://github.com/hellovoidwo ...

  7. vue项目初始化步骤

    项目初始化:() 1. 安装vue-cli :    npm install -g vue-cli 2.初始化项目:   vue init webpack  my-project 3.进入项目:  c ...

  8. [前端]如何写一个水平导航栏?(浮动、inline-block+消除间距)

    在看W3school时,看到一个很好的例子,如何制作一个水平的导航栏?没有任何要求,只需要达到下面的效果: 我认为这个例子包含了很多css布局需要了解的知识,因此单独写一下. W3school上面的方 ...

  9. Vue项目初始化

    1. 生成项目模板 vue init <模板名> 本地文件夹名称2. 进入到生成目录里面 cd xxx npm install3. npm run dev vue项目模板介绍: simpl ...

随机推荐

  1. sed命令常用用法

    1.字符串替换 sed -i "s/xxx/yyy/g" /home/test.log // 将home目录下的test.txt文件中的所有xxx字符串替换成yyy字符串 sed ...

  2. 【shell脚本】打印九九乘法表

    打印九九乘法表 一.seq介绍 seq命令用于以指定增量从首数开始打印数字到尾数,即产生从某个数到另外一个数之间的所有整数,并且可以对整数的格式.宽度.分割符号进行控制 语法: [1] seq [选项 ...

  3. HDU 1723 Distribute Message DP

    The contest’s message distribution is a big thing in prepare. Assuming N students stand in a row, fr ...

  4. jetty9部署

    https://blog.51cto.com/5404542/1751702     Jetty 9部署web应用 Jetty相关的文章比较少,不过官方文档挺齐全的.做下记录也是好事. jetty9跟 ...

  5. C# 截图ScreenCapture,保存

    简化版: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  6. python 错误、调试、单元测试、文档测试

    错误分为程序的错误和由用户错误的输入引起的错误,此外还有因为各种各样意外的情况导致的错误,比如在磁盘满的时候写入.从网络爬取东西的时候,网络断了.这类错误称为异常 错误处理 参考链接:https:// ...

  7. java如何实现webservice中wsdlLocation访问地址的可配置化

    背景:项目中调用了别的系统的webservice接口,调用成功之后发现wsdlLocation的地址是写死的,不方便修改,所以需要实现地址,包括用户名密码的可配置.项目的框架是Spring,调用web ...

  8. FCC-学习笔记 Spinal Tap Case

    FCC-学习笔记   Spinal Tap Case 1>最近在学习和练习FCC的题目.这个真的比较的好,推荐给大家. 2>中文版的地址:https://www.freecodecamp. ...

  9. es6中,promise使用过程的小总结

    参考资料传送门:戳一戳 1.是什么 Promise是异步编程的一种解决方案,有三种状态:pending(进行中).fulfilled(已成功)和rejected(已失败); 一般成功了状态用resol ...

  10. 【java集合总结】-- 数组总结+自己封装数组类

    一.前言 本篇文章总结目前学习的有关数组方面的知识,首先总结一下数组相关的核心概念,然后在封装一个自己的泛型动态数组类(ava已经封装的有现成的,自己封装只是为了加深理解),最后再学习解析下Array ...