根据参数更新对象中的字符串值 - Typescript

Update the string value in the object based on the parameter - Typescript

我在单独的 ts 文件中导出了一个对象,我希望对其进行参数化

export const customer = {
  comment: 'Wow, this is great because {{username}} is doing what we needed' 
};

然后在另一个ts文件中,我得到了这个常量但是我想传递用户名的值

import * as customerConstants from 'constants';
export class CustomerComponent implements OnInit{
 username ='learn' 
 customerData = customerConstants.customer ;

  ngOnInit(): void {
    this.customerData.comment = customerData + username;
  }

我想在客户常量中用 learn 代替用户名。可能吗?

由于 customer.comment 是字符串,您可以使用 replace()'{{username}}' 替换为 username 变量值。