欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

前端Vue項目詳解--初始化及導(dǎo)航欄

 更新時間:2019年06月24日 16:50:53   作者:休耕  
這篇文章主要介紹了前端Vue項目詳解--初始化及導(dǎo)航欄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,,需要的朋友可以參考下

一、項目初始化

創(chuàng)建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".

根據(jù)提示啟動項目:

$ cd luffy_project/
$ npm run dev

由于在初始化時選擇了vue-router,因此會自動創(chuàng)建/src/router/index.js文件。

刪除Helloworld組件相關(guān)信息后,index.js文件內(nèi)容如下所示:

import Vue from 'vue'
import Router from 'vue-router'
// @絕對路徑 檢索到 ...src/
// 如果Router當(dāng)做局部模塊使用一定要Vue.use(Router)
// 以后在組件中,可以通過this.$router 獲取Router實例化對象
// 路由信息對象 this.$routes 獲取路由配置信息
Vue.use(Router)
// 配置路由規(guī)則
export default new Router({
routes: [
{
'path': '/'
}
]
})

二、基于ElementUI框架實現(xiàn)導(dǎo)航欄

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

elementUI是一個UI庫,它不依賴于vue,但確是當(dāng)前和vue配合做項目開發(fā)的一個比較好的UI框架。

(1)npm安裝

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

$ npm i element-ui -S

(2)CDN

目前可以通過 unpkg.com/element-ui 獲取到最新版本的資源,在頁面上引入 js 和 css 文件即可開始使用。

<!-- 引入樣式 -->
<link rel="stylesheet"  rel="external nofollow" >
<!-- 引入組件庫 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

使用CND引入 Element 需要在鏈接地址上鎖定版本,以免將來 Element 升級時受到非兼容性更新的影響。鎖定版本的方法請查看 unpkg.com。

2、引入 Element

在項目中可以引入整個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' // 注意樣式文件需要單獨引入
// 調(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á)到減小項目體積的目的。

首先安裝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)航欄實現(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>
&nbsp;| &nbsp;
<span>注冊</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è)計的一款全新的中文系統(tǒ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)建和路由配置編寫

添加“首頁”、“免費課程”、“輕課”、“學(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'
// @絕對路徑 檢索到 ...src/
// 如果Router當(dāng)做局部模塊使用一定要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)
// 配置路由規(guī)則
export default new Router({
routes: [
{
path: '/',
redirect: '/home' // 訪問/,直接跳轉(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頁面,編寫導(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>
&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: '學(xué)位課程'}
],
isShow: false
}
}
}
</script>

編寫headerList列表及列表中的導(dǎo)航對象,在 導(dǎo)航欄中遍歷對象獲取對應(yīng)信息,顯示在頁面效果如下所示:

3、router-link路由跳轉(zhuǎn)

經(jīng)過上面的編寫,雖然導(dǎo)航欄已經(jīng)可以正常顯示,但是a標(biāo)簽是不會做自動跳轉(zhuǎn)的。 需要使用 router-link 進(jìn)一步改寫LuffyHeader.vue,使得路由跳轉(zhuǎn)得以渲染對應(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>
&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: '學(xué)位課程'}
],
isShow: false
}
}
}
</script>

使用to='{name:list.name}'設(shè)置命令路由,這樣點擊a標(biāo)簽就可以跳轉(zhuǎn)了。顯示效果如下所示:

可以看到雖然點擊了輕課,但是和其他導(dǎo)航項樣式?jīng)]有任何分別,需要設(shè)置路由active樣式完成優(yōu)化。

4、linkActiveClass設(shè)置路由的active樣式

linkActiveClass 全局配置 <router-link> 的默認(rèn)“激活 class 類名”。

active-class 設(shè)置 鏈接激活時使用的 CSS 類名。默認(rèn)值可以通過路由的構(gòu)造選項 linkActiveClass 來全局配置。

(1)在路由配置linkActiveClass

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

import Vue from 'vue'
import Router from 'vue-router'
// @絕對路徑 檢索到 ...src/

// 如果Router當(dāng)做局部模塊使用一定要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)
// 配置路由規(guī)則
export default new Router({
linkActiveClass: 'is-active',
routes: [
{
path: '/',
redirect: '/home' // 訪問/,直接跳轉(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來模擬一個完整的URL,于是當(dāng)URL改變時,頁面不會重新加載。比如http://www.abc.com/#/index,hash值為#/index。hash模式的特點在于hash出現(xiàn)在url中,但是不會被包括在HTTP請求中,對后端沒有影響,不會重新加載頁面。

如果不想要這種顯示比較丑的hash,可以用路由的 history模式,這種模式充分利用 history.pushState API來完成URL跳轉(zhuǎn)而無需重新加載頁面。

(1)路由修改為history模式

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

import Vue from 'vue'
import Router from 'vue-router'
// @絕對路徑 檢索到 ...src/
// 如果Router當(dāng)做局部模塊使用一定要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)
// 配置路由規(guī)則
export default new Router({
linkActiveClass: 'is-active',
mode: 'history', // 改為history模式
routes: [
{
path: '/',
redirect: '/home' // 訪問/,直接跳轉(zhuǎn)到/home路徑
},
.....
]
})

使用history模式時,url就像正常url,例如http://yoursite.com/user/id,這樣比較美觀。

顯示效果如下所示:

(2)后端配置

但是要用好這種模式,需要后臺配置支持。vue的應(yīng)用是單頁客戶端應(yīng)用,如果后臺沒有正確的配置,用戶在瀏覽器訪問http://yoursite.com/user/id 就會返回404,這樣就不好了。

因此要在服務(wù)端增加一個覆蓋所有情況的候選資源:如果 URL 匹配不到任何靜態(tài)資源,則應(yīng)該返回同一個 index.html 頁面,這個頁面就是app依賴的頁面。

后端配置示例

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue實現(xiàn)ajax滾動下拉加載,同時具有l(wèi)oading效果(推薦)

    vue實現(xiàn)ajax滾動下拉加載,同時具有l(wèi)oading效果(推薦)

    這篇文章主要介紹了vue實現(xiàn)ajax滾動下拉加載,同時具有l(wèi)oading效果的實現(xiàn)代碼,文章包括難點說明,介紹的非常詳細(xì),感興趣的朋友參考下
    2017-01-01
  • vue實現(xiàn)文件上傳

    vue實現(xiàn)文件上傳

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue使用$attrs實現(xiàn)爺爺直接向?qū)O組件傳遞數(shù)據(jù)

    Vue使用$attrs實現(xiàn)爺爺直接向?qū)O組件傳遞數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了Vue如何使用$attrs實現(xiàn)爺爺直接向?qū)O組件傳遞數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2024-02-02
  • Vue2中無法檢測到數(shù)組變動的原因及解決

    Vue2中無法檢測到數(shù)組變動的原因及解決

    由于某些限制,vue2不能檢測到某些情況下數(shù)組的變動,本文就將具體講解這兩種限制的解決思路
    2021-06-06
  • vue3路由配置以及路由跳轉(zhuǎn)傳參詳解

    vue3路由配置以及路由跳轉(zhuǎn)傳參詳解

    路由跳轉(zhuǎn)的同時傳遞參數(shù)是比較常見的,下面這篇文章主要給大家介紹了關(guān)于vue3路由配置以及路由跳轉(zhuǎn)傳參的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • vue修改對象的屬性值后頁面不重新渲染的實例

    vue修改對象的屬性值后頁面不重新渲染的實例

    今天小編就為大家分享一篇vue修改對象的屬性值后頁面不重新渲染的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Vue-element-admin平臺側(cè)邊欄收縮控制問題

    Vue-element-admin平臺側(cè)邊欄收縮控制問題

    這篇文章主要介紹了Vue-element-admin平臺側(cè)邊欄收縮控制問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue.js 60分鐘快速入門教程

    Vue.js 60分鐘快速入門教程

    vuejs是當(dāng)下很火的一個JavaScript MVVM庫,它是以數(shù)據(jù)驅(qū)動和組件化的思想構(gòu)建的。今天給大家分享一篇教程關(guān)于Vue.js 60分鐘快速入門教程,一起看看吧
    2017-03-03
  • Vue Element前端應(yīng)用開發(fā)之功能點管理及權(quán)限控制

    Vue Element前端應(yīng)用開發(fā)之功能點管理及權(quán)限控制

    在一個業(yè)務(wù)管理系統(tǒng)中,如果我們需要實現(xiàn)權(quán)限控制功能,我們需要定義好對應(yīng)的權(quán)限功能點,然后在界面中對界面元素的功能點進(jìn)行綁定,這樣就可以在后臺動態(tài)分配權(quán)限進(jìn)行動態(tài)控制了,權(quán)限功能點是針對角色進(jìn)行控制的,也就是簡稱RBAC(Role Based Access Control)。
    2021-05-05
  • Vue前端生成UUID的方法詳解

    Vue前端生成UUID的方法詳解

    這篇文章主要給大家介紹了關(guān)于Vue前端生成UUID的相關(guān)資料,前端需要生成一個唯一標(biāo)識符作為ID時,可以使用Vue UUID插件,比如生成訂單號、用戶ID等等,需要的朋友可以參考下
    2023-09-09

最新評論