前端Vue項(xiàng)目詳解--初始化及導(dǎo)航欄
一、項(xiàng)目初始化
創(chuàng)建webpack模板項(xiàng)目如下所示:
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".
根據(jù)提示啟動(dòng)項(xiàng)目:
$ cd luffy_project/ $ npm run dev
由于在初始化時(shí)選擇了vue-router,因此會(huì)自動(dòng)創(chuàng)建/src/router/index.js文件。
刪除Helloworld組件相關(guān)信息后,index.js文件內(nèi)容如下所示:
import Vue from 'vue' import Router from 'vue-router' // @絕對(duì)路徑 檢索到 ...src/ // 如果Router當(dāng)做局部模塊使用一定要Vue.use(Router) // 以后在組件中,可以通過(guò)this.$router 獲取Router實(shí)例化對(duì)象 // 路由信息對(duì)象 this.$routes 獲取路由配置信息 Vue.use(Router) // 配置路由規(guī)則 export default new Router({ routes: [ { 'path': '/' } ] })
二、基于ElementUI框架實(shí)現(xiàn)導(dǎo)航欄
1、elementUI——適合Vue的UI框架
elementUI是一個(gè)UI庫(kù),它不依賴于vue,但確是當(dāng)前和vue配合做項(xiàng)目開發(fā)的一個(gè)比較好的UI框架。
(1)npm安裝
推薦使用 npm 的方式安裝,能更好地和 webpack 打包工具配合使用。
$ npm i element-ui -S
(2)CDN
目前可以通過(guò) unpkg.com/element-ui 獲取到最新版本的資源,在頁(yè)面上引入 js 和 css 文件即可開始使用。
<!-- 引入樣式 --> <link rel="stylesheet" rel="external nofollow" > <!-- 引入組件庫(kù) --> <script src="https://unpkg.com/element-ui/lib/index.js"></script>
使用CND引入 Element 需要在鏈接地址上鎖定版本,以免將來(lái) Element 升級(jí)時(shí)受到非兼容性更新的影響。鎖定版本的方法請(qǐng)查看 unpkg.com。
2、引入 Element
在項(xiàng)目中可以引入整個(gè)Element,或者是根據(jù)需要僅引入部分組件。
(1)完整引入
在 main.js 中寫入如下內(nèi)容:
import Vue from 'vue' import App from './App' import router from './router' // elementUI導(dǎo)入 import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' // 注意樣式文件需要單獨(dú)引入 // 調(diào)用插件 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"> <!-- 導(dǎo)航區(qū)域 --> <el-button type="info">信息按鈕</el-button> <router-view/> </div> </template> <script> export default { name: 'App' } </script>
顯示效果:
(2)按需引入
借助 babel-plugin-component,可以只引入需要的組件,以達(dá)到減小項(xiàng)目體積的目的。
首先安裝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 中寫如下內(nèi)容:
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); /* 或?qū)憺? * Vue.use(Button) * Vue.use(Select) */ new Vue({ el: '#app', render: h => h(App) });
3、導(dǎo)航欄實(shí)現(xiàn)
首先創(chuàng)建/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="#" rel="external nofollow" rel="external nofollow" > 導(dǎo)航鏈接 </a> </li> </ul> </div> <div class="nav-right"> <span>登錄</span> | <span>注冊(cè)</span> </div> </div> </el-header> </el-container> </template> <script> export default { name: 'LuffyHeader', data(){ return { } }, }; </script>
再創(chuàng)建/static/global/global.css文件:
* { padding: 0; margin: 0; } body { font-size: 14px; color: #4a4a4a; font-family: PingFangSC-Light; /*蘋果設(shè)計(jì)的一款全新的中文系統(tǒng)字體,該字體支持蘋果的動(dòng)態(tài)字體調(diào)節(jié)技術(shù)*/ } ul { list-style: none; } a { text-decoration: none; }
最后在App.vue中引入和使用組件:
<template> <div id="app"> <!-- 導(dǎo)航區(qū)域 --> <LuffyHeader/> <router-view/> </div> </template> <script> import LuffyHeader from '@/components/Common/LuffyHeader' export default { name: 'App', components:{ LuffyHeader } } </script>
顯示效果如下所示:
三、導(dǎo)航欄路由跳轉(zhuǎn)
1、組件創(chuàng)建和路由配置編寫
添加“首頁(yè)”、“免費(fèi)課程”、“輕課”、“學(xué)位課”四大組件,因此創(chuàng)建如下文件:
src/components/Home/Home.vue src/components/Course/Course.vue src/components/LightCourse/LightCourse.vue src/components/Micro/Micro.vue
在src/router/index.js中引入組件,配置路由規(guī)則:
import Vue from 'vue' import Router from 'vue-router' // @絕對(duì)路徑 檢索到 ...src/ // 如果Router當(dāng)做局部模塊使用一定要Vue.use(Router) // 以后在組件中,可以通過(guò)this.$router 獲取Router實(shí)例化對(duì)象 // 路由信息對(duì)象 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) // 配置路由規(guī)則 export default new Router({ routes: [ { path: '/', redirect: '/home' // 訪問(wèn)/,直接跳轉(zhuǎn)到/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、導(dǎo)航鏈接編寫
修改 LuffyHeader.vue頁(yè)面,編寫導(dǎo)航鏈接:
<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="#" rel="external nofollow" rel="external nofollow" > {{ list.title }} </a> </li> </ul> </div> <div class="nav-right"> <span>登錄</span> | <span>注冊(cè)</span> </div> </div> </el-header> </el-container> </template> <script> export default { name: 'LuffyHeader', data() { return { headerList: [ {id: '1', name: 'Home', title: '首頁(yè)'}, {id: '2', name: 'Course', title: '免費(fèi)課程'}, {id: '3', name: 'LightCourse', title: '輕課'}, {id: '4', name: 'Micro', title: '學(xué)位課程'} ], isShow: false } } } </script>
編寫headerList列表及列表中的導(dǎo)航對(duì)象,在 導(dǎo)航欄中遍歷對(duì)象獲取對(duì)應(yīng)信息,顯示在頁(yè)面效果如下所示:
3、router-link路由跳轉(zhuǎn)
經(jīng)過(guò)上面的編寫,雖然導(dǎo)航欄已經(jīng)可以正常顯示,但是a標(biāo)簽是不會(huì)做自動(dòng)跳轉(zhuǎn)的。 需要使用 router-link 進(jìn)一步改寫LuffyHeader.vue,使得路由跳轉(zhuǎn)得以渲染對(duì)應(yīng)組件:
<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> | <span>注冊(cè)</span> </div> </div> </el-header> </el-container> </template> <script> export default { name: 'LuffyHeader', data() { return { headerList: [ {id: '1', name: 'Home', title: '首頁(yè)'}, {id: '2', name: 'Course', title: '免費(fèi)課程'}, {id: '3', name: 'LightCourse', title: '輕課'}, {id: '4', name: 'Micro', title: '學(xué)位課程'} ], isShow: false } } } </script>
使用to='{name:list.name}'設(shè)置命令路由,這樣點(diǎn)擊a標(biāo)簽就可以跳轉(zhuǎn)了。顯示效果如下所示:
可以看到雖然點(diǎn)擊了輕課,但是和其他導(dǎo)航項(xiàng)樣式?jīng)]有任何分別,需要設(shè)置路由active樣式完成優(yōu)化。
4、linkActiveClass設(shè)置路由的active樣式
linkActiveClass 全局配置 <router-link> 的默認(rèn)“激活 class 類名”。
active-class 設(shè)置 鏈接激活時(shí)使用的 CSS 類名。默認(rèn)值可以通過(guò)路由的構(gòu)造選項(xiàng) linkActiveClass 來(lái)全局配置。
(1)在路由配置linkActiveClass
在 src/router/index.js 中做如下配置:
import Vue from 'vue' import Router from 'vue-router' // @絕對(duì)路徑 檢索到 ...src/ // 如果Router當(dāng)做局部模塊使用一定要Vue.use(Router) // 以后在組件中,可以通過(guò)this.$router 獲取Router實(shí)例化對(duì)象 // 路由信息對(duì)象 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) // 配置路由規(guī)則 export default new Router({ linkActiveClass: 'is-active', routes: [ { path: '/', redirect: '/home' // 訪問(wèn)/,直接跳轉(zhuǎn)到/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 默認(rèn) hash 模式——使用URL的hash來(lái)模擬一個(gè)完整的URL,于是當(dāng)URL改變時(shí),頁(yè)面不會(huì)重新加載。比如http://www.abc.com/#/index,hash值為#/index。hash模式的特點(diǎn)在于hash出現(xiàn)在url中,但是不會(huì)被包括在HTTP請(qǐng)求中,對(duì)后端沒(méi)有影響,不會(huì)重新加載頁(yè)面。
如果不想要這種顯示比較丑的hash,可以用路由的 history模式,這種模式充分利用 history.pushState API來(lái)完成URL跳轉(zhuǎn)而無(wú)需重新加載頁(yè)面。
(1)路由修改為history模式
修改 src/router/index.js 文件如下所示:
import Vue from 'vue' import Router from 'vue-router' // @絕對(duì)路徑 檢索到 ...src/ // 如果Router當(dāng)做局部模塊使用一定要Vue.use(Router) // 以后在組件中,可以通過(guò)this.$router 獲取Router實(shí)例化對(duì)象 // 路由信息對(duì)象 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) // 配置路由規(guī)則 export default new Router({ linkActiveClass: 'is-active', mode: 'history', // 改為history模式 routes: [ { path: '/', redirect: '/home' // 訪問(wèn)/,直接跳轉(zhuǎn)到/home路徑 }, ..... ] })
使用history模式時(shí),url就像正常url,例如http://yoursite.com/user/id,這樣比較美觀。
顯示效果如下所示:
(2)后端配置
但是要用好這種模式,需要后臺(tái)配置支持。vue的應(yīng)用是單頁(yè)客戶端應(yīng)用,如果后臺(tái)沒(méi)有正確的配置,用戶在瀏覽器訪問(wèn)http://yoursite.com/user/id 就會(huì)返回404,這樣就不好了。
因此要在服務(wù)端增加一個(gè)覆蓋所有情況的候選資源:如果 URL 匹配不到任何靜態(tài)資源,則應(yīng)該返回同一個(gè) index.html 頁(yè)面,這個(gè)頁(yè)面就是app依賴的頁(yè)面。
后端配置示例
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)ajax滾動(dòng)下拉加載,同時(shí)具有l(wèi)oading效果(推薦)
這篇文章主要介紹了vue實(shí)現(xiàn)ajax滾動(dòng)下拉加載,同時(shí)具有l(wèi)oading效果的實(shí)現(xiàn)代碼,文章包括難點(diǎn)說(shuō)明,介紹的非常詳細(xì),感興趣的朋友參考下2017-01-01Vue使用$attrs實(shí)現(xiàn)爺爺直接向?qū)O組件傳遞數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Vue如何使用$attrs實(shí)現(xiàn)爺爺直接向?qū)O組件傳遞數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-02-02Vue2中無(wú)法檢測(cè)到數(shù)組變動(dòng)的原因及解決
由于某些限制,vue2不能檢測(cè)到某些情況下數(shù)組的變動(dòng),本文就將具體講解這兩種限制的解決思路2021-06-06vue修改對(duì)象的屬性值后頁(yè)面不重新渲染的實(shí)例
今天小編就為大家分享一篇vue修改對(duì)象的屬性值后頁(yè)面不重新渲染的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Vue-element-admin平臺(tái)側(cè)邊欄收縮控制問(wèn)題
這篇文章主要介紹了Vue-element-admin平臺(tái)側(cè)邊欄收縮控制問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue Element前端應(yīng)用開發(fā)之功能點(diǎn)管理及權(quán)限控制
在一個(gè)業(yè)務(wù)管理系統(tǒng)中,如果我們需要實(shí)現(xiàn)權(quán)限控制功能,我們需要定義好對(duì)應(yīng)的權(quán)限功能點(diǎn),然后在界面中對(duì)界面元素的功能點(diǎn)進(jìn)行綁定,這樣就可以在后臺(tái)動(dòng)態(tài)分配權(quán)限進(jìn)行動(dòng)態(tài)控制了,權(quán)限功能點(diǎn)是針對(duì)角色進(jìn)行控制的,也就是簡(jiǎn)稱RBAC(Role Based Access Control)。2021-05-05