vue spa php,基于single-spa的vue微前端框架搭建

本文详细介绍了如何搭建和整合一个微前端框架,包括创建主应用和子应用,修改主应用的index.html以引入所需库,配置子应用的入口文件和webpack设置,以及解决跨域问题。最后通过运行项目展示了主应用加载子应用的效果。
摘要由CSDN通过智能技术生成

一、一套微前端框架通常包含四个部分:

3.主应用

4.子应用

这里重点介绍一下,主应用,子应用的搭建过程,首先通过vue-cli创建2个vue应用,vue_main主应用,vue_child子应用

二、修改主应用vue_main修改index.html,在head部分添加以下代码:

{

"imports": {

"child": "http://localhost:8081/app.js", // 确保文件浏览器中可以打开

"single-spa": "https://cdnjs.cloudflare.com/ajax/libs/single-spa/4.3.7/system/single-spa.min.js",

"vue": "https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js",

"vue-router": "https://cdn.jsdelivr.net/npm/vue-router@3.0.7/dist/vue-router.min.js"

}

}

2.修改app.html

   logo.png

export default {

name: 'App',

mounted () {

window.System.import('single-spa').then((res) => {

var singleSpa = res

singleSpa.registerApplication('child', () => window.System.import('child'), location => true)

singleSpa.start()

})

}

}

#app {

font-family: 'Avenir', Helvetica, Arial, sans-serif;

-webkit-font-smoothing: antialiased;

-moz-osx-font-smoothing: grayscale;

text-align: center;

color: #2c3e50;

margin-top: 60px;

}

这样主应用就修改好了

三、修改子应用vue_child

1.安装项目依赖npm i single-spa-vue systemjs-webpack-interop

2.修改入口文件main.js// The Vue build version to load with the `import` command

// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import { setPublicPath } from 'systemjs-webpack-interop'

import Vue from 'vue'

import App from './App'

import singleSpaVue from 'single-spa-vue'

import router from './router'

setPublicPath('child')

Vue.config.productionTip = false

/* eslint-disable no-new */

// new Vue({

//   el: '#app',

//   router,

//   components: { App },

//   template: ''

// })

const vueLifecycles = singleSpaVue({

Vue,

appOptions: {

render: (h) => h(App),

router

}

})

export const bootstrap = vueLifecycles.bootstrap

export const mount = vueLifecycles.mount

export const unmount = vueLifecycles.unmount

3.修改webpack配置文件webpack.base.conf.jsoutput: {

path: config.build.assetsRoot,

filename: '[name].js',

library:'child',  // 新添加

libraryTarget: 'umd',  // 新添加

publicPath: process.env.NODE_ENV === 'production'

? config.build.assetsPublicPath

: config.dev.assetsPublicPath

},

4.修改webpack配置文件webpack.dev.conf.js,确保子应用允许跨域devServer: {

headers:{

"Access-Control-Allow-Origin":"*"

}

}

四.运行项目查看效果,这样在主应用中加载了子应用的内容

1604369787775988.png

五、通过脚手架生成子应用npm init single-spa

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值