property+模板字符串=?

@property({ tooltip: “zh” }) zhText: string = ``;

1

this.label.string = this.zhText;

string怎么样才可以按照模板字符串运行

什么意思,你是想在编辑器修改Zh Text,编辑器里的界面文本立刻变?

不是的,${Global.test}在运行的时候会被认为是string类型,看图2

这需要你自己去解析替换字符串。

1赞

好吧 可能也只能这样了

import Global from "../Global";

protected objects = {
   "Global": Global
}

protected replaceTemplateValues(templateString) {
    const regex = /\${(.*?)}/g;
    let matches;
    let parsedText = templateString;

    while ((matches = regex.exec(templateString)) !== null) {
      const expression = matches[1];
      const properties = expression.split('.');
      let object = this.objects[properties[0]];

      if (object) {
        for (let i = 1; i < properties.length; i++) {
          const propertyName = properties[i];

          if (propertyName in object) {
            object = object[propertyName];
          } else {
            object = undefined;
            break;
          }
        }

        if (object !== undefined) {
          const replacement = object.toString();
          parsedText = parsedText.replace(matches[0], replacement);
        }
      }
    }
    return parsedText;
  }

this.replaceTemplateValues("这个是测试文本:${Global.obj.test.value}")
this.replaceTemplateValues("这个是测试文本:${Global.string}")