import Axios from 'axios';
export default {
name: 'SyncComponent',
props: {
// 父组件提供请求地址
url: {
type: String,
default: ''
}
},
data() {
return {
resData: ''
};
},
async mounted() {
if (!this.url) return;
const res = await Axios.get(this.url); // 我们在组件挂载完成时,请求远端代码并存储结果。
this.resData = res.data;
}
};
// 在 build 目录下新建 webpack.sync-components.prod.conf.js 文件
const webpack = require('webpack');
const path = require('path');
const utils = require('./utils');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
function resolve(dir) {
return path.join(__dirname, '..', dir)
}
module.exports = {
// 此处引入要打包的组件
entry: {
componentA: resolve('/src/views/component-a.vue')
},
// 输出到静态目录下
output: {
path: resolve('/static/'),
filename: '[name].js',
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
esModule: false, // ****** vue-loader v13 更新 默认值为 true v12及之前版本为 false, 此项配置影响 vue 自身异步组件写法以及 webpack 打包结果
loaders: utils.cssLoaders({
sourceMap: true,
extract: false // css 不做提取
}),
transformToRequire: {
video: 'src',
source: 'src',
img: 'src',
image: 'xlink:href'
}
}
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
}),
// 压缩JS
new webpack.optimize.UglifyJsPlugin({
compress: false,
sourceMap: true
}),
// 压缩CSS 注意不做提取
new OptimizeCSSPlugin({
cssProcessorOptions: {
safe: true
}
})
]
};
new Function。
async mounted() {
if (!this.url) return;
const res = await Axios.get(this.url);
let Fn = Function;
this.mode = new Fn(`return ${res.data}`)();
}
// vue-loader v13 esModule 更新 默认值为 true, v12及之前版本为 false, 此项配置影响 vue 自身异步组件写法以及 webpack 打包结果
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
esModule: false
... 以下省略千军万码
}
}
// UglifyJs 需要取消变量名替换配置,此配置并不会极大影响压缩率
new webpack.optimize.UglifyJsPlugin({
compress: false,
sourceMap: true
})
// 修改 sync-component.vue 组件
// 新增 v-bind="$attrs" v-on="$listeners"
<component
:is="mode"
v-bind="$attrs"
v-on="$listeners">
</component>
// inheritAttrs: true
export default {
name: 'SyncComponent',
props: {
// 父组件提供请求地址
url: {
type: String,
default: ''
}
},
inheritAttrs: true
... 以下省略千军万码
}
// 全局添加 axios 对象 import axios from 'axios'; Vue.prototype.$http = axios;
// webpack.sync-components.prod.conf.js 添加
externals: {
vue: 'vue',
'element-ui': 'element-ui',
axios: 'axios'
}
async mounted() {
if (!this.url) return;
// Cache 缓存 根据 url 参数
if (!window.SyncComponentCache) {
window.SyncComponentCache = {};
}
let res;
if (!window.SyncComponentCache[this.url]) {
window.SyncComponentCache[this.url] = Axios.get(this.url);
res = await window.SyncComponentCache[this.url];
} else {
res = await window.SyncComponentCache[this.url];
}
let Fn = Function;
this.mode = new Fn(`return ${res.data}`)();
console.log(this.mode);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有