TypeScript/Angular:记录实例变量



目前我正在学习使用TypeScript构建Angular应用程序。在开发过程中,我创建了一个新对象。该对象的类包含一个变量(设置(,我想将其记录到joke.module.ts中的控制台。

但是,这是行不通的。我得到的错误是:函数实现丢失或未紧跟在声明之后。

为什么会这样,我应该如何记录?

笑话.组件.ts

import { Component } from '@angular/core';
@Component({
selector: 'joke',
templateUrl: './joke.component.html'
})
export class JokeComponent {
setup: string;
punchline: string;
constructor() {
this.setup = "What did the cheese say when it looked in the mirror?";
this.punchline = "Halloumi (hello me)"
}
}

笑话.模块.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { JokeComponent } from './component/joke.component';
@NgModule({
imports: [BrowserModule],
declarations: [JokeComponent],
bootstrap: [JokeComponent]
})
export class JokeModule {
joke = new JokeComponent();
console.log(joke.setup); // error here
}

这是因为您尝试直接在类中而不是在方法中编写实现:

export class JokeModule {
private joke: JokeComponent;
constructor() {
this.joke = new JokeComponent();
console.log(this.joke.setup); // error here
}
}

另外,我正在为那个私人笑话寻找奖励积分。

最新更新