首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InversifyJS -注入不是一个函数。

InversifyJS -注入不是一个函数。
EN

Stack Overflow用户
提问于 2016-07-06 19:32:31
回答 1查看 1.4K关注 0票数 3

我正在尝试使用InversifyJS建立一个NodeJS/TypeScript项目,但我在起步方面遇到了一些困难。在过去的几天里,我一直在研究这个问题,但我无法解决这个问题。所有的构建都是正确的,但是当我试图启动Node服务器时,我会得到以下错误:

代码语言:javascript
复制
TypeError: inversify_1.injectable is not a function

我正在运行NodeJS v6.2.2 (Windows10 x64),并安装了TypeScript 1.8。我试过使用VS2015和gulp来构建。下面是我尝试过的一个很小的例子,它经历了同样的错误。我看不出有什么问题,但我一定是遗漏了一些明显的东西。(撇开草率的代码不说,它应该还能工作。)

server.ts:

代码语言:javascript
复制
/// <reference path="./node_modules/reflect-metadata/reflect-metadata.d.ts" />
/// <reference path="./node_modules/inversify-dts/inversify/inversify.d.ts" />
/// <reference path="./typings/index.d.ts" />

import "reflect-metadata"
import { Application } from "./app/app"
import { ITest, TestClass } from "./app/testclass"
import { Kernel, injectable, inject } from "inversify"

var kernel = new Kernel();
kernel.bind<ITest>("ITest").to(TestClass);
kernel.bind<Application>(Application).to(Application);

var app = kernel.get<Application>(Application);
app.initialize();

app/app.ts

代码语言:javascript
复制
/// <reference path="../node_modules/inversify-dts/inversify/inversify.d.ts" />
/// <reference path="../typings/index.d.ts" />

import * as Hapi from "hapi";
import { inject, injectable } from "inversify"
import { ITest } from "../app/testclass"

@injectable()
export class Application {
    private testClass: ITest;

    constructor( @inject("ITest") test: ITest) {
        this.testClass = test;
    };

    initialize() {
        var server = new Hapi.Server({
            connections: {
                router: {
                    isCaseSensitive: false,
                    stripTrailingSlash: true
                }
            }
        });

        server.connection({
            port: '3000',
            host: 'localhost'
        });

        server.route({
            method: 'GET',
            path: '/',
            handler: (request: Hapi.Request, reply: Hapi.IReply) => {
                var str = this.testClass.test();
                reply(str);
            }
        });

        server.start(function () {
            console.log('Server running at:', server.info.uri);
        });
    }
}

app/testclass.ts

代码语言:javascript
复制
/// <reference path="../node_modules/inversify-dts/inversify/inversify.d.ts" />

import { injectable } from "inversify"

export interface ITest {
    test(): string;
}

@injectable()
export class TestClass implements ITest {
    test(): string {
        return "testing";
    }
}

tsconfig.json

代码语言:javascript
复制
{
    "files": [
        "server.ts",
        "app/app.ts",
        "app/testclass.ts"
    ],
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "target": "es6",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "outDir": "../release/"
    }
}

节点的全错误输出:

代码语言:javascript
复制
C:\Projects\inversifytest\release\app\app.js:50
    inversify_1.injectable(),
                ^

TypeError: inversify_1.injectable is not a function
    at Object.<anonymous> (C:\Projects\inversifytest\release\app\app.js:50:17)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Projects\inversifytest\release\server.js:6:15)
    at Module._compile (module.js:541:32)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-07 19:26:39

我想出来了-我怀疑这是个愚蠢的错误。我安装了错误的InversifyJS版本。装饰器只使用新版本2.x,npm已经安装了1.3.1 (因为我没有显式指定版本)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38232306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档