简体   繁体   English

如何在角度2的另一个组件中导入类?

[英]How to import class in another component in angular 2?

I am new to Angular 2 and TypeScript and I'm trying to following code in which I want to use the variable of Test class in my another component viz header. 我是Angular 2和TypeScript的新手,我尝试遵循以下代码,在其中我要在另一个组件viz标头中使用Test类的变量。

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 is static because I want to use it without making test instance. var1是静态的,因为我想在不创建测试实例的情况下使用它。

Code in the another component viz header 另一个组件的标头中的代码

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() {
  }

}

This code showing only hello not "10" which is a static variable. 此代码仅显示问候,而不显示“ 10”,它是一个静态变量。

Thank you in advance. 先感谢您。

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(private test: Test) { }

  ngOnInit() {
  }

}

Mistakes 误区

  • use template not Template 使用template而不是Template
  • use : not ; 使用:; after template 模板后
  • make instance in the constructor 在构造函数中创建实例

So even though you do not want to instantiate instances of Test, you will still need to use instance properties of your component. 因此,即使您不想实例化Test的实例,您仍将需要使用组件的实例属性。

Within your component simply bind the class Test to a an instance property, and access that property in the template. 在您的组件中,只需将Test类绑定到一个实例属性,然后在模板中访问该属性。

test = Test;

Access this in the template with: 使用以下模板在模板中进行访问:

{{ test.var1 }}

Here is another way: 这是另一种方式:

import { Component, OnInit } from '@angular/core';
import { Test } from './app.component';
@Component({
    selector: 'app-header',
    template: `<h1> Hello</h1><h1>{{var1}}</h1>`
})
export class HeaderComponent implements OnInit {
    var1 = Test.var1; // place static var into local var
    ngOnInit() {
    }
}

You had set the variable as public. 您已将变量设置为public。 you have initialized Test as a private property. 您已将Test初始化为私有属性。 Hence it's scope is just inside the component and not on the template. 因此,它的作用域仅在组件内部,而不在模板上。

import { Component } from '@angular/core';
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css'] 
})

export class AppComponent {
}

@Injectable()
export class Test{
  static var1:number = 10;
}


import {Test} from '../app.component';
@Component({
  selector: 'app-header',
  Template;`<h1> Hello</h1>
  <h1>{{Test.var1}}</h1>`
})

export class HeaderComponent implements OnInit {
   constructor(test:Test) { }
   ngOnInit() {
   }
}

Here using @Injectable() decorator before test class, And use the Test in providers[] of app.module.ts , I hope it works.. 在这里,在测试类之前使用@Injectable()装饰器,并在app.module.ts的 provider []中使用Test ,我希望它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM