singleSpa记录

本文档记录了singleSpa的安装过程以及如何配置single-spa-vue.config.js文件,同时介绍了在main.js中引入的步骤。
摘要由CSDN通过智能技术生成
  1. 安装single-spa
  2. 新建文件config,创建single-spa-vue.config.js
import singleSpaVue from "single-spa-vue"
import App from "../App" (相对路径)

export default (Vue,router,store,i18n)=>{
  let lifecycles=null
  const bootstrap=(props)=>{
 if(!lifecycles){
 lifecycles=singleSpaVue({
 Vue,
 appOptions:{
 el:"#"+props.targetEl+props.name
 render:(h)=>{
 return h(APP)
 },
 router,
 store,
 i18n
 }
 })
}
return lifecycles.bootstrap(props)
}
const mount=(props)=>{
if(!lifecycles){
return Promise.resolve()
}
if(props.targetEl){
  const targetEl=document.getElementById(props.targetEl)
  if(targetEl){
  const myElId=props.targetEl+props.name
  //多加一层,防止字应用节点卸载不完全
  const myElContainerId=myElId+'Container'
  let myElContainer=document.getElementById(myElContainerId)
  if(!myElContainer){
  myElContainer=document.createElement("div")
  //新建加载节点
  myElContainer.setAttribute("id",myElContainerId)
  myElContainer.setAttribute("class","flex-layout")
  targetEl.appendChild(myElContainer)
  }
  let myEl=document.getElementById(myElId)
  if(!myEl){
   myEl=document.createElement("div")
   //新建加载节点
   myEl.setAttribute("id",myElId)
   myEl.setAttribute("class","flex-layout")
   myElContainer.appendChild(myEl)
  }
}else{
conslole.log("子应用挂在的目标节点不存在!!ID:",props.targetEl)
}
}
for(const key in props.store){
 store.state[key]=props.store[key]
}
return lifecycles.mount(props)
}
const unmount=(props)=>{
 if(!lifycycles){
 return Promise.resolve()
}
return lifecycles.unmount(props).then(()=>{
 const targetEl=document.getElementById(props.targetEl)
 if(targetEl){
 const myElContainerId=props.targetEl+props.name+"Container"
 const myElContainer=document.getElementById(myElContainerId)
 if(myElContainer){
 targetEl.removeChild(myElContainer)
}
}
})
}

return{
bootstrap,
mount,
unmount
}
}

3.main.js中引入

import singleSpaConfig from "./config/single-spa-vue.config"
const vueLife=singleSpaConfig(Vue,router,store,i18n)
export const bootstrap=vueLife.bootStrap
export const mount=vueLife.mount
export const unmount=vueList.unmount
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值