Search Docs
本节我们将详细介绍 vue 框架的应用作为子应用的接入步骤。v2 demo、v3 demo
npm install @garfish/bridge-vue-v2 --save
更多 bridge 函数参数介绍请参考 这里
import Vue from 'vue'; import VueRouter from 'vue-router'; import store from './store'; import App from './App.vue'; import Home from './components/Home.vue'; import { vueBridge } from '@garfish/bridge-vue-v2'; Vue.use(VueRouter); Vue.config.productionTip = false; function newRouter(basename) { const router = new VueRouter({ mode: 'history', base: basename, routes: [ { path: '/home', component: Home }, ], }); return router; } export const provider = vueBridge({ // 根组件 rootComponent: App, // 可选,注册 vue-router或状态管理对象 appOptions: ({ basename, dom, appName, props, appInfo }) => { // pass the options to Vue Constructor. check https://vuejs.bootcss.com/api/#%E9%80%89%E9%A1%B9-%E6%95%B0%E6%8D%AE return { el: '#app', router: newRouter(basename), store, }; }, });
import { h, createApp } from 'vue'; import { createRouter, createWebHistory } from 'vue-router'; import { stateSymbol, createState } from './store.js'; import App from './App.vue'; import Home from './components/Home.vue'; import { vueBridge } from '@garfish/bridge-vue-v3'; const routes = [ { path: '/home', component: Home }, ]; function newRouter(basename) { const router = createRouter({ history: createWebHistory(basename), routes, }); return router; } export const provider = vueBridge({ rootComponent: App, // 可选,注册 vue-router或状态管理对象 handleInstance: (vueInstance, { basename, dom, appName, props, appInfo}) => { vueInstance.use(newRouter(basename)); vueInstance.provide(stateSymbol, createState()); }, });
import Vue from 'vue'; import VueRouter from 'vue-router'; import store from './store'; import App from './App.vue'; import Home from './components/Home.vue'; import { vueBridge } from '@garfish/bridge-vue-v2'; Vue.use(VueRouter); Vue.config.productionTip = false; function newRouter(basename) { const router = new VueRouter({ mode: 'history', base: basename, routes: [ { path: '/home', component: Home }, ], }); return router; }
// webpack.config.js const webpack = require('webpack'); const isDevelopment = process.env.NODE_ENV !== 'production'; module.exports = { output: { // 开发环境设置 true 将会导致热更新失效 clean: isDevelopment ? false : true, filename: '[name].[contenthash].js', chunkFilename: '[name].[contenthash].js', // 需要配置成 umd 规范 libraryTarget: 'umd', // 修改不规范的代码格式,避免逃逸沙箱 globalObject: 'window', // webpack5 使用 chunkLoadingGlobal 代替,或不填保证 package.json name 唯一即可 jsonpFunction: 'garfish-demo-react', // 保证子应用的资源路径变为绝对路径 publicPath: 'http://localhost:8080', }, plugin: [ // 保证错误堆栈信息及 sourcemap 行列信息正确 new webpack.BannerPlugin({ banner: 'Micro front-end', }), ], devServer: { // 保证在开发模式下应用端口不一样 port: '8000', headers: { // 保证子应用的资源支持跨域,在上线后需要保证子应用的资源在主应用的环境中加载不会存在跨域问题(**也需要限制范围注意安全问题**) 'Access-Control-Allow-Origin': '*', }, }, };
【重要】注意:
last but not least, 别忘了添加子应用独立运行逻辑,这能够让你的子应用脱离主应用独立运行,便于后续开发和部署。
// src/main.js import Vue from 'vue'; import VueRouter from 'vue-router'; // 这能够让子应用独立运行起来,以保证后续子应用能脱离主应用独立运行,方便调试、开发 if (!window.__GARFISH__) { const router = new VueRouter({ mode: 'history', base: '/', routes: [ { path: '/home', component: Home }, ], }); new Vue({ store, router, render: (h) => h(App), }).$mount('#app'); }