我是Angular 2和TypeScript的新手,我正试图在代码中使用我的另一个组件viz头中的Test类的变量。如何在角度2中导入另一个组件中的类?
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
}
export class Test{
static var1:number=10;
}
var1是静态的,因为我想在不使用测试实例的情况下使用它。
代码在头即
import { Component, OnInit } from '@angular/core';
import {Test} from '../app.component';
@Component({
selector: 'app-header',
Template;`<h1> Hello</h1>
<h1>{{Test.var1}}</h1>`
})
export class HeaderComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
该代码仅示出你好不是“10”,这是一个静态变量的另一种组分。
预先感谢您。
这还没有工作 –
现在有什么问题?控制台错误? –
我将对象obj设为obj = new Test(); 和
obj.var1
用于模板 但没有显示我10作为输出 –