## 一、配置好proxyTable代理,需要重啟下項目,不然不生效
config/index.js 在開發(dev)配置下添加如下
~~~
proxyTable: {
'/api': {
target: 'http://xxx.com/api',
changeOrigin: true, //是否跨域
pathRewrite: {
// '^/api': '/api' //需要rewrite重寫的,
}
}
},
~~~
配置方式:https://www.cnblogs.com/congxueda/p/7087144.html
如果配置沒有生效,可以重啟下服務看下
## 二、修改build后的路徑
config/index.js -> build
~~~
assetsPublicPath: './',
~~~
## 三、禁止在瀏覽器自動打開
config/index.js -> dev
~~~
autoOpenBrowser: false,
~~~
## 四、vue-router使用history模式,build后在apache服務器訪問
首先配置個虛擬主機,如www.test.com 目錄指向網站根目錄中的build后的項目,然后新建個.htaccess文件,內容如下,
~~~
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
~~~


最后訪問 www.test.com 就可以了

>注意: 配置完虛擬主機后proxyTable就無效了
## 五、script text/template模板里不能使用v-for
~~~
<script type="text/template">
<ul>
<li v-for="item in data">
{{ item.name }}
</li>
</ul>
</script>
~~~
這樣會報item是undefined,,解決方案,把script text/template換成div容器
如果要獲取更新后的html
~~~
vm.$nextTick(function() {
console.log($('#js_flight').html());
});
~~~
## 六、如果不想雙向綁定可以這樣
~~~
<div id="js_app">
<input v-model="filterUser.name" />
{{ user.name }}
</div>
<script>
new Vue({
el: '#js_app',
data: {
user: {
name: 'zhangsan'
}
},
computed: {
filterUser: function() {
return JSON.parse(JSON.stringify(this.user));
}
}
});
~~~
## 七、在數據沒有錯誤的情況下,如果遇到sort不更新的話,就嘗試下加個key屬性
~~~
<ul>
<li v-for="item in list" :key="item.id">{{ item.sort }} {{ item.name }}</li>
</ul>
~~~
## 八、如果遇到key是undefined,那么整個vue會掛掉,這里只要在Kt函數里多加個判斷,判斷r是存在的
> function Kt(r,i){return r&&r.key===i.key&&(r.tag===i.tag&&r.isComment===i.isComment&&t(r.data)
## 九、build時去掉.map
config/index.js
~~~
productionSourceMap: false
~~~
## 十、nginx 服務器部署
下載 nginx for window
下載地址:http://nginx.org/en/download.html

到安裝目錄下啟動 nginx
E:\nginx-1.14.0
~~~
start nginx
~~~
訪問 localhost 就可以看到默認的頁面了。
下面我們把 vue build 的目錄放到 E:\nginx-1.14.0\html下,然后修改配置文件
~~~
location / {
try_files $uri $uri/ /index.html;
root html/dist;
index index.html index.htm;
}
location /api {
proxy_pass http://xxx/api; # 最好使用一個公共的path
#rewrite '/api' '/'
#add_header?'Access-Control-Allow-Origin'?'\*';
????#add_header?'Access-Control-Allow-Methods'?'GET,?POST,?OPTIONS';
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
~~~
重啟 nginx
~~~
nginx.exe -s reload
~~~
刷新瀏覽器就可以看到項目頁面了。
查看 nginx 命令
~~~
nginx -h
~~~

注意:如果Nginx報錯,就看下本地啟動的服務端口號與Nginx配置的各項服務端口號是否有沖突
## 十、解決背景圖片路徑不對的問題
在 build/utils.js里搜索ExtractTextPlugin,然后添加publicPath: '../../'
~~~
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
publicPath: '../../',
fallback: 'vue-style-loader'
})
} else {
return ['vue-style-loader'].concat(loaders)
}
~~~
## 十一、刷新頁面
**方式1:**
~~~
this.$router.go(0);
~~~
**方式2:**
```
location.reload();
```
**方式3:**
使用provide 和 inject 以允許一個祖先組件向其所有子孫后代注入一個依賴,不論組件層次有多深,并在起上下游關系成立的時間里始終生效。
App.vue
```
<template>
<div>
<router-view v-if="isRouterAlive"></router-view>
</div>
<template>
<script>
export default {
data() {
return {
isRouterAlive: true
}
},
provide() {
return {
reload: this.reload
}
},
methods: {
reload() {
this.isRouterAlive = false;
this.$nextTick(()=>{
this.isRouterAlive = true;
});
}
}
}
</script>
```
Home.vue
```
<template>
<div>
{{ timestamp }}
<button type="button" @click="refresh">刷新頁面</button>
</div>
</template>
<script>
export default {
name: 'home',
data() {
return {
timestamp: Date.now()
}
},
inject: ['reload'],
methods: {
refresh() {
this.reload();
}
}
}
</script>
```
注意:方式1和方式2刷新頁面體驗不是很好,如果網速不夠快的話,會有一瞬間的白屏
## 十二、兼容IE
~~~
npm install babel-polyfill --save-dev
~~~
然后在webpack.base.conf.js里,改下入口配置,如下
~~~
entry: {
'babel-polyfill': 'babel-polyfill',
app: './src/main.js'
}
~~~
## 十三、vue-router 3.x懶加載
官方推薦:https://router.vuejs.org/guide/advanced/lazy-loading.html#grouping-components-in-the-same-chunk
~~~
let Login = ()=>import('@pages/login/index')
~~~
其它方式:
~~~
let Login = resolve=>require(['@/pageslogin/index'], resolve)
~~~
如果build后,跳轉頁面沒有內容,可能沒找到文件,這時可以看下
config/index.js下的build的路徑配置,確保assetsPublicPath為根目錄
~~~
build: {
// ...
assetsPublicPath: '/'
~~~
## 十四、如遇到JSON.stringify編譯報錯,可以改成window.JSON.stringify試下
## 十五、如果vendor打包文件過大,可以試著拆分,通過cdn引入,如果還想能過import導入使用,可以在webpack.base.conf.js里加上externals配置
https://webpack.js.org/configuration/externals/#src/components/Sidebar/Sidebar.jsx
~~~
entry: {},
output: {},
externals: {
'vue': 'Vue',
'vue-router': 'VueRouter',
'vuex': 'Vuex',
'axios': 'axios',
'element-ui': 'ElementUI'
}
~~~
## 十六、gzip壓縮chunked傳輸nginx配置
~~~
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#開啟gzip
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/css application/javascript image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
try_files $uri $uri/ /index.html;
root html/dist;
index index.html index.htm;
chunked_transfer_encoding on; // 開啟chunk
}
# 緩存js、css
location ~ .*\.(js|css)?$ {
expires 1h;
}
location /test {
proxy_pass http://www.xxx.com/test;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#...
~~~
## 十七、nginx修改上傳文件大小,在http下添加client_max_body_size配置就行了,說明下如:
~~~
Syntax: client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location
~~~
## 十八、單頁內路由跳轉
1、路由模式使用hash,或者不設,因為默認mode就是hash
## 十九、如果遇到key是未定義的,這時可能是因為在綁定key的時候有重復,可以針對改下ke?y
- 事件
- mouse縮放與拖動
- drag拖動
- 事件兼容
- animation/transition
- canvas
- 改變圖片顏色
- html轉圖片
- 視頻操作
- 圖片縮放、水印、放大鏡
- 虛線
- 圓環進度條
- 形狀事件
- 圓角矩形
- 繪制注意
- arcTo與貝塞爾
- 橢圓及橢圓進度
- 五角星進度
- 常用圖形
- 計算顯示文本寬度
- 算法
- 幾何算法
- 地圖應用相關
- 運行符
- web安全
- 新窗口打開
- xss
- 分享交流
- php環境搭建及xhr交互
- node環境搭建及xhr交互
- node之socketio
- svg之入門介紹
- svg動畫
- vue之搜索聯想
- vue之登錄和echarts
- vue之組件交互與slot
- vue之loading
- vue之上傳進度
- webpack及cli
- 開發技巧
- 常用
- 移動端
- 錯誤處理
- 預加載
- 代理判斷
- 數組擴展
- 對象擴展
- 字符串擴展
- 語音播報
- 收集
- 文章/日記
- 框架/庫/插件
- 工具
- 學習網站
- 專業術語
- 正則
- 常用驗證
- 方法基礎
- es6擴展
- 深入實踐
- 快捷使用
- html
- css
- http協議
- http
- https
- socket
- 地圖/圖表
- mapbox
- echarts
- arcgis
- MapView及事件
- 添加WMS/WMTS層
- 增刪點線面
- 入門使用
- popup彈層
- 大數據處理
- 批量點
- 批量線
- 在線繪制
- GraphicLayer顯示/隱藏
- 動態改變位置
- 去除版權信息
- 添加控件
- Symbol
- 自定義path標記
- 圖片標記
- 文本標記
- 旋轉
- UI
- 自定義
- 3D地圖
- 創建實例
- basemap
- 底圖切換
- 自定義底圖
- 中心和范圍
- pupup彈層更新
- 坐標轉換
- 方向線
- leaflet
- amap
- 框架/類庫/腳手架
- vue
- 常見問題
- 組件框架
- vue-router
- 命名視圖
- url參數映射到prop
- sublime支持
- 隨手記
- 常用功能
- threejs
- 常用效果
- 其他特效
- requirejs
- 簡單使用
- jquery
- 方法擴展
- 使用筆記
- 組件擴展
- react
- 黨見問題
- 學習筆記
- 學習筆記-進階
- react-redux
- react-router
- redux
- 其他模塊說明
- 組件框架
- sublime支持
- gulp
- 安裝使用
- js壓縮
- css壓縮
- 組合使用
- copy文件
- 項目使用
- protobuf
- 入門
- layui
- 登錄驗證
- laydate
- 安裝工具
- yarn
- reactNative
- 入門介紹
- vueNative
- 入門介紹
- 版本控制
- git常用
- git擴展
- git問題
- git其他
- git擴展2
- 編輯器
- vscode
- atom
- webstorm
- 插件
- clipboard
- 奇淫巧技
- js
- 個性打印
- css
- 濾鏡效果
- 文本省略
- 當前色
- 新特性
- 花樣邊框效果
- 波紋效果
- 個性placeholder
- 偽元素內容
- 容器居中
- 知識點
- js
- 遞歸
- 沙箱
- 內存泄漏
- es6語法
- 變量介紹
- FileRead
- ajax
- web存儲
- css
- rem布局