溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Vuejs2 + Webpack框架里,模擬下載的實例講解

發(fā)布時間:2020-10-02 12:49:33 來源:腳本之家 閱讀:159 作者:zhangchao19890805 欄目:web開發(fā)

在實際的開發(fā)工作中,難免要配合銷售人員,提前做一些前端的 DEMO 出來。這個時候往往還沒有連接后端 API。假如要演示一個下載連接,那么應該如何做呢?

我們希望能夠達成以下兩點:

1、在開發(fā)環(huán)境下,我們可以在 webpack-dev-server 開發(fā)服務器上點擊下載連接,點擊后瀏覽器就能不下載文件。

2、當演示的時候,代碼編譯后放到 nginx 中。用戶可以點擊下載鏈接。nginx存放的都是業(yè)務代碼。

那么如何做到這兩點呢?假如我們要模擬下載一個 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的相對路徑應該從使用 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)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI