🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more
Socket
Book a DemoInstallSign in
Socket

@ks-dilu/vue

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ks-dilu/vue

的卢微前端Vue主应用SDK

latest
Source
npmnpm
Version
0.0.2
Version published
Maintainers
2
Created
Source

@ks-dilu/vue

的卢微前端 React 版本的主应用组件化 SDK,分别从路由级 组件级子应用视角出发,介绍如何通过 SDK 来将应用接入微前端框架

安装依赖

Vue3

$ npm install -S @ks-dilu/vue

如果业务是 Vue2.7 版本的,需要安装 vue-demi; 2.7 以下的还需要安装 @vue/composition-api;

Vue2

$ npm install -S vue-demi @vue/composition-api @ks-dilu/vue

路由级 DLRouter

Vue3


import { DLRouter } from '@ks-dilu/vue';


const micros = [{
  activeRule: '/zone';
  entry: 'https://xxx.cdn.com/xx',
  name: 'test1',
},{
  activeRule: '/zone';
  entry: 'https://xxx.cdn.com/xx',
  name: 'test1'
}]

const App = () => {
  return (
    <DLRouter
      fetchMicros={async () => {
        return micros;
      }}
    ></DLRouter>
  );
};

Vue2

<template>
  <div class="home">
    <DLRouter :fetchMicros="fetchMicros">
      <template v-slot:default="slotProps">
        <div>
          {{ slotProps.state }}
        </div>
      </template>
    </DLRouter>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import { DLRouter, type DLRouterProps } from '@ks-dilu/vue/v2';
import { RouteMicros } from './micros';

export default Vue.extend({
  name: 'DLContent',

  methods: {
    fetchMicros() {
      return Promise.resolve(RouteMicros);
    },
  },
  components: { DLRouter },
});
</script>

通过上面的 Demo 可以非常方便的完成主应用的接入微前端框架, 并且路由劫持,根据激活规则展现子应用;

Props

属性说明如下

属性说明类型默认值必填
extra会透传给子应用,配合@ks-dilu/react-micro 可以实现子应用数据通信object
loader状态发生变化时会调用的方法function
defaultMountAppLink设置主应用启动后默认进入的微应用string
containerId容器 Idstring否,配合 isOccupyOuterContainer = true 时,需要指定,并且该 ID 的 Dom 要存在
isOccupyOuterContainer是否使用外部定义的容器 DOM 节点stringfalse
collect日志收集函数CollectLogFn(type, opts) => void
activeFilter子应用激活过滤器,必须是同步纯函数ActivityFn
onBeforeLoad子应用开始加载前触发function
onBeforeMount子应用开始挂载前触发function
onAfterMount子应用挂载完成触发function
onBeforeUnmount子应用开始卸载销毁前触发function
onAfterUnmount子应用卸载销毁触发function
onFirstMount第一个子应用 mount 调用function
onError添加全局的未捕获异常处理器function
advancedqiankun 的高级配置Advanced
fetchMicros获取要注册的子应用async () => MicroInfo[]

组件级 DLWidget

Vue3

import { createRoot } from 'react/client';
import { DLRouter } from '@ks-dilu/react';

const App = () => {
  return <DLWidget entry="https://xxx.cdn.com/xx" name="test1"></DLWidget>;
};

const container = createRoot(document.getElementById('main_root_id'));

container.render(<App />);

Vue2

<template>
  <div class="wrap">
    <DLWidget ref="DLWidgetInstance" :extra="extra" :name="name" :entry="entry"></DLWidget>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import { DLWidget } from '@ks-dilu/vue/v2';

export default Vue.extend({
  components: {
    DLWidget,
  },
  props: {
    name: String,
    entry: String,
  },
  data() {
    return {
      extra: {
        title: '开始了',
        type: 'widget',
      },
    };
  },
});
</script>
<style lang="less" scoped>
.wrap {
  width: 400px;
}
</style>

通过上面的 Demo,可以手动加载指定的子应用;

Props

属性说明如下

属性说明类型默认值必填
extra会透传给子应用,配合@ks-dilu/react-micro 可以实现子应用数据通信object
loader状态发生变化时会调用的方法function
collect日志收集函数CollectLogFn(type, opts) => void
onBeforeLoad子应用开始加载前触发function
onBeforeMount子应用开始挂载前触发function
onAfterMount子应用挂载完成触发function
onBeforeUnmount子应用开始卸载销毁前触发function
onAfterUnmount子应用卸载销毁触发function
onError添加全局的未捕获异常处理器function
namestring唯一的标识
entryEntry唯一的标识
advancedqiankun 的高级配置Advanced

Props 中类型说明

Advanced 说明

属性说明类型默认值必填
prefetch是否开启预加载, 仅仅用于路由级子应用时生效booleanfalse
fetch自定义的 fetch 方法function-
sandbox是否开启沙箱boolean 或 {strictStyleIsolation?: boolean, experimentalStyleIsolation?: boolean}true
singular是否为单实例场景,单实例指的是同一时间只会渲染一个微应用booleantrue
excludeAssetFilter指定部分特殊的动态加载的微应用资源(css/js) 不被 qiankun 劫持处理function
getPublicPath参考 qiankunfunction
getTemplate通过自己实现的 getTemplate 方法过滤微应用 HTML 模板中的异常脚本function
// 参考乾坤文档
export interface Advanced {
  /**
   * 是否开启沙箱,默认为 true
   */
  sandbox?:
    | boolean
    | {
        strictStyleIsolation?: boolean;
        experimentalStyleIsolation?: boolean;
      };
  singular?: boolean | ((app: RegistrableApp<any>) => Promise<boolean>);
  excludeAssetFilter?: (url: string) => boolean;
  getPublicPath?: (entry: string) => string;
  getTemplate?: (tpl: string) => string;
  fetch?: (url: RequestInfo | URL, init?: RequestInit) => Promise<any>;
  /**
   * 仅仅用于路由级子应用时生效
   */
  prefetch?: string[];
}

export interface MicroInfo {
  activeRule: string;
  entry: Entry;
  extra?: Record<string, any>;
  name: string;
  type?: MicroType;
}

export enum MicroType {
  Route = 'route',
  Component = 'component',
}

export type CollectLogFn = (type: CollectType, options: Record<string, any>) => void;

FAQs

Package last updated on 24 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts