分享一个模板解析的方法

/**
* 根据模板渲染 字段用 ${} 包含,例如 let obj={a:"你好"}, tpl = "${a}" renderTemplete(tpl,{a}) = "你好"
* @param {String} tpl
* @param {Object} obj
*/
export const renderTemplate = (tpl, obj) => {
if (!Object[Symbol.hasInstance](obj)) {
return "";
}
return tpl.replace(/${S*?}/gi, match => {
const key = match.replace(/${/, "").replace(/}/, "");
return get(obj, key);
});
};

原文地址:https://www.cnblogs.com/qingcui277/p/11236042.html