Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 5和lerna NullInjectorError:InjectionToken没有提供程序_Angular_Dependency Injection_Angular2 Injection_Lerna - Fatal编程技术网

Angular 5和lerna NullInjectorError:InjectionToken没有提供程序

Angular 5和lerna NullInjectorError:InjectionToken没有提供程序,angular,dependency-injection,angular2-injection,lerna,Angular,Dependency Injection,Angular2 Injection,Lerna,我的用例如下所示: 我有2个5个图书馆 这两个库都是从以下项目克隆的: 我有一个由lerna管理的monorepo 子库有一个父库需要使用的非常简单的指令 子库的代码 import { NgModule } from '@angular/core'; import {CommonModule} from "@angular/common"; @NgModule({ imports: [CommonModule], declarations: [ Sample

我的用例如下所示:

  • 我有2个5个图书馆
  • 这两个库都是从以下项目克隆的:
  • 我有一个由lerna管理的monorepo
  • 子库有一个父库需要使用的非常简单的指令
子库的代码

import { NgModule } from '@angular/core';
import {CommonModule} from "@angular/common";

@NgModule({
    imports: [CommonModule],
    declarations: [
        SampleDirective
    ],
    exports: [
        SampleDirective
    ]
})
export class ChildModule { }
import {Directive, PLATFORM_ID, Inject} from '@angular/core';

@Directive({
    selector: '.sample'
})
export class SampleDirective {
    constructor(@Inject(PLATFORM_ID) private _element: Object) {

    }
}
子库中指令的代码

import { NgModule } from '@angular/core';
import {CommonModule} from "@angular/common";

@NgModule({
    imports: [CommonModule],
    declarations: [
        SampleDirective
    ],
    exports: [
        SampleDirective
    ]
})
export class ChildModule { }
import {Directive, PLATFORM_ID, Inject} from '@angular/core';

@Directive({
    selector: '.sample'
})
export class SampleDirective {
    constructor(@Inject(PLATFORM_ID) private _element: Object) {

    }
}
在父模块中,我安装了子模块,我正在进行以下简单的单元测试

import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Component} from '@angular/core';
import {ChildModule} from "@nz/child-lib";

@Component({
    selector: 'nz-host',
    template: `
        <div class="sample"></div>
    `
})
export class TestWrapperComponent{}


describe('injection problem', () => {
    let testFixture: ComponentFixture<TestWrapperComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [TestWrapperComponent],
            imports: [ChildModule]                
        });
    }));

    beforeEach(async(() => {
        testFixture = TestBed.createComponent(TestWrapperComponent);
        testFixture.detectChanges();
    }));

    it('test', () => {
        expect(true).toBe(true);
    });
});
即使在使用以下代码模拟平台ID时

{provide: PLATFORM_ID, useValue: 'browser'}
错误仍然存在

作为符号链接的包

我有一个新的理论,为什么它会发生在我这边。 我想,因为我正在使用lerna来管理我的包和包依赖关系。 由于我通过lerna将子模块添加到主机模块中,因此lerna在主机的节点模块中创建了子模块的符号链接。 所以我的理论是,当我们使用的库被符号链接时,DI无法识别他需要注入什么。 试图找出如何使用--preserve符号链接运行测试


非常感谢

所以问题确实是符号链接。 在node_模块中使用symlinks或使用包管理工具(如lerna,它将内部包与symlink链接)时。 angular不知道如何正确地注入项目


我的解决方案是在运行删除符号链接的测试之前,使用硬拷贝安装软件包,然后运行测试。

我在Angular 6多模块项目和Lerna中遇到了同样的问题

首先,在Angular配置文件中有一个名为
preserveSymLinks
的选项设置为
true
Angular.json
在Angular 6中)


之后,我可以用symbolik链接将模块链接到我的项目,但不能用Lerna链接。我还不知道为什么,但我必须在Windows环境下使用mklink命令执行此操作。

您需要在测试中模拟
平台id
。即使模拟平台id,我仍然会收到相同的错误。请参见底部的“后期编辑”。也是在几周前,这种代码还可以工作,我不必为了注入它而模拟平台id。非常感谢您的帮助,我使用
npm链接
与我的库一起工作,并进入了本期。我可以确认删除链接和复制文件是有效的。我还没有使用lernajs(很快),而是使用npm链接,
preserveSymLinks
工作得很好:)+10