简体   繁体   English

是否可以在角度2的构造函数中声明变量

[英]is it possible to declare variable in a constructor in angular 2

This is the component I am referring to its a child component to the main component when the app is run, an error is thrown pointing to this components selector <app-new></app-new> 这是我在运行应用程序时将其称为主要组件的子组件的组件,指向该组件选择器<app-new></app-new>会引发错误

error says: 错误说:

'No provider for String! '没有String提供商! ; ; Zone: ; 区域: Task: Promise.then ; 任务:Promise.then; Value: Error: No provider for String!' 值:错误:没有字符串提供程序!

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-new',
  template: `<h2>SOme New Compnent which uses interpolation as well</h2>
  <h3>{{someProp}}</h3>`
})
export class CompyNewComponent implements OnInit {
  public prop2: string;
  public prop3: number;

  constructor(private someProp: string) {
    this.someProp = 'set throught the constuctor';
  }
  someMethod() {
    console.log(this.someProp);
  }

  ngOnInit() {
    this.someMethod();
    console.log('this is inside the ngoninit life cylce hook');
  }

}

// App Modules File //应用模块文件

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { NewComponent } from './new/new.component';
import { DirectDirective } from './direct.directive';
import { SomeComponent } from './compy/some.component';
import { CompyNewComponent } from './new compy/compy1.component';

@NgModule({
  declarations: [
    AppComponent,
    NewComponent,
    DirectDirective,
    SomeComponent,
  CompyNewComponent],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

i think the error lies here -> constructor(private someProp: string) { this.someProp = 'set throught the constuctor'; } 我认为错误就在这里-> constructor(private someProp: string) { this.someProp = 'set throught the constuctor'; } constructor(private someProp: string) { this.someProp = 'set throught the constuctor'; }

in angular inside a constructor you add services not properties like you do in react. 在构造函数中的尖角中,您添加服务而不是像在react中那样添加属性。

So remove the constructor and add this.prop1 = 'something1'; this.prop2 = 'something2'; 因此,删除构造函数并添加this.prop1 = 'something1'; this.prop2 = 'something2'; this.prop1 = 'something1'; this.prop2 = 'something2';

inside the ngOnInit() ngOnInit()内部

EDIT: import { CompyNewComponent } from './new compy/compy1.component'; 编辑: import { CompyNewComponent } from './new compy/compy1.component'; there might be an error there too...better use - for example new-compy 那里也可能有错误...更好地使用-例如new-compy

If you define a class with a constructor, you can declare variables in that constructor: 如果使用构造函数定义类,则可以在该构造函数中声明变量:

class MyClass {
    constructor(public str: string, public value: number) {
    }
}

class AnotherClass {
    ...
    public myMethod(): void {
        let obj = new MyClass("Hello World", 10);
        ....
    }
}

However, when you define a component or a service, the constructor is called by Angular, which expects parameters with injection metadata, as mentioned in this Angular documentation about dependency injection: 但是,当您定义组件或服务时,Angular会调用构造函数,它会期望带有注入元数据的参数,如以下有关依赖项注入的Angular文档中所述

When Angular creates a class whose constructor has parameters, it looks for type and injection metadata about those parameters so that it can inject the right service. 当Angular创建一个其构造函数具有参数的类时,它将查找有关这些参数的类型和注入元数据,以便可以注入正确的服务。

If Angular can't find that parameter information, it throws an error. 如果Angular找不到该参数信息,则会引发错误。

Angular can only find the parameter information if the class has a decorator of some kind. 如果该类具有某种装饰器,则Angular只能找到参数信息。 While any decorator will do, the @Injectable() decorator is the standard decorator for service classes. 尽管任何装饰器都会这样做,但@Injectable()装饰器是服务类的标准装饰器。

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

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