【发布时间】:2019-02-21 11:13:23
【问题描述】:
如果我有这样的组件:
import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
selector: '...',
templateUrl: './...html',
styleUrls: ['./...scss']
})
export class TestClass implements OnInit {
testVariable : string = 'Test';
constructor() { }
ngOnInit() {}
printTest(): void{
console.log(this.testVariable);
}
}
如果我从另一个组件调用 printTest 方法,结果将是未定义的。为什么?我怎样才能弄清楚这一点?
【问题讨论】:
-
在
printTest中,将tetsVariable替换为testVariable。 -
向我们展示如何从其他组件调用该方法,以及如何从其他组件获取对
TestClass的引用。
标签: javascript angular typescript scope this