【问题标题】:Angular 6 - Class variablesAngular 6 - 类变量
【发布时间】: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


【解决方案1】:

这里是错别字

console.log(this.tetsVariable);

应该是

console.log(this.testVariable);

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2019-02-08
    • 2019-02-28
    • 1970-01-01
    相关资源
    最近更新 更多