vue2子应用接入微前端
vue2基于webpack4进行配置
- 修改vue.config.js配置
- 输出模式修改为库模式
- 将访问目录指向dist目录
- 修改跨域请求,允许主应用访问子应用的资源 ```javascript const path = require(‘path’); const { name } = require(‘./package’);
function resolve(dir) { return path.join(__dirname, dir); }
const port = 9004;
module.exports = {
outputDir: ‘dist’,
assetsDir: ‘static’,
filenameHashing: true,
publicPath: http://localhost:${port}
,
devServer: {
contentBase: path.join(_dirname, ‘dist’), // 开发环境下访问的目录为dist
hot: true,
disableHostCheck: true,
port,
headers: {
‘Access-Control-Allow-Origin’: ‘*’, // 允许跨域请求,主应用访问子应用的资源
},
},
// 自定义webpack配置
configureWebpack: {
resolve: {
alias: {
‘@’: resolve(‘src’),
},
},
output: {
libraryTarget: ‘umd’, // 把子应用打包成 umd 库格式
filename: ‘vue2.js’, // 输出文件名称
library: ‘vue2’, // 在全局环境下访问的内容 window.vue2
jsonpFunction: `webpackJsonp${name}`,
},
},
};
- 修改main.js
- 添加微前端标识和生命周期
- 声明render函数来创建vue实例
- 区分不是微前端和是微前端情况下,运行整个项目
```javascript
import Vue from 'vue'
import App from './App.vue'
import router from './router';
import store from './store';
Vue.config.productionTip = false
let instance = null;
const render = () => {
instance = new Vue({
router,
store,
render: h => h(App)
}).$mount('#app-vue')
}
// 微前端环境标识,在不是微前端情况下也可以正常运行
if (!window.__MICRO_WEB__) {
render()
}
// 开始加载
export async function bootstrap() {
console.log('vue2 开始加载');
}
// 加载成功
export async function mount() {
console.log('渲染成功');
render()
}
console.log(instance);
// 卸载
export async function unmount(ctx) {
console.log('卸载');
instance = null;
const { container } = ctx
if (container) {
document.querySelector(container).innerHTML = ''
}
}
vue3子应用接入微前端
- vue3的vue.config.js的改动和vue2是一样的,这里就不写了
- vue3的main.js改动 ```javascript import { createApp } from ‘vue’; import App from ‘./App.vue’; import router from ‘./router’; import { setMain } from ‘./utils/global’
let instance = null;
function render() { instance = createApp(App); instance .use(router) .mount(‘#app’); }
if (!window.MICRO_WEB) { render(); } export async function bootstrap() { console.log(‘vue3.0 app bootstrap’); }
export async function mount(app) { setMain(app) render(); }
export async function unmount(ctx) { instance.unmount(); instance = null; const { container } = ctx if (container) { document.querySelector(container).innerHTML = ‘’ }
<a name="vnTb6"></a>
# react15子应用接入微前端
- 修改webpack.confg.js
```javascript
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
entry: {
path: ['./index.js']
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'react15.js',
library: 'react15',
libraryTarget: 'umd',
umdNamedDefine: true,
publicPath: 'http://localhost:9002/'
},
module: {
rules: [
{
test: /\.js(|x)$/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
},
{
test: /\.(c|sc)ss$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
},
{
test: /\.(png|svg|jpg|gif)$/,
use: {
loader: 'url-loader',
}
}
]
},
optimization: {
splitChunks: false,
minimize: false
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].css'
})
],
devServer: {
headers: { 'Access-Control-Allow-Origin': '*' },
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9002,
historyApiFallback: true,
hot: true,
}
}
- 修改index.js ```javascript import React from ‘react’ import ReactDOM from ‘react-dom’ import BasicMap from ‘./src/router/index.jsx’; import “./index.scss” import { setMain } from ‘./src/utils/global’
const render = () => {
ReactDOM.render((
if (!window.MICRO_WEB) { render() }
export async function bootstrap() { console.log(‘react bootstrap’) }
export async function mount(app) { setMain(app) // 记录主应用传过来的方法 console.log(‘react mount’) render() }
export async function unmount(ctx) { console.log(‘react unmout’) const { container } = ctx if (container) { document.querySelector(container).innerHTML = ‘’ } } ```
react16子应用接入微前端
- react16和react15修改方式一样,这里就不写了。