运行中动态加载远程vue文件,解析template、script、style并注册全局组件:

(1).通过http请求获取组件数据,然后使用http-vue-loader插件解析script(解析为各生命周期钩子函数和methods对象),然后动态注册为vueComponent使用。 通过:is 来动态切换组件。

必须使用全量vue库,因为需要Vue来解析template模板为render函数。

vue-cli中默认使用的时运行时vue库,需要配置vue.config.js加入vue运行时编译模块:

module.exports = { runtimeCompiler: true, }

复制代码
import httpVueLoader from "http-vue-loader";
const mTpl = await DictionaryInfo(filter);//axios调用
 // 调用接口获取动态组件数据:{ template:  "<div class=\"main\"> xxx </div> ", script:  "module.exports = {data() {}, mounted() {}, methods: {} }", style: ".main { width: 100% }"
If(!mTpl?.script) return;
loadStyleString(mTpl.style);//读取配置中的css,加入到页面
const scriptStr= `<script>${mTpl.script}<\/script>`;
//解析script字符串
httpVueLoader("data:text/plain," + encodeURIComponent(scriptStr))().then(compiler => {
  Vue.component(mTpl.name, { mixins: [compBase], template: mTpl.template, ...compiler });
});
function loadStyleString(cssText) {//Chrome 112:CSS 支持嵌套语法,可以直接写嵌套css了
  var style = document.createElement("style");
  try {  style.appendChild(document.createTextNode(cssText));  } catch (ex) {
    // IE早期的浏览器 ,需要使用style元素的stylesheet属性的cssText属性
    style.styleSheet.cssText = cssText;
  }
  document.getElementsByTagName("head")[0].appendChild(style);
}

(2).也可以****使用vue2-sfc-loader来解析template script(template解析为render函数,script解析为各生命周期钩子函数和methods对象)****后再注册为VueComponent。

可以只使用运行时vue库,因为template已由vue2-sfc-loader解析了,可不用全量vue库。

复制代码
const tpl = await DictionaryInfo(filter);//axios调用
 // 调用接口获取动态组件数据:{ template:  "<div class=\"main\"> xxx </div> ", script:  "module.exports = {data() {}, mounted() {}, methods: {} }", style: ".main { width: 100% }"
const options = {
    moduleCache: {  vue: Vue },
    getFile(url) {
        return `${tpl.template}<script>${tpl.script}<\/script><style>${tpl.style}<\/style>`;
    },
    addStyle(textContent) {//Chrome 112:CSS 支持嵌套语法,可以直接写嵌套css了
        const style = document.createElement("style");
        try {   style.appendChild(document.createTextNode(textContent));  } catch (ex) {
        // IE早期的浏览器 ,需要使用style元素的stylesheet属性的cssText属性
            style.styleSheet.cssText = textContent;
        }
        document.getElementsByTagName("head")[0].appendChild(style);
    }
}
const pl = await loadModule('/xxx.vue', options);//解析后返回render函数、生后周期钩子函数、methods组成的对象
if (pl)  Vue.component(tpl.name, { mixins: [mixins], ...pl })
相关推荐
小宋102125 分钟前
知识库里的 Excel 不能当纯文本读:表头行识别、合并单元格回填与公式缓存值的结构化入库
前端·人工智能·缓存·excel
map_3d_vis28 分钟前
百度地图折线 Polyline:颜色、虚线、方向箭头和纹理(JSAPI 4.0)
前端·javascript·地图·百度地图
智购科技自动贩卖机36 分钟前
自动售货机SaaS多租户数据库实战:分库分表与租户隔离~YH
android·java·linux·开发语言·javascript·网络
qq_363066931 小时前
vite qiankun子系统css分包解决方案
前端·css·qiankun
叼烟扛炮1 小时前
[LangGraph] 学习笔记(050):自纠正 RAG,检索之后先问一句「这材料用得上吗」
前端·笔记·学习
鲁邦通物联网1 小时前
机器人梯控跨项目部署中的楼层映射建模与配置校验方法
运维·前端·机器人·机器人梯控·agv梯控·非侵入式采集·机器人乘梯
RuoyiOffice2 小时前
Vue3商城后台实战:一款商品,从规格上架走到部分退款
vue.js·spring boot·vue3·商城系统·商城·企业管理·ruoyi office
星栈13 小时前
我在浏览器里试了试 PhotoCraft|Rust + WASM 修图,是玩具还是生产力?
前端
雨中飘荡的记忆14 小时前
淘宝搜不到的东西,能不能去全世界找?一个程序员的创业尝试
前端·设计
kyriewen15 小时前
我让 4 位 AI 面试官打分,同一个我差了 40 分
前端·面试·程序员