假设我在一个文件 (test1.ts) 中声明了一个枚举:
export enum Colors{
red=1,
blue=2,
green=3
}
Run Code Online (Sandbox Code Playgroud)
然后在另一个文件 (test2.ts) 中,我声明了一个具有方法的类。该方法的参数之一是 Colors 枚举中定义的 Color:
'use strict';
declare var require: any;
declare var exports: any;
var Colors = require('Colors');
class DoSomethingWithColor{
ColorFunction(aColour:Colors){
//Funky color processing here..
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误:
不能精细命名颜色
即使它在第二个文件中导出并需要。我在这里做错了什么,或者这只是不是做我想做的事情的“打字稿”方式(如果是这样,首选的方式是什么?)?
谢谢
As was mentioned by jonrsharpe in the comments you have to use one of the typescript supported import statements.
import * from './test1' as Colors
Run Code Online (Sandbox Code Playgroud)
or
import {Colors} from './test1'
Run Code Online (Sandbox Code Playgroud)
有关 typescript 中模块(现在是命名空间)的导入语句和最佳实践的更多信息,请查看他们的文档:https://www.typescriptlang.org/docs/handbook/modules.html https://www.typescriptlang.org /docs/handbook/namespaces-and-modules.html
通常,如果您使用导出/导入语句,则需要CommonJS或WebPack等模块加载器。这些程序捆绑您的代码,并负责确保依赖项在运行时可供导入程序使用。从 import 语句开箱即用的事实来看,您很可能已经在使用模块加载器了。
归档时间: |
|
查看次数: |
5589 次 |
最近记录: |