[英]Angular Class not working
我正在嘗試使用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;
}
}
和
import { Component, OnInit } from '@angular/core';
import { WizardPage } from '../../shared/models/wizard-page';
@Component({
selector: 'app-wizard-base',
templateUrl: './wizard-base.component.html',
styleUrls: ['./wizard-base.component.scss']
})
export class WizardBaseComponent implements OnInit {
wizardPage: WizardPage = new WizardPage();
wizardPage.setType("address");
constructor() { }
ngOnInit() {}
}
這在行上給出了一個錯誤
wizardPage.setType("address");
即(CLI):
ERROR in src/app/wizard/wizard-base/wizard-base.component.ts(14,13): error TS1005: ';' expected.
src/app/wizard/wizard-base/wizard-base.component.ts(14,22): error TS1003: Identifier expected.
在Visual Studio Code中:
[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
有人知道為什么這行不通嗎?
謝謝!
您可以直接在類中聲明類的成員,但不能直接訪問它們。 基本上,您可以在類的任何方法內訪問變量,並且該變量將屬於this
變量(上下文)。 在這里,您應該調用ngOnit
生命周期掛鈎。
ngOnInit() {
this.wizardPage.setType("address");
}
在指定子類或繼承類中變量的可訪問性方面,可以使用訪問說明符,可以是public
, protected
, private
。 這些訪問說明符的工作方式類似於它們在C#
, Java
等OOP
語言中的工作方式。感謝@Florian
的評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.