この質問に似ていますが、すでに選択した回答があるようです。私は次のものを持っています...
import {Component, OnDestroy} from '@angular/core';
import {MdIconRegistry} from "@angular2-material/icon/icon-registry";
import {MD_ICON_DIRECTIVES} from "@angular2-material/icon/icon";
import {ShowcaseCardComponent} from "./showcase-card/showcase-card.component";
import { WheelService } from "../../wheel/wheel.service";
@Component({
selector: 'jg-showcase',
template: String(require('./showcase.component.pug')),
styles: [String(require('./showcase.component.styl'))],
directives: [MD_ICON_DIRECTIVES, ShowcaseCardComponent],
viewProviders: [MdIconRegistry]
})
export class ShowcaseComponent implements OnDestroy{
currentCard: number = 0;
subscription: any;
cards = [
{
number: 0,
color: "blue",
content: "<h1>Card1</h1>"
},
{
number: 1,
content: "<h1>Card2</h1>"
}
];
constructor(wheelService: WheelService){
this.subscription = wheelService.wheelEmitter.subscribe((data: any) => {
if(data.direction > 0 && this.currentCard !== this.cards.length){
this.currentCard++;
}
else if(this.currentCard){
this.currentCard--;
}
console.log("Current Card "+this.currentCard);
})
};
ngOnDestroy() {
this.subscription.dispose();
};
}
問題はthis
、インスペクターで定義されていないことですが、適切な console.log が表示されます...
Current Card 1
Current Card 2
コンソールが未定義であると言っているのはなぜですか
this.currentCard
VM10154:1 Uncaught TypeError: Cannot read property 'currentCard' of undefined(…)