Javascript 角度6类变量

Javascript 角度6类变量,javascript,angular,typescript,scope,this,Javascript,Angular,Typescript,Scope,This,如果我有这样一个组件: import { Component, OnInit, ViewChild } from '@angular/core'; @Component({ selector: '...', templateUrl: './...html', styleUrls: ['./...scss'] }) export class TestClass implements OnInit { testVariable : string = 'Test'; cons

如果我有这样一个组件:

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方法,结果将是未定义的。为什么?我怎么才能弄明白呢?

这里输入错误

console.log(this.tetsVariable);
应该是

console.log(this.testVariable);

printTest
中,将
tetsVariable
替换为
testVariable
。向我们展示如何从其他组件调用该方法,以及如何从该其他组件获取对
TestClass
的引用。