微前端wujie的使用与nginx部署整理

效果展示

微前端wujie的使用与nginx部署整理_第1张图片

 

废话不说直接上代码

官网入口

前提:主项目是vue2 子项目是vite+vue3

一、主项目搭建

1、创建vue2项目 vue create main-vue


# vue2 框架
npm i wujie-vue2 -S
# vue3 框架
npm i wujie-vue3 -S

2、安装wujie组件 npm i wujie-vue2 -S

//vue2路由只能用3.x vue3则是4.x
3、安装路由 yarn add [email protected]  

二、主项目主要代码

API详解

1)首先设置vue.config跨域

module.exports = {
  lintOnSave: false,
  publicPath: process.env.NODE_ENV === "production" ? "/demo-main-vue/" : "/",
  devServer: {
    headers: {
      "Access-Control-Allow-Origin": "*",
    },
    open: process.env.NODE_ENV === "development",
    port: "8100",
  },
  transpileDependencies: [ "sockjs-client" ],
};

2)在我们入口文件进行无界组件挂载

import WujieVue from "wujie-vue2";
Vue.use(WujieVue);

// 在 xxx-sub 路由下子应用将激活路由同步给主应用,主应用跳转对应路由高亮菜单栏
bus.$on("sub-route-change", (name, path) => {
  const mainName = `${name}-sub`;
  const mainPath = `/${name}-sub${path}`;
  const currentName = router.currentRoute.name;
  const currentPath = router.currentRoute.path;

  if (mainName === currentName && mainPath !== currentPath) {
    console.log(mainPath,'5555555');
    router.push({ path: mainPath });
  }
});

setupApp({
  name: "vite",
  url: hostMap("//localhost:7500/"),
  attrs,
  exec: true,
  props,
  fetch: credentialsFetch,
  degrade,
  ...lifecycles,
});

3)、在界面使用wujie 进行子应用加载



 computed:{
     viteUrl(){
       return hostMap("//localhost:7500/") + `#/${this.$route.params.path}`;
     }
  },

 propsMethod(){
      alert("触发了主程序的弹窗");
    }

子应用

1)、main挂载

declare global {
  interface Window {
    // 是否存在无界
    $wujie: any;
    __POWERED_BY_WUJIE__?: boolean;
    // 子应用mount函数
    __WUJIE_MOUNT: () => void;
    // 子应用unmount函数
    __WUJIE_UNMOUNT: () => void;
    // 子应用无界实例
    __WUJIE: { mount: () => void };
  }
}

if (window.__POWERED_BY_WUJIE__) {
  let instance: any;
  window.__WUJIE_MOUNT = () => {
    const router = createRouter({ history: createWebHistory(basename), routes });
    instance = createApp(App)
      .use(router);
    instance.mount("#app");
  };
  window.__WUJIE_UNMOUNT = () => {
    instance.unmount();
  };
  // module脚本异步加载,应用主动调用生命周期
  window.__WUJIE.mount();
} else {
  createApp(App)
    .use(createRouter({ history: createWebHistory(basename), routes }))
    .mount("#app");
}

2)app.vue







nginx部署

#wujie 配置
server {
        listen 80;
        server_name www.wujie.com wujie.com;
        index index.jsp index.html index.htm;
        root /data/webapps/wujie;
        #iframe跨域问题 配置
 #       proxy_set_header Upgrade $http_upgrade;
  #      proxy_set_header Connection "Upgrade";
   #     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #    proxy_set_header X-Real-IP $remote_addr;
      #  proxy_hide_header X-Frame-Options;
      #  add_header X-Frame-Options ALLOWALL;
       # add_header Access-Control-Allow-Origin *; # 必须要有
      #  add_header Access-Control-Allow-Headers *; # 必须要有
      #  proxy_cookie_domain remote localhost;  # 重要 对应upstream remote #注意别写错位置了 proxy_cookie_path //;
       # 主应用基座
        location / {
                index index.html index.htm;
                try_files $uri $uri/ /index.html last;
                if (!-e $request_filename) {
                    proxy_pass http://192.168.20.32;
                       }
                 }
        location /demo-main-vue {
                alias   /data/webapps/wujie/;
        #       if ($uri ~* "b1000") {
                    #访问/product/b1000,重写为/p/1000
        #           rewrite ^/(.*)/(.*)$ /p/1000 permanent;
        #           break;
        #      }
                index index.html index.htm;
        }
        #微应用基座
        location /demo-vite {
                alias /data/webapps/wujie/childer/;
                index index.html index.htm;
        #       add_header Access-Control-Allow-Origin *;
         #       add_header Access-Control-Allow-Methods *;
          #      add_header Access-Control-Allow-Headers *;
                try_files $uri $uri/ /index.html last;

        }
               access_log  logs/jvm_web/jvm_wujie/access.log  main;
               error_log   logs/jvm_web/jvm_wujie/error.log  crit;

 }

注意⚠️:

这个地址一定要与自己服务器匹配这个才是部署的关键 

微前端wujie的使用与nginx部署整理_第2张图片

希望能帮到正在遇到部署问题的小伙伴 

附:代码demo

你可能感兴趣的:(微前端wujie,个人学习笔记,vue.js,javascript,前端)