1. 程式人生 > 實用技巧 >vue外賣一:vue-cli搭建專案、編碼測試、打包釋出專案、向量圖示、stylus、分析結構、向量字型圖示、render es6寫法、dos建檔案/夾、reset.css、移動端頭部、0.3s延遲

vue外賣一:vue-cli搭建專案、編碼測試、打包釋出專案、向量圖示、stylus、分析結構、向量字型圖示、render es6寫法、dos建檔案/夾、reset.css、移動端頭部、0.3s延遲

一、使用 vue-cli(腳手架)搭建專案

  1. Vue-cli是 vue 官方提供的用於搭建基於 vue+webpack+es6 專案的腳手架工具
  2. 線上文件: https://github.com/vuejs/vue-cli
    https://docs.qq.com/pdf/DSEdmZVVZWlV4amJP

3) 操作:

npm install -g vue-cli
vue init webpack gshop
cd gshop
npm install
npm run dev #具體在package.json裡檢視或修改此命令

訪問: localhost:8080

配置自動開啟瀏覽器config/index.js

開啟config/index.js,把1處原false改為true即可

// Various Dev Server settings
    host: 'localhost', // 
    port: 8080, // 執行埠,可改
    autoOpenBrowser: true, //【1】自動在瀏覽器開啟

二、編碼測試與打包釋出專案

1) 編碼測試

npm run dev

訪問: http://localhost:8080
編碼, 自動編譯打包(HMR), 檢視效果

2) 打包釋出

npm run build
npm install -g serve
serve dist

訪問: http://localhost:5000

三、相關資源-目錄設計建立-stylus包安裝

0.向量字型圖示

icoMoon 官網: https://icomoon.io
阿里巴巴向量庫: http://www.iconfont.cn

1.專案目錄設計及功能

2.建立目錄命令及檔案

進入src目錄用命令建立8個資料夾:

F:\gshop-client\src> mkdir api common components filte
rs mock pages router store

建立含空格的資料夾(檔名加個雙引號即可):mkdir "test demo1"

新建兩個檔案

App.vue
main.js

3.stylus stylus-loader安裝指定版本

這裡安裝指定版本,否則可能報錯
stylus:將程式碼轉化為css
-loader:讓webpack理解stylus

cnpm install [email protected] [email protected] --save-dev

解除安裝

npm uninstall stylus stylus-loader

頁面內聯樣式寫法

<style lang="stylus" rel="stylesheet/stylus">
</style>

實際寫一個styl檔案

src/common/stylus/common.styl

$green = #02a774;
$yellow = #F5A100;
$bc = #e4e4e4;

// 一畫素下邊框
bottom-border-1px($color)
	position relative
	border none
	&:after
	content ''
	position absolute
	left 0
	bottom 0
	width 100%
	height 1px
	background-color $color
	transform scaleY(0.5)

// 一畫素上邊框
top-border-1px($color)
	position relative
	&::before
	content ''
	position absolute
	z-index 200
	left 0
	top 0
	width 100%
	height 1px
	background-color $color

//根據畫素比縮放 1px 畫素邊框
@media only screen and (-webkit-device-pixel-ratio: 2 )
	.border-1px
	&::before
	transform scaleY(.5)
	@media only screen and (-webkit-device-pixel-ratio: 3 )
	.border-1px
	&::before
	transform scaleY(.333333)

//根據畫素比來使用 2x 圖 3x 圖
bg-image($url)
	background-image: url($url + "@2x.png")
	@media (-webkit-min-device-pixel-ratio: 3),(min-device-pixel-ratio: 3)
	background-image: url($url + "@3x.png")


//清除浮動
clearFix()
	*zoom 1
	&::after
	content ''
	display block
	clear both

四、分析應用的整體vue元件結構

src
|-- components------------非路由元件資料夾
 |-- FooterGuide---------------底部元件資料夾
  |-- FooterGuide.vue--------底部元件 vue 
  
|-- pages-----------------路由元件資料夾
 |-- Msite---------------首頁元件資料夾
  |-- Msite.vue--------首頁元件vue  
 |-- Search----------------搜尋元件資料夾
  |-- Search.vue---------搜尋元件 vue
 |-- Order--------------訂單元件資料夾
  |-- Order.vue-------訂單元件 vue
 |-- Profile--------------個人組件資料夾
  |-- Profile.vue-------個人組件 vue
  
|-- App.vue---------------應用根元件vue
|-- main.js---------------應用入口js

五、寫對應基礎程式碼

https://www.cnblogs.com/chenxi188/p/13891559.html

1.App.vue

<template>
	<div>
		app
	</div>
</template>

<script>
	export default {}
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>

2.main.js render的es6寫法

render: h => h(App)新寫法詳見:
https://www.cnblogs.com/chenxi188/p/13891559.html

// 入口檔案
import Vue from 'vue'
import App from './App'

// 2.0新版本es6寫法
new Vue({
	el: '#app',
	render: h => h(App)
})


/*
 //1.0版寫法
 new Vue({
  el: '#app',
  components: { App },
  template: '<App/>'
})
*/

3.dos建立pages/下資料夾

mkdir  Profile Order Search Msite

4.dos建立對應檔案

此處用了三種方式,儲存為bat檔案執行,或逐條複製到cmd執行
其中type效果最好,不會在檔案裡放任何內容

cd>./Profile/Profile.vue 
type nul>./Order/Order.vue
copy nul>./Search/Search.vue 
copy nul>./Msite/Msite.vue

分別寫入基礎程式碼:

<template>
<div>msite(對應的檔名)</div>
</template>

<script>
	export default{}
</script>

<style lang='stylus' rel='stylesheet/stylus'>
	
</style>

六、寫根目錄/static/css/reset.css並引入

/**
 * Eric Meyer's Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/)
 * http://cssreset.com
 */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header,
menu, nav, output, ruby, section, summary,
time, mark, audio, video, input {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font-weight: normal;
  vertical-align: baseline;
}

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, menu, nav, section {
  display: block;
}

body {
  line-height: 1;
}

blockquote, q {
  quotes: none;
}

blockquote:before, blockquote:after,
q:before, q:after {
  content: none;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}

/* custom */
a {
  color: #7e8c8d;
  text-decoration: none;
  -webkit-backface-visibility: hidden;
}

li {
  list-style: none;
}

::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}

::-webkit-scrollbar-track-piece {
  background-color: rgba(0, 0, 0, 0.2);
  -webkit-border-radius: 6px;
}

::-webkit-scrollbar-thumb:vertical {
  height: 5px;
  background-color: rgba(125, 125, 125, 0.7);
  -webkit-border-radius: 6px;
}

::-webkit-scrollbar-thumb:horizontal {
  width: 5px;
  background-color: rgba(125, 125, 125, 0.7);
  -webkit-border-radius: 6px;
}

html, body {
  width: 100%;
  height: 100%;
}

body {
  -webkit-text-size-adjust: none;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

/*顯示省略號*/
.ellipsis{
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

引入重置樣式 根目錄/index.html

<link rel="stylesheet" href="/static/css/reset.css">

七、主頁引入向量圖示樣式、移動端設定、及解決點選0.3秒延遲問題

含完整index.html程式碼

<!DOCTYPE html>
<html>
  <head>
	<meta charset="utf-8">
	<!-- 視窗定義:主要用於移動端 -->
	  <meta name="viewport"
	        content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
	  <title>gshop</title>
	  <!-- 重置樣式 -->
	  <link rel="stylesheet" href="/static/css/reset.css">
	  <!-- 向量圖示樣式 -->
	  <link rel="stylesheet" href="http://at.alicdn.com/t/font_518606_6676bmcalnrhehfr.css"></link>

	  <!-- 解決點選響應延時 0.3s 問題 含script標籤內程式碼 -->
	  <script src="https://as.alipayobjects.com/g/component/fastclick/1.0.6/fastclick.js"></script>
	  <script>
	    if ('addEventListener' in document) {
	      document.addEventListener('DOMContentLoaded', function() {
	        FastClick.attach(document.body);
	      }, false);
	    }
	    if(!window.Promise) {
	      document.writeln('<script src="https://as.alipayobjects.com/g/component/es6-promise/3.2.2/es6-promise.min.js"'+'>'+'<'+'/'+'script>');
	    }
	  </script>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>