组织介绍

VOYOM Micro 微前端框架

导航

核心模块

  • @ams-micro/main

    核心framework,用户校验,导航,整体UI框架,css变量控制。

  • @ams-micro/common

    公共配置,全局对象存放模块

功能模块

  • @ams-micro/service-*

    负责各功能开发,不同功能模块应当分离为不同包。

@ams-micro/main

服务配置

voyom.env.js

module.exports = {
  dev: { //开发环境
    test_service:
      "testService@http://localhost:8601/service-two/testService.js",
  },
  trial: { //测试环境 
    test_service:
      "testService@http://localhost:8601/service-two/testService.js",
  },
  prod: { //生产环境
    test_service:
      "testService@http://localhost:8601/service-two/testService.js",
  },
};

该文件配置了test_service服务,的入口JS地址。
生产|测试地址根据实际情况变更。

引用格式为[服务名]@[服务入口文件绝对地址]

服务注册

src/router-main.ts

export const routerMain=[
  {
    path: "module-two",
    //将test_service注册入 module-two目录下
    module: () => import("test_service/testService"),
  },
]

引用格式为import([定义的服务别名]/[服务输出的模块名])

这样在module-two/路由下的页面便由test_service托管。

关于@voyom/vue-router:

@ams-micro/service-*

定义服务

webpack.config.js

 new ModuleFederationPlugin({
        name:`${configVal.serviceName}`,
        filename:`${configVal.serviceName}.js`,
        remotes:{},
        /**
         * 输出当前module ==> /dist/testService.js
         */
        exposes:{
          [`./${configVal.serviceName}`] : join(configVal.entry)
        },
        /**
         * 提取共享包
         */
        shared: {
          vue: { 
            requiredVersion: deps["vue"]
          },
          "@ztwx/utils": {
            requiredVersion: deps["@ztwx/utils"] 
          },
          vuex: { requiredVersion: depsNative["vuex"] },
          "vue-router": {
            requiredVersion: deps["vue-router"],
          },
          "@voyo/http": {
            requiredVersion: depsNative["@voyo/http"],
          },
          "@ams-micro/common": {
            requiredVersion: "0.0.1",
          },
          "vue-property-decorator": {
            requiredVersion: deps["vue-property-decorator"],
          },
        },
      })

已配置的共享模块,在服务包运行|打包时,均不会被引入。

程序执行时,会从main中获取共享模块代码。

configVal 的配置参数会从voyom.env.js里获取

打包配置文件

voyom.env.js

module.exports={
  dev:{
    entry: "src/main.ts",
    baseUrl:"http://localhost:8601/service-two/",
    serviceName: "testService",
  },
  trial:{
    entry: "src/main.ts",
    baseUrl:"http://localhost:3000/service-two/",
    serviceName: "testService",
  },
  prod:{
    entry: "src/main.ts",
    baseUrl:"http://localhost:8601/service-two/",
    serviceName: "testService",
  }
}

服务注册(路由注册)

src/main.ts

import {VoyoRouterChild} from "@voyom/vue-router";
const child= new VoyoRouterChild({
  name:"test-service",
  routes:[
    {
      path:"one-page",component:()=>import("./pages/one-page/one-page.vue")
    },
    {
      path:"two-page",component:()=>import("./pages/two-page/two-page.vue")
    }
  ]
});
export default child;

服务原文件应当输出一个default VoyoRouterChild实例。

注册成功后,会自动addRoute,服务中定义的路由。

dev开发

  • 启动 @ams-micro/main
  • 启动 子服务
  • 在mainhttp://localhost:8600中访问子服务页面

BaseUrl 注意

子服务的baseUrl,务必配置为完整url,无论开发,生产。

Http请求

基本请求样例:

import {global} from "@ams-micro/common";
const http=global.http;
http.xhr({
  method: "post",
  path: "/queryById",
  json: {
    "id":1
  }
})
  .toPromise()
  .then(result=>{})

关于 @voyo/http

成就
0
Star
0
Fork
成员(2)
1415844 dream shao 1596693130
我在大运河边
lousc

搜索帮助