Vuejs2 + Webpack框架里,模擬下載的實例講解
在實際的開發(fā)工作中,難免要配合銷售人員,提前做一些前端的 DEMO 出來。這個時候往往還沒有連接后端 API。假如要演示一個下載連接,那么應(yīng)該如何做呢?
我們希望能夠達成以下兩點:
1、在開發(fā)環(huán)境下,我們可以在 webpack-dev-server 開發(fā)服務(wù)器上點擊下載連接,點擊后瀏覽器就能不下載文件。
2、當演示的時候,代碼編譯后放到 nginx 中。用戶可以點擊下載鏈接。nginx存放的都是業(yè)務(wù)代碼。
那么如何做到這兩點呢?假如我們要模擬下載一個 test.docx 文件。我們可以利用 url-loader 來對 .docx 文件做處理??赡苡腥藭枺骸皍rl-loader 一般不是處理 img 標簽里面的圖片文件路徑嗎?”為了解決這個 img 標簽的問題,我們可以在一個頁面中加上隱藏的圖片標簽。最后加一個 a 標簽: <a href="/test.docx" rel="external nofollow" rel="external nofollow" >下載</a>。下面的篇幅要幫助讀者搭建一個簡單的項目,來演示這種方法。
* 演示項目 *
項目名稱是blog02,項目目錄結(jié)構(gòu)如下:
blog02 │ ├─src │ ├─App.vue │ ├─home.vue │ ├─main.js │ ├─test.docx │ └─router.js │ ├─.babelrc ├─index.template.html ├─package.json └─webpack.config.js
App.vue
<template>
<div>
<router-view></router-view>
</div>
</template>
<script>
export default {
}
</script>
home.vue
<template>
<div class="home-wrapper">
<span class="my-style">這里是首頁</span>
<!-- 下載鏈接 -->
<a href="/test.docx" rel="external nofollow" rel="external nofollow" >下載</a>
<!-- 觸發(fā) url-loader,使得 url-loader 處理 word 文檔。 -->
<img v-show="isShow" src="./test.docx"/>
</div>
</template>
<script>
export default {
data(){
// 隱藏包含 word 文檔路徑的 img 標簽。
return {isShow:false};
}
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
.home-wrapper{
.my-style{
width:900px;
height:600px;
float:right;margin-right:200px;
padding-top:100px;
color:#FF0000;
}
}
</style>
main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import routes from './router'
import VueSuperagent from 'vue-superagent'
import 'babel-polyfill';
Vue.use(VueRouter);
Vue.use(VueSuperagent);
const router = new VueRouter({
mode: 'history',
routes
})
new Vue({
el: '#app',
router,
render: h => h(App)
})
router.js
import Home from './home.vue'
export default [{
path:'/',
component:Home
}]
.babelrc
{
"presets": [
["latest", {
"es2015": { "modules": false }
}]
]
}
index.template.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>blog02</title> </head> <body> <div id="app"> <router-view></router-view> </div> <!--<script src="/dist/[name].[chunkhash].js"></script>--> </body> </html>
package.json
{
"name": "blog02",
"description": "CSDN blog02",
"version": "1.0.0",
"author": "",
"private": true,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "rimraf dist && cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"babel-polyfill": "^6.23.0",
"vue": "^2.2.1",
"vue-router": "^2.3.0",
"vue-superagent": "^1.2.0"
},
"devDependencies": {
"babel-core": "^6.0.0",
"babel-loader": "^6.0.0",
"babel-preset-latest": "^6.0.0",
"cross-env": "^3.0.0",
"css-loader": "^0.25.0",
"file-loader": "^0.9.0",
"html-webpack-plugin": "^2.28.0",
"node-sass": "^4.5.0",
"rimraf": "^2.6.1",
"sass-loader": "^5.0.1",
"url-loader": "^0.5.8",
"vue-loader": "^11.1.4",
"vue-template-compiler": "^2.2.1",
"webpack": "^2.2.0",
"webpack-dev-server": "^2.2.0"
}
}
webpack.config.js
var path = require('path')
var webpack = require('webpack')
const HTMLPlugin = require('html-webpack-plugin')
module.exports = {
entry: {
app: ['./src/main.js'],
// 把共用的庫放到vendor.js里
vendor: [
'babel-polyfill',
'vue',
'vue-router',
'vuex'
]
},
output: {
path: path.resolve(__dirname, './dist'),
// 因為用到了 html-webpack-plugin 處理HTML文件。處理后的HTML文件都放到了
// dist文件夾里。html文件里面js的相對路徑應(yīng)該從使用 html-webpack-plugin 前
// 的'/dist/' 改成 '/'
publicPath: '/',
// publicPath: '/dist/',
filename: '[name].[hash].js'
// filename:'build.js'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': 'vue-style-loader!css-loader!sass-loader',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
// font loader
{
test: /\.(ttf|eot|woff|svg)$/i,
loader: 'url-loader'
},
// 圖片處理
{
test: /\.(png|jpg|gif)$/,
loader: 'url-loader',
options: {
limit: '1000',
name: '[name].[ext]?[hash]'
}
},
// 處理模擬下載文件
{
test: /\.(docx)$/,
loader: 'url-loader',
options: {
limit: '10',
name: '[name].[ext]'
}
}
// {
// test: /\.(png|jpg|gif|svg)$/,
// loader: 'file-loader',
// options: {
// name: '[name].[ext]?[hash]'
// }
// }
]
},
plugins:[
// 把共用的庫放到vendor.js里
new webpack.optimize.CommonsChunkPlugin({name: 'vendor'}),
// 編譯HTML。目的:在生產(chǎn)環(huán)境下,為了避免瀏覽器緩存,需要文件按照哈希值重命名。
// 這里編譯可以自動更改每次編譯后引用的js名稱。
new HTMLPlugin({template: 'index.template.html'})
],
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
devServer: {
historyApiFallback: true,
noInfo: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
以上這篇Vuejs2 + Webpack框架里,模擬下載的實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
原生JS?Intersection?Observer?API實現(xiàn)懶加載
這篇文章主要為大家介紹了原生JS?Intersection?Observer?API實現(xiàn)懶加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
Vue源碼學(xué)習(xí)之初始化模塊init.js解析
本篇文章主要介紹了Vue源碼學(xué)習(xí)之初始化模塊init.js解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Vant Uploader實現(xiàn)上傳一張或多張圖片組件
這篇文章主要為大家詳細介紹了Vant Uploader實現(xiàn)上傳一張或多張圖片組件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
el-checkbox-group?的v-model無法綁定對象數(shù)組的問題解決
elementUI官方文檔中el-checkbox-group組件綁定的都為一維數(shù)組,本文主要介紹了解決el-checkbox-group?的v-model無法綁定對象數(shù)組,感興趣的可以了解一下2023-05-05
vue router總結(jié) $router和$route及router與 router與route區(qū)別
這篇文章主要介紹了vue router總結(jié) $router和$route及router與 router與route區(qū)別,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-07-07
vue中for循環(huán)更改數(shù)據(jù)的實例代碼(數(shù)據(jù)變化但頁面數(shù)據(jù)未變)
這篇文章主要介紹了vue中for循環(huán)更改數(shù)據(jù)的實例代碼(數(shù)據(jù)變化但頁面數(shù)據(jù)未變)的相關(guān)資料,需要的朋友可以參考下2017-09-09

