/**
* 根据模板渲染 字段用 ${} 包含,例如 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);
});
};

posted on 2019-07-24 09:52  祁祁  阅读(323)  评论(0编辑  收藏  举报