我正试图在react原生typescript项目中使用我自己的typescript依赖ioc服务容器。
依赖项使用typescript属性装饰器,如下所示:
const service = new Service();
ServiceContainer.set('service', () => service);
class Foo {
@inject
service!: Service;
}
这对reactJS、Vue&节点后端。但它在react native中不起作用。Foo中的属性service
仍然未定义。经过一些调试,我发现传递给inject
-decorator的属性是一个空对象,而不是Foo类。
我认为问题在于转换我的代码。。。但我不确定。
babel.config.js:
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
['@babel/plugin-proposal-decorators', { 'legacy': true }], // required for @boundMethod decorator
],
};
tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
// ...
"experimentalDecorators": true,
}
}
在tsconfig文件上使用emitDecoratorMetadata:true