首页 > 解决方案 > Typescript - @types/uniqid - 使用命名空间和 require("uniqid") 的问题

问题描述

在安装@types/uniqid npm install --save @types/uniqid以在类中使用后,我在使用 new 实例化此类时遇到问题。

如果我放在页面顶部import uniqid = require("uniqid");使用this.id = uniqid();它是不可能实例化控制器文件中的类(如let movie = new Models.Movie();)。如果我不使用'uniqid'控制器文件将找到正确的命名空间Models和类Movie

有人有解决方案吗?非常感谢

亲切的问候

电影文件中的代码

import uniqid = require("uniqid"); // Problem I think is here

namespace Models {

    export class Movie 
    {        
        id: string;
        title: string;
        poster: string;

        constructor(title:string, poster:string, id?: string) 
        {
            this.id = uniqid();
            this.title = title;
            this.poster = poster;
        }
    }
}

控制器文件中的代码

///<reference path="../Models/Movie.ts" />
///<reference path="../Contracts/IApiService.ts" />
///<reference path="../../Infrastructure/Api/Api.ts" />

namespace Controller {

    export class MovieController implements Contract.ExternalApiData {


        public ShowAllPopularMovies(data:any)
        {
           let movie = new Models.Movie('something','string'); // Problem here, controller file does not find Movie class
        }
    }
}

标签: typescript

解决方案


尝试使用import * as uniqId from 'uniqid';而不是您当前的导入。


推荐阅读