[英]Angular NullInjector for Location when using providedIn: platform
I have a strange problem.我有一个奇怪的问题。 I am working on a library that is imported into my project where I have a service specified as below:
我正在开发一个导入到我的项目中的库,其中指定了如下服务:
import { Injectable } from '@angular/core';
import { Location } from '@angular/common';
@Injectable({
providedIn: 'platform'
})
export class MyService {}
However when I import this into my project, I get a NullInjector error for Location.但是,当我将它导入到我的项目中时,我收到了位置的 NullInjector 错误。
If I change the Injectable to providedIn: 'root', then I get a NullInjector for MyService.如果我将 Injectable 更改为providedIn:'root',那么我会得到一个用于MyService 的NullInjector。
Does anyone know how I can use providedIn: 'platform' and still be able to inject Location?有谁知道我如何使用providedIn:'platform'并且仍然能够注入位置?
I've tried adding Location to the app module and it made no difference.我尝试将 Location 添加到应用程序模块,但没有任何区别。
The problem was that I was not properly importing MyService in the project that was receiving it.问题是我没有在接收它的项目中正确导入 MyService。 providedIn: 'platform' simply masked the wider issue.
providedIn: 'platform' 只是掩盖了更广泛的问题。
Once I imported it in imports: [] I was able to revert all the providedIns back to 'root' and the problem is gone.一旦我在 imports: [] 中导入它,我就能够将所有提供的 Ins 恢复为“root”,问题就消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.