如何在常量中替换es6模板字符串

时间:2021-05-27 00:06:12

I have this constant:

我有这个常数:

.constant('SOMETHING', {
   name: `${name}`,
   subject: `${surname}`,
})

When I am trying to do this for example SOMETHING.name it says that it's undefined.

当我试图这样做时,例如SOMETHING.name,它说它是未定义的。

Any ideas how I can store template strings and then use it?

有关如何存储模板字符串然后使用它的任何想法?

Thank you very much in advance.

非常感谢你提前。

1 个解决方案

#1


2  

This isn't a feature that ES6 template literals provide.

这不是ES6模板文字提供的功能。

es6-template package may be used to interpolate regular JS string with template literal syntax. At this point ES6 template literal syntax has no real benefits over other template engines.

es6-template包可用于使用模板文字语法插入常规JS字符串。此时,ES6模板文字语法与其他模板引擎相比没有任何实际好处。

If the context is the framework (I assume that constant stands for AngularJS constant service), it may beneficial to use template facilities that the framework offers, i.e.

如果上下文是框架(我假设常量代表AngularJS常量服务),那么使用框架提供的模板工具可能是有益的,即

app.constant('SOMETHING', {
   name: '{{ name }}',
   ...
});

app.run((SOMETHING, $interpolate) => {
  let somethingName = $interpolate(SOMETHING.name)({ name: 'name' });
  ...
});

#1


2  

This isn't a feature that ES6 template literals provide.

这不是ES6模板文字提供的功能。

es6-template package may be used to interpolate regular JS string with template literal syntax. At this point ES6 template literal syntax has no real benefits over other template engines.

es6-template包可用于使用模板文字语法插入常规JS字符串。此时,ES6模板文字语法与其他模板引擎相比没有任何实际好处。

If the context is the framework (I assume that constant stands for AngularJS constant service), it may beneficial to use template facilities that the framework offers, i.e.

如果上下文是框架(我假设常量代表AngularJS常量服务),那么使用框架提供的模板工具可能是有益的,即

app.constant('SOMETHING', {
   name: '{{ name }}',
   ...
});

app.run((SOMETHING, $interpolate) => {
  let somethingName = $interpolate(SOMETHING.name)({ name: 'name' });
  ...
});