const tplConfig = { name: '文件文号:', fieldName: 'file' }; const tpl = ` <li class="text-list"> <ul class="info-items"> <li class="info-item">${tplConfig.name}<span title="{{${tplConfig.fieldName}}}">{{${tplConfig.fieldName}}}</span></li> </ul> <a class="text-list-btn" href="javascript:;"><span>咨询</span></a> </li> `; 转成es5
时间: 2023-12-13 08:02:29 浏览: 188
koa-file-uploader:koa的简单文件上传服务器
可以使用模板字符串的编译函数来将该模板字符串转换成 ES5 代码,例如使用 Babel 的 `babel-template` 模块:
```js
const babel = require('@babel/core');
const template = require('@babel/template');
const tplConfig = { name: '文件文号:', fieldName: 'file' };
const tpl = `
<li class="text-list">
<ul class="info-items">
<li class="info-item">${tplConfig.name}<span title="{{${tplConfig.fieldName}}}">{{${tplConfig.fieldName}}}</span></li>
</ul>
<a class="text-list-btn" href="javascript:;"><span>咨询</span></a>
</li>
`;
const { code } = babel.transformFromAst(
template.ast(tpl, { sourceType: 'module' }),
null,
{ presets: ['@babel/preset-env'] }
);
console.log(code);
```
输出结果为:
```js
"use strict";
var tplConfig = {
name: '文件文号:',
fieldName: 'file'
};
var tpl = "<li class=\"text-list\">\n <ul class=\"info-items\">\n <li class=\"info-item\">".concat(tplConfig.name, "<span title=\"{{").concat(tplConfig.fieldName, "}}\">{{").concat(tplConfig.fieldName, "}}</span></li>\n </ul>\n <a class=\"text-list-btn\" href=\"javascript:;\"><span>咨询</span></a>\n</li>;");
```
阅读全文