typescript - 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
}
}
}
解决方案
尝试使用import * as uniqId from 'uniqid';
而不是您当前的导入。
推荐阅读
- view - 我很难过如何使用 SwiftUI 在视图上弹出子视图
- python - 如何根据熊猫中的特定字符串选择实际行及以上?
- webpack - 为什么 Create-React-App 的任何输出构建文件都不包含 __webpack_require__ 函数
- karate - 空手道 - 如何根据特定 json 数组中的多个属性值获取特定属性的值
- javascript - 在反应中显示悬停在特定卡上
- microsoft-graph-api - 在 Graph Explorer 中请求列出根子节点返回空
- sql - 试图列出在西雅图某物业工作的员工的员工姓名和 ID
- python - 为什么 PyOpenGL 生成的位图图像中会出现额外的 RGB 颜色?
- php - PHP SQL数组在foreach循环中重复每个ID而不是一次一个ID
- javascript - 跨文件的打字稿全局变量