简体   繁体   English

使用 lodash 的打字稿字符串插值

[英]typescript string interpolation with lodash

In my application, I have a H custom component that takes in a header and a subheader as props.在我的应用程序中,我有一个 H 自定义组件,它接受一个标题和一个子标题作为道具。

For my header prop, I want to concatenate the lodash expression with a string.对于我的标题道具,我想将 lodash 表达式与字符串连接起来。 See below my code.请参阅下面我的代码。

        <H
          header= `${_.startCase(type)} SomeString`
          subheader= "Lorem ipsum do eiusmod"
        />

I have tried the suggestions here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals , but maybe because I am using lodash it is not working.我已经尝试过这里的建议https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals ,但可能是因为我使用的是 lodash 它不起作用。

The syntax for your prop is incorrect:您的道具的语法不正确:

header={`${_.startCase(type)} SomeString`}

In React, any prop value that contains code must be enclosed in {} .在 React 中,任何包含代码的 prop 值都必须包含在{}中。

Only constant strings like prop="a string" may be passed without a pair of enclosing {} .只有像prop="a string"这样的常量字符串可以在没有一对封闭{}的情况下传递。 All other values require it*.所有其他值都需要它*。


* Well, there are two special cases. * 好吧,有两种特殊情况。

children which is set like the following. children设置如下。 Here children is something like <>content</> .这里的children类似于<>content</>

<MyComponent>content</MyComponent>

And boolean shorthand props.和布尔速记道具。 Here isCool is true .这里isCooltrue

<MyComponent isCool />

Although, if you want to set it to false you need curly braces again:虽然,如果要将其设置为false ,则需要再次使用花括号:

<MyComponent isCool={false} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM