我正在为我正在使用的 Typescript 应用程序试用 Inversify.js。现在,没有涉及框架,所以它是纯 ES2015。
我正在尝试按照主页中的示例进行操作,但遇到了以下问题: 当我尝试在浏览器中运行它时出现“Reflect.hasOwnMetadata 不是函数”。
我使用 Webpack 作为打包工具。
这是我的文件夹结构:
这是主要的 app.ts 文件:
/// <reference path="../typings/index.d.ts" />
/// <reference path="./domain/abstract/match.interface.ts" />
import kernel from "../inversify/inversify.config.ts";
import {symbols} from "../inversify/symbols.ts";
var ninja = kernel.get<INinja>("INinja");
ninja.fight();
ninja.sneak();
接口(interface).d.ts:
interface INinja {
fight(): string;
sneak(): string;
}
interface IKatana {
hit(): string;
}
interface IShuriken {
throw();
}
反转.config.ts
/// <reference path="../node_modules/inversify/type_definitions/inversify/inversify.d.ts" />
/// <reference path="../node_modules/reflect-metadata/reflect-metadata.d.ts" />
/// <reference path="inversify.ts" />
import {Kernel} from "inversify"
//import {MatchHub} from "../app/components/Hubs/match/match-hub.component.ts";
//import {symbols} from "./symbols.ts";
import {Ninja, Katana, Shuriken} from "./inversify.ts";
var kernel = new Kernel();
kernel.bind<INinja>("INinja").to(Ninja);
kernel.bind<IKatana>("IKatana").to(Katana);
kernel.bind<IShuriken>("IShuriken").to(Shuriken);
export default kernel;
符号.ts:
export const symbols = {
Match : Symbol("Match")
}
tsconfig.json:
{
"compilerOptions": {
"noImplicitAny": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"removeComments": true,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"bower_components",
"wwwroot"
]
}
Webpack.config.js:
module.exports = {
entry: './app/app.ts',
output: {
filename: '../Scripts/app/app.js'
},
resolve: {
extensions: ['', '.Webpack.js', '.web.js', '.ts','.js', '.tsx']
},
module: {
loaders: [
{
test: /\.ts?$/,
exclude: /(node_modules|bower_components)/,
loader: 'ts-loader'
}
]
},
watch: true
}
Firefox 控制台错误:
Webpack 输出:
当我尝试安装 Inversify 时弹出以下警告:
这是一个错误吗?还是我做错了什么?谢谢!
PS:尝试按照示例文件进行操作,但我什么都看不懂!
我来自使用 Ninject 的 ASP.NET MVC 5,所以我可以理解大部分语法。
最佳答案
看来您需要包含 reflect-metadata包裹。尝试通过执行以下操作在 inversify.config.ts 中添加导入:
import "reflect-metadata";
关于javascript - Inversify.js - Reflect.hasOwnMetadata 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37534890/