javascript - Inversify.js - Reflect.hasOwnMetadata 不是函数

标签 javascript asp.net-mvc typescript inversifyjs

我正在为我正在使用的 Typescript 应用程序试用 Inversify.js。现在,没有涉及框架,所以它是纯 ES2015。

我正在尝试按照主页中的示例进行操作,但遇到了以下问题: 当我尝试在浏览器中运行它时出现“Reflect.hasOwnMetadata 不是函数”。

我使用 Webpack 作为打包工具。

这是我的文件夹结构:

enter image description here

这是主要的 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 控制台错误:

firefox console error

Webpack 输出:

enter image description here

当我尝试安装 Inversify 时弹出以下警告:

enter image description here

这是一个错误吗?还是我做错了什么?谢谢!

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/

相关文章:

asp.net-mvc - 带有下拉列表的 Telerik 网格

javascript - 在 jsconfig.json 中找不到“import-resolver-typescript/lib”错误

javascript - 使用钩子(Hook)响应更改 bool 值并获取回调

javascript - 使用 JavaScript 进行计算

javascript - 强制 __proto__ 平等

javascript - 优化 HTML5 canvas 中的像素设置

c# - 是否可以在服务器中使用Google Analytics(分析)?

c# - 在枚举列表中的按钮上使用 jquery

javascript - 将鼠标悬停在图表上时如何显示百分比?

typescript - 使用 typescript 扩展 JQueryStatic