【发布时间】:2017-02-25 12:28:58
【问题描述】:
我是一名 javascript 程序员,试图用 angular 2 编写打字稿,需要一些帮助:
我有一个组件,它需要来自另一个打字稿文件的数据然后使用它,例如(我意识到这是非常做作的,我想了解如何做这样的事情):
app.component.ts:
import { exampleArray } from './array';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(){
this.logToConsole(exampleArrary.arrayProperty);
}
logToConsole(anything:number[]) {
console.log(anything);
}
}
array.ts:
export class exampleArray {
arrayProperty = [1, 2, 3];
}
但我收到此错误:
Module 'array' has no exported member 'exampleArray'.)
app.component.ts (12,23): Cannot find name 'exampleArrary'.)
这是解决此类问题的正确方法吗?有什么我想念的吗?我尝试过“新班”但没有运气
【问题讨论】:
-
1.您没有在应用程序组件的任何地方使用导入的 Array 类。 2.数组是...数组的基本类型。为您自己的班级使用另一个名称。请不要选择数字或字符串。
-
@JBNizet 我已经编辑了问题以反映这一点
-
为了访问对象的属性,您需要创建一个对象:
const a = new exampleArray(); console.log(a.arrayProperty);。如果 arrayProperty 是static,那么您的代码就可以了,即会有一个 exampleArray,链接到类本身,而不是一个新数组,链接到类的每个实例。 (如果您正确拼写 exampleArray,即不是exampleArrary)。请注意,按照惯例,类以大写字母开头。 -
很晚了,谢谢提示
标签: angular typescript