角类不起作用

迈克·D

我正在尝试使用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

有人知道为什么这行不通吗?

谢谢!

Pankaj Parkar

您可以直接在类中声明类的成员,但不能直接访问它们。基本上,您可以在类的任何方法内访问变量,并且该变量属于this(上下文)。在这里,您应该调用ngOnit生命周期挂钩。

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

在指定的儿童或者继承类变量的可访问性方面,您可以使用访问说明符,可以是publicprotectedprivate这些访问说明工程作为的他们如何在类似的OOP语言一样C#Java等感谢@Florian的意见,抬起头来。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章