angular - 是否可以以子类不必在构造函数中传递路由器的方式在父类中注入路由器?
问题描述
我有一个 带有依赖项 Http、Router 的service.ts并被注入到 parent.ts中。
child.ts需要扩展父类,但不应该为在构造函数中传递服务而烦恼。为了实现这一点,我尝试了方法 (a)来实现父类而不是受信任的方法 (b),并使用 @NgModule 装饰器向 app.module.ts 中的提供者添加服务。
问题:我无法使用方法 (a)创建注入器
服务.ts
@Injectable()
export class Service {
constructor(private http: Http,
private router:Router
) {}
}
父.ts
方法(一)
export class Parent {
serv:Service
constructor(private name: string) {
const injector = Injector.create({
providers:
[{provide: Service, deps: [Http,Router]}, {provide:Http, deps:[]}, {provide:
Router, deps:[]}]
})
this.serv = injector.get(Service)
}
}
方法(b)
import { Router } from '@angular/router';
export class BasetaskComponent {
constructor(private name: string, private router : Router) {}
}
child.ts
export class Child extends Parent {
constructor() {
super('name')
}
解决方案
推荐阅读
- c# - 返回任务
> 从没有等待的 EF Core 查询? - java - 如何使用来自其他项目的模块的 Maven 配置文件
- php - 5.6 和 7.0 中 htmlentities 的 PHP 问题
- sql - 包含分隔列的导入 CSV 数据集的规范化
- selenium - Selenium 脚本抛出异常为 org.openqa.selenium.UnhandledAlertException,即使没有警报
- android-studio - 我无法在 android studio 的 xml 文件的设计选项卡中预览我的应用程序
- exception - 当应用程序在 Android 中崩溃时,Xamarin.Forms 记录异常
- c# - 在 Startup.cs .net core 2.1 中加载程序集
- node.js - 读取文件和将字符串推送到缓冲区有什么区别?- 节点
- wordpress - 如何在wordpress中将注册表滑出到下一个?