如何在输入占位符中使用 FormattedMessage

How to use FormattedMessage in input placeholder

我知道这里有类似的问题:

但我正在使用 TypeScript。

此代码:

import { injectIntl, intlShape } from 'react-intl';

ChildComponent.propTypes = {
  intl: intlShape.isRequired
}

不适合我。

如何使用格式化消息设置此占位符。

<InputPass
  placeholder="Enter password"
/>

您始终可以使用 react-intl 中的 formatMessage 函数。

所以你可以这样做:

<InputPass
    placeholder={intl.formatMessage("messageKey")}
/> 

如果您的问题更多在于使用打字稿输入 injectIntl​​。您可以执行以下操作:

interface MyComponentProps {
  /* non injected props */
}
interface MyComponentInjectedProps extends MyComponent {
  intl: InjectedIntl;
}

class MyComponent extends Component<MyComponentInjectedProps, {}> {
    ...
}

export default injectIntl<MyComponentProps>(MyComponent)

请注意我们如何将 ComponentProps 作为类型参数传递,这样当您使用来自其他地方的组件时,打字稿不会抱怨 intl 属性 未被传递。