首页 > 其他分享 >vue模板编译器

vue模板编译器

时间:2024-06-19 16:55:39浏览次数:12  
标签:vue ast label 编译器 code result attrsMap type 模板

vue模板编译器



import {ASTElement, compile} from 'vue-template-compiler';
import {TableColumnDesc} from "@/components/table/base-table/type.ts";


export const codeAdapter = function (code: string): {
  tableCode: string;
  columns: TableColumnDesc[];
} {
  const result:{
    tableCode: string;
    columns: TableColumnDesc[];
  } = {
    tableCode: "",
    columns: [],
  };
  const {ast} = compile(code);
  console.log(ast);
  // table
  result.tableCode = tableCode(ast);

  result.columns = columnCode(ast);
  return result;
};

function tableCode(ast: ASTElement) {

  let code = `<BaseTable
  `;
  const attrsMap = ast.attrsMap;
  const attrs = ["ref", ":data", "v-loading", "rowKey", ":summary-method", ":summaryMethod", "@current-change", "@currentChange",];
  attrs.forEach(attr => {
    if (attrsMap[attr]) {
      code += ` ${attr}="${attrsMap[attr]}"
`;
    }
  });

  const boolAttrs = [
    ":highlight-current-row", ":highlightCurrentRow", "highlight-current-row", "highlightCurrentRow",
    ":show-summary", ":showSummary", "show-summary", "showSummary",
    "border", ":border",
    "stripe", ":stripe",
  ];
  boolAttrs.forEach(attr => {

    if (Object.hasOwn(attrsMap, attr)) {
      code += `${attr}
`;
    }

  });
  code += "/>";
  return code;
}

function columnCode(ast: ASTElement): TableColumnDesc[] {
  return ast.children.filter(i => i.tag == 'el-table-column' || i.tag == 'jh-table-column').map(item => {
    const attrsMap: Record<string, string> = item.attrsMap;

    let {
      label,
      type,
      width,
      prop,
      fixed,
      align,
    } = attrsMap;

    if (type === "index") {
      return {
        type: "index",
        label: label || "序号",
      }
    }
    if (type == "selection") {
      return {
        type: "index",
        label: label || "序号",
      }
    }
    const result: TableColumnDesc = {};
    if (type) {
      result.type = type;
    }
    if (label) {
      result.label = label;
    }
    if (fixed) {
      result.fixed = fixed;
    }
    if (typeof prop === "string") {
      result.name = prop;
    }
    else {
      if (attrsMap[":propAttr"]) {
        result.name = eval(attrsMap[":propAttr"])[0];
      }
    }
    if (typeof align === "string" && align !== "center") {
      result.align = align;
    }

    if (attrsMap[":width"]) {
      result.width = attrsMap[":width"];
    }
    return result;
  });
}

标签:vue,ast,label,编译器,code,result,attrsMap,type,模板
From: https://www.cnblogs.com/zhuxiang1633/p/18256596

相关文章

  • <编译器> 7. 中间代码 | 4. 主要语句的IR树
    1.调用output:=concat(output,s)2.数组,赋值a[i+1]:=0数组变量a[i]为a(基地址)+i*w(偏移量)3.while循环伪代码:whilecnddobodystart:ifnotcndgotodonebodygotostartdone两种实现:4.for循环:变量声明+while循......
  • vue获取日期的封装方法
    ①封装js方法文件,我一般存放在utils文件夹下面。//封装的js文件名叫currentDate.jsconstgetDate={//对日期进行简单的增加和格式化操作,用于获取指定日期的后一天日期,并以特定格式返回。getIntroDuctionDay(dateTime){letdate=newDate(dateTime);......
  • Python文件嵌入html(vue)中,在浏览器中跑起来
    近期有个需求,之前项目是用python写的,需要包一层html,在浏览器中跑起来。推荐框架PyScript  https://pyscript.net/ 文档中有一些在线的example 在本地试了试<!doctypehtml><html><head><!--Recommendedmetatags--><metacharset="UTF-8"><metaname......
  • nuxt3+vue3+vite+TS实现国际化
    前言博主最近打算用Nuxt3重构了自己SSR博客,打算添加国际化功能,众所周知,实现国际化已是一个很常见的功能。用户可以根据自己的喜好,设置页面的自己合适的语言,从而实现国际化浏览。这样用户体验度会大大提升。博客用的nuxt3+vue3+vite+TS实现国际化,国际化用的vue-i18n,下面我给大......
  • Vue3如何绑定全局方法和变量
    Vue2中Vue.prototype绑定全局方法,此写法在Vue3已经不存在了。与任何全局的东西一样,应该谨慎使用。Vue3使用provide/inject或者app.config.globalProperties进行绑定如果全局属性与组件自己的属性冲突,组件自己的属性将具有更高的优先级。 一、provide/inject方法......
  • 基于SpringBoot+Vue的高校爱心捐赠系统设计与实现(源码+lw+部署+讲解)
    文章目录前言详细视频演示具体实现截图技术可行性分析技术简介后端框架SpringBoot前端框架Vue系统开发平台系统架构设计业务流程分析为什么选择我们自己的公众号(一点毕设)海量实战案例代码参考数据库参考源码及文档获取前言......
  • 学生个人网页设计作品 学生个人网页模板 简单个人主页成品 个人网页制作 HTML学生个人
    ......
  • vue通讯中provide / inject适⽤于隔代组件通信原理和例子
    在Vue中,provide和inject是用于实现跨层级组件通信的API,特别适用于隔代组件通信的场景。下面我将详细解释其原理和提供一个具体的例子。原理定义:provide:允许一个祖先组件向其所有子孙后代组件提供一个依赖,不论组件层次有多深,只要在其下游,就可以通过inject来接收。injec......
  • mybatis-mp 高级用法:ORM+SQL模板,真正意义实现ORM!!!
    官网:mybatis-mp.cn目前ORM以JPAPLUS为首的ORM,遇到稍微复杂的、或者数据库特性函数时通常需要自己写sql,或代码中,或xml中,这就有点难受了1:有没有好的办法?mybatis-mp的做法就是ORM+SQL模板,SQL模板不仅仅是sql字符串,它还帮你替换关系的信息:列SysUserRo......
  • Vue 3中的ref:深入理解响应式引用
    ......