Javascript 如何在typescript中导入/使用反射
我正在尝试在中使用代码。它使用反射。这是一份副本:Javascript 如何在typescript中导入/使用反射,javascript,node.js,typescript,Javascript,Node.js,Typescript,我正在尝试在中使用代码。它使用反射。这是一份副本: export function CustomComponent(annotation: any) { return function (target: Function) { var parentTarget = Object.getPrototypeOf(target.prototype).constructor; var parentAnnotations = Reflect.getMetadata('annotatio
export function CustomComponent(annotation: any) {
return function (target: Function) {
var parentTarget = Object.getPrototypeOf(target.prototype).constructor;
var parentAnnotations = Reflect.getMetadata('annotations', parentTarget);
var parentAnnotation = parentAnnotations[0];
Object.keys(parentAnnotation).forEach(key => {
if (isPresent(parentAnnotation[key])) {
annotation[key] = parentAnnotation[key];
}
});
var metadata = new ComponentMetadata(annotation);
Reflect.defineMetadata('annotations', [ metadata ], target);
}
}
首先,我有两个错误:
Property 'getMetadata' does not exist on type 'typeof Reflect'.
Property 'defineMetadata' does not exist on type 'typeof Reflect'.
然后我运行了npm install reflect metadata
,但我不知道如何使用它
import { Reflect } from reflect-metadata;
Module '".../node_modules/reflect-metadata/index"' has no exported
member 'Reflect'.
或
或
或
或
当然,我只是错过了一些愚蠢的东西,甚至是一个打字错误。如何使用此代码?Typescript使用的模块加载范例与JavaScript略有不同 假设您有一个模块
module
,它定义了三个类a
、B
和C
import { A } from "Modulus"
将从模块导入类(或函数)A
,并使其在当前模块中可用。如果Typescript在模数
中未找到名为A
的导出,则将抛出错误
// Equivalent to the following in JavaScript:
// var ModuleNameOfYourChoice = require("Modulus")
import * as ModuleNameOfYourChoice from "Modulus"
将导入module
中声明的所有导出,并以ModuleNameOfYourChoice
的名称将它们提供给当前模块
对于您的代码,您需要reflect metadata
模块中定义的所有导出,因此需要将其作为
import * as Reflect from "reflect-metadata"
祝你一切顺利
Typescript文档:您必须将类型声明与(js)库一起导入 在.ts文件中:
import "reflect-metadata";
到今天为止,
@types/reflect metadata
已被弃用,因为reflect metadata
已经包含自己的类型
因此,要使用它们,只需导入库(使用全局范围)。就这样
npm安装反映元数据--保存
导入“反映元数据”代码>
如果可以使用es7+,我找到了另一个解决方案。因此,在
tsconfig.json文件中,您只需要更改一行。将“目标”
的分配更改为至少es2017。现在,您可以使用现成的反射。我认为这是一个非常好的解决方案,因为您不必关心其他依赖项
示例tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"outDir": "../dist",
"module": "umd",
"target": "es2017",
"sourceMap": true,
"noImplicitAny": true,
"strictNullChecks": true,
"removeComments": false,
"moduleResolution": "node"
},
"exclude": [
"node_modules",
"bower_components",
"dist"
]
}
谢谢,捆绑包更新失败错误是另外一个错误。导入“反射元数据”;导致eslint抱怨“应分配导入的模块”。这不是一种好的编码方式。那么isPresent()
?函数isPresent(obj:any):布尔值{return obj!==undefined&&obj!==null;}@Jackie-hmm这对我来说很奇怪。你能提供一些例子吗?当然我现在在这里玩@Jackie,如果我尝试打开这个链接,我会得到404
// Equivalent to the following in JavaScript:
// var ModuleNameOfYourChoice = require("Modulus")
import * as ModuleNameOfYourChoice from "Modulus"
import * as Reflect from "reflect-metadata"
npm install reflect-metadata -D
import "reflect-metadata";
{
"compilerOptions": {
"experimentalDecorators": true,
"outDir": "../dist",
"module": "umd",
"target": "es2017",
"sourceMap": true,
"noImplicitAny": true,
"strictNullChecks": true,
"removeComments": false,
"moduleResolution": "node"
},
"exclude": [
"node_modules",
"bower_components",
"dist"
]
}