micro-app vue2和3 入门使用方法

文章介绍了微前端概念,由ThoughtWorks在2016年提出,类似微服务架构,旨在拆分大型前端应用为独立的小应用,通过micro-app实现这些应用的融合和独立运行。文中详细展示了如何在Vue2和Vue3项目中配置主应用和子应用,包括安装micro-app、创建路由、嵌入子应用及处理公共路径等步骤。
摘要由CSDN通过智能技术生成

micro-app 简介

微前端的概念是由ThoughtWorks在2016年提出的,它借鉴了微服务的架构理念,核心在于将一个庞大的前端应用拆分成多个独立灵活的小型应用,每个应用都可以独立开发、独立运行、独立部署,再将这些小型应用融合为一个完整的应用,或者将原本运行已久、没有关联的几个应用融合为一个应用。微前端既可以将多个项目融合为一,又可以减少项目之间的耦合,提升项目扩展性,相比一整块的前端仓库,微前端架构下的前端仓库倾向于更小更灵活。

主应用配置

使用方法

创建vue2或3项目
vue create master-file

安装micro-app
npm i @micro-zoe/micro-app --save

进入main.js文件

// vue2 
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import VueRouter from "vue-router";
import microApp from "@micro-zoe/micro-app"; 
Vue.config.productionTip = false;
microApp.start();
new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");


// vue3 
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import * as echarts from 'echarts'
import microApp from "@micro-zoe/micro-app";
microApp.start();
const app = createApp(App)
app.config.globalProperties.$echarts = echarts
app.use(store).use(router).mount('#app')
分配一个路由给子应用(views新建my-page.vue)
// vue2

 import Vue from "vue";
import VueRouter from "vue-router";
import MyPage from "../views/my-page.vue";

Vue.use(VueRouter);

const routes = [
  {
    // 👇👇👇👇👇 非严格匹配,/my-page/* 都指向 MyPage 页面
    path: "/my-page/*",
    name: "my-page",
    component: MyPage,
  },
];

const router = new VueRouter({
  mode: "history",
  // 设置主应用基础路由为main-vue(用于后续部署),则子应用基础路由(baseroute)为/main-vue/xxx
  base: process.env.BASE_URL,
  routes,
});
export default router ;

// vue3

import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
      // 👇👇👇👇👇 非严格匹配,/my-page/* 都指向 MyPage 页面
    path: "/my-page/*", 
    name: "my-page",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/my-page.vue')
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  base: process.env.BASE_URL,
  routes
})

export default router

在my-page.vue上嵌入子应用
 // vue2和vue3都一样
<template>
  <div>
      <h1>子应用</h1>
       <!-- 
      name(必传):应用名称
      url(必传):应用地址,会被自动补全为http://localhost:3000/index.html
      baseroute(可选):基座应用分配给子应用的基础路由,就是上面的 `/my-page`
     -->
    <micro-app name='app1' url='http://localhost:3000/' baseroute='/my-page'></micro-app>
  </div>
</template>
主应用配置完毕!

子应用配置

创建 vue 子应用
vue create childs-vue

在src同级文件创建vue.config.js

// vue.config.js

module.exports = {
  devServer: {
    port: 3000,
    headers: {
      "Access-Control-Allow-Origin": "*",
    },
  },
};

在router文件夹 index.js

//vue2
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
Vue.use(VueRouter);
const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  // 👇👇👇👇👇 __MICRO_APP_BASE_ROUTE__ 为micro-app传入的基础路由
  base: window.__MICRO_APP_BASE_ROUTE__ || process.env.BASE_URL,
  routes,
});

 
export default router;


// vue3 

import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  base: window.__MICRO_APP_BASE_ROUTE__ || process.env.BASE_URL,
  routes
})
export default router
})

在src里新建文件public-path.js

//public-path.js
// __MICRO_APP_ENVIRONMENT__和__MICRO_APP_PUBLIC_PATH__是由micro-app注入的全局变量
if (window.__MICRO_APP_ENVIRONMENT__) {
  // eslint-disable-next-line
  __webpack_public_path__ = window.__MICRO_APP_PUBLIC_PATH__
}

把 public-path.js引入main.js

// vue2 就不写了 都一样的

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

// 引入public-pathk.js

import './public-pathk'

createApp(App).use(store).use(router).mount('#app')

卸载监听


// main.js

// vue2 
import "./public-path"; // 必须放在第一个要不然图片不显示
import Vue from "vue";
import App from "./App.vue";
import VueRouter from "vue-router";
import router from "./router";
Vue.config.productionTip = false;
const app = new Vue({
  router ,
  render: (h) => h(App),
}).$mount("#app");
 
// 监听卸载操作
window.addEventListener("unmount", function () {
  app.$destroy();
});

// vue3 

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './public-pathk'
createApp(App).use(store).use(router).mount('#app')
window.addEventListener("unmount", function () {
    App.$destroy();
})
这样就基本完成了 能在主应用里看到子应用的 东西

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值