加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Vuejs2 + Webpack框架里,模拟下载的实例讲解

发布时间:2020-12-16 23:38:38 所属栏目:百科 来源:网络整理
导读:在实际的开发工作中,难免要配合销售人员,提前做一些前端的 DEMO 出来。这个时候往往还没有连接后端 API。假如要演示一个下载连接,那么应该如何做呢? 我们希望能够达成以下两点: 1、在开发环境下,我们可以在 webpack-dev-server 开发服务器上点击下载连

在实际的开发工作中,难免要配合销售人员,提前做一些前端的 DEMO 出来。这个时候往往还没有连接后端 API。假如要演示一个下载连接,那么应该如何做呢?

我们希望能够达成以下两点:

1、在开发环境下,我们可以在 webpack-dev-server 开发服务器上点击下载连接,点击后浏览器就能不下载文件。

2、当演示的时候,代码编译后放到 nginx 中。用户可以点击下载链接。nginx存放的都是业务代码。

那么如何做到这两点呢?假如我们要模拟下载一个 test.docx 文件。我们可以利用 url-loader 来对 .docx 文件做处理。可能有人会问:“url-loader 一般不是处理 img 标签里面的图片文件路径吗?”为了解决这个 img 标签的问题,我们可以在一个页面中加上隐藏的图片标签。最后加一个 a 标签: 下载。下面的篇幅要帮助读者搭建一个简单的项目,来演示这种方法。

* 演示项目 *

项目名称是blog02,项目目录结构如下:

App.vue

home.vue

下载

<img v-show="isShow" src="./test.docx"/>

main.js

Vue.use(VueRouter);
Vue.use(VueSuperagent);

const router = new VueRouter({
mode: 'history',routes
})

new Vue({
el: '#app',router,render: h => h(App)
})

router.js

export default [{
path:'/',component:Home
}]

.babelrc

index.template.html

blog02

package.json

webpack.config.js

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)$/,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。目的:在生产环境下,为了避免浏览器缓存,需要文件按照哈希值重命名。
// 这里编译可以自动更改每次编译后引用的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框架里,模拟下载的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读