Angular 角类不工作

Angular 角类不工作,angular,typescript,Angular,Typescript,我试图在Angular中使用类来让我的生活更轻松,但到目前为止还不起作用。以下是我的代码和收到的错误: import { WizardData } from '../models/wizard-data'; export class WizardPage { private type: String; private data: WizardData; public getType(){ return this.type; } pub

我试图在Angular中使用类来让我的生活更轻松,但到目前为止还不起作用。以下是我的代码和收到的错误:

import { WizardData } from '../models/wizard-data';

export class WizardPage {
    private type: String;
    private data: WizardData;

    public getType(){
        return this.type;
    }

    public setType(type: String){
        this.type = type;
    }

    public getData(){
        return this.data;
    }

    public setData(data: WizardData){
        this.data = data;
    }
}

这在线路上给出了一个错误

wizardPage.setType("address");
即(CLI):

在Visual Studio代码中:

[ts] Duplicate identifier 'wizardPage'.
[ts] Subsequent property declarations must have the same type.  Property 'wizardPage' must be of type 'WizardPage', but here has type 'any'.
(property) WizardBaseComponent.wizardPage: WizardPage
有人知道为什么这样不行吗


谢谢

您可以直接在类内声明类的成员,但不能直接访问它们。基本上,您可以访问类的任何方法中的变量,它将属于
这个
(上下文)。在这里,你应该调用
ngOnit
lifecyclehook

ngOnInit() {
   this.wizardPage.setType("address");
}


在指定子类或继承类中变量的可访问性方面,您可以使用访问说明符,可以是
公共的
受保护的
私有的
。这些访问说明符的工作原理与它们在
OOP
语言中的工作原理类似,如
C
Java
,等等。感谢
@Florian
在评论中的提醒。

啊,谢谢你指出这一点!不幸的是,同样的错误依然存在…@PankajParkar事实上不必如此,javascript和typescript允许调用
new
,而不使用
()
不要写那么多该死的代码。@MikeD很高兴帮助你,谢谢:)你可以访问类成员,除非它们被声明为
私有
受保护的
@MikeD:你应该看看封装,它是一个核心的OOP机制。我更新了我的答案,谢谢你的提醒,伙计:)
[ts] Duplicate identifier 'wizardPage'.
[ts] Subsequent property declarations must have the same type.  Property 'wizardPage' must be of type 'WizardPage', but here has type 'any'.
(property) WizardBaseComponent.wizardPage: WizardPage
ngOnInit() {
   this.wizardPage.setType("address");
}