Javascript 如何使用injectIntl导出React类getter?
这是我的班级:Javascript 如何使用injectIntl导出React类getter?,javascript,reactjs,internationalization,react-intl,Javascript,Reactjs,Internationalization,React Intl,这是我的班级: import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { injectIntl, intlShape } from 'react-intl'; class MyClass extends Component { constructor(props) { super(props); } get pageTitle() { const {
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { injectIntl, intlShape } from 'react-intl';
class MyClass extends Component {
constructor(props) {
super(props);
}
get pageTitle() {
const { intl } = this.props;
return intl.formatMessage({id: 'messages_my_class_page_title'});
}
render() {
return (
<div>
Dummy content
</div>
);
}
}
MyClass.propTypes = {
intl: intlShape.isRequired
}
export default injectIntl(MyClass);
import React,{Component}来自'React';
从“道具类型”导入道具类型;
从'react intl'导入{injectIntl,intlShape};
类MyClass扩展组件{
建造师(道具){
超级(道具);
}
获取页面标题(){
const{intl}=this.props;
返回intl.formatMessage({id:'messages\u my\u class\u page\u title'});
}
render(){
返回(
虚拟内容
);
}
}
MyClass.propTypes={
intl:intlShape.isRequired
}
导出默认injectIntl(MyClass);
当我输入随机文本且未使用react intl injectIntl函数时,会显示我的页面标题。
我的“react intl”适用于所有其他情况。甚至对于使用“提升非反应静态”库的静态特性
我偶然发现了这个
编辑1:
我可以用电脑修理它
<FormattedMessage id="messages_my_class_page_title"/>
但是我想知道如何使用injectIntl方式。那么,您在尝试使用react intl获取页面标题时是否出错?如果你这样做了,错误消息是什么?嗨,Jaxx,我没有收到任何错误消息。这是一个空返回。当injectIntl创建组件的副本时,我甚至无法在getter中对输出进行console.log。如果删除getter(即只有一个内容完全相同的
pageTitle()
方法),然后在组件中使用{this.pageTitle()}
调用该方法,你看到显示的格式化标题了吗?@Jaxx我正在寻找getter的解决方案。因为该值是继承的,并且子类设置了该值。try with ref:true?