简体   繁体   English

Angular NullInjector for Location 使用providedIn:平台

[英]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.

相关问题 在 angular 中,使用 providedIn 和 forRoot 时的循环依赖 - in angular, Circular dependency when using providedIn and forRoot 尽管使用了 @Injectable({providedIn: 'root' }) 装饰器,Angular 服务在注入到组件中时不会充当单例 - Angular Service not acting as singleton when injected in components despite using @Injectable({providedIn: 'root' }) decorator 提供平台与根 - providedIn platform vs root Angular获得了循环依赖项检测警告,当使用带有providerIn:LazyModule的服务时 - Angular got Circular dependency detection WARNING when using a Service with providedIn: LazyModule 提供Angular 7服务:'root' - Angular 7 Service providedIn: 'root' 在 Angular 6 中生成服务时,提供 Injectable 装饰器的目的是什么? - What is the purpose of providedIn with the Injectable decorator when generating Services in Angular 6? 在'root','platform','any' w.r.t providedIn 中,在 Angular 的这种情况下应该首选哪一个? - Out of 'root', 'platform', 'any' w.r.t providedIn, which one should be preferred in which case in Angular? ANgular 5- BsModalRef引发NullInjector错误 - ANgular 5- BsModalRef throws NullInjector error Akita 和 Angular 错误:StaticInjector 和 NullInjector 存储、查询 - Akita and Angular Error: StaticInjector and NullInjector Store, Query Angular 服务在 VS forRoot 中提供 - Angular service providedIn VS forRoot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM