我是 angular6 的新手,当我将参数从组件发送到服务时,我收到 StaticInjectorError。怎么了?
我的代码是这样的组件:
import { Component, Input, OnInit } from '@angular/core';
import { myService } from '../../@core/data/myService';
@Component({
selector: 'table',
templateUrl: './table.component.html'
})
export class TableComponent implements OnInit {
constructor(private service: myService) {
}
ngOnInit() {
this.service = new myService ("name");
}
Run Code Online (Sandbox Code Playgroud)
服务:
import { Injectable, Inject } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { DataService } from './data.service';
@Injectable() export class myService {
constructor(
entityName: string,
) {
console.log(entityName);
}
}
Run Code Online (Sandbox Code Playgroud)
app.module.ts:
import { …Run Code Online (Sandbox Code Playgroud) frontend dependency-injection web-frontend typescript angular6
我正在尝试将字符串注入我的角度组件.下面的代码工作正常,但它给出了弃用警告: 不推荐使用get:从v4.0.0开始使用Type或InjectionToken
@Component({
selector: 'app-label',
templateUrl: './label.component.html',
styleUrls: ['./label.component.scss']
})
export class LabelComponent {
public name: string;
public caption: string;
constructor(
private injector: Injector
) {
this.name = this.injector.get('name');
this.caption = this.injector.get('caption');
}
}
Run Code Online (Sandbox Code Playgroud)
因此我尝试使用这个非常简短
且不完整的文档Angular 5 Injector来提出类似下面的内容,但是我不想useValue或者useClass,我需要的是注入的实际值.
class BS { }
// [..]
const name = new InjectionToken<string>('name');
const caption = new InjectionToken<string>('caption');
const injector = ReflectiveInjector.resolveAndCreate([
{ provide: name, useClass: BS},
{ provide: caption, useClass: BS}
]);
this.name = this.injector.get(name);
this.caption = …Run Code Online (Sandbox Code Playgroud)