如何使用从数据库或变量查询的模板文字?

How to use template literals queried from database or variable?

我有 HTML 个电子邮件模板存储在 MySQL 数据库中。其中大部分包含 JavaScript 个变量。我尝试使用模板文字(即:${this.auth.user.name})来存储它们,但这不会 return 变量而是确切的字符串。我认为我的问题是因为字符串被双引号或单引号而不是反引号括起来。

所以我的 JavaScript 看起来如下,其中 template 是从我的 MySQL 此处显示查询的对象数组。

var email_template: this.template[0].body;

The actual output is Hello ${this.auth.name}!

我期待 Hello Jack 的输出!

您知道问题出在哪里吗?

提前致谢, 蒂博

尝试像这样制作 new Function

const name = "Jack";
const string = "Hello ${name}";
const actualString = new Function("return `" + string + "`").call(string);
console.log(actualString);