【问题标题】:Angular 13 - Assertion Error on importing own packageAngular 13 - 导入自己的包时出现断言错误
【发布时间】:2022-01-08 13:13:23
【问题描述】:

也许有人可以帮助我:我已经创建了我的第一个 Angular 包。包本身必须提供不同的服务来向服务器发出 HTTP 请求并返回结果。

但是,当导入服务时,网页停止渲染,我收到一条错误消息:

core.mjs:6484 ERROR Error: ASSERTION ERROR: token must be defined [Expected=> null != undefined <=Actual]
    at throwError (core.mjs:326)
    at assertDefined (core.mjs:322)
    at bloomHashBitOrFactory (core.mjs:3591)
    at getOrCreateInjectable (core.mjs:3379)
    at Module.ɵɵdirectiveInject (core.mjs:14392)
    at NodeInjectorFactory.AppComponent_Factory [as factory] (app.component.ts:10)
    at getNodeInjectable (core.mjs:3556)
    at instantiateRootComponent (core.mjs:10159)
    at createRootComponent (core.mjs:12259)
    at ComponentFactory.create (core.mjs:21580)

我的服务:

// imports ...

/**
 * DoctorService
 */
@Injectable({
  providedIn: "root"
})
export class DoctorService {

  constructor(private readonly httpClient: HttpClient) { }

  /**
   * Returns a doctor by a given uuid.
   *
   * @param uuid
   * @return Promise<Doctor>
   */
  public async getDoctorByUUID(uuid: string): Promise<Doctor> {
    const uuidRegExp = new RegExp(/[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}/);

    if (!uuidRegExp.test(uuid)) {
      console.error("UUID does not have a valid format. It needs to be in a 8-4-4-4-12 digit pattern.")
      return new Promise<Doctor>(() => {
        return [];
      });
    }

    const httpParams: HttpParams = new HttpParams();
    httpParams.set("module", "mydoc");
    httpParams.set("sektion", "show_doctor");
    httpParams.set("uuid", uuid);
    httpParams.set("return", "json");

    const request = this.httpClient.get<Doctor>(API_BASE_URL, { params: httpParams });

    return firstValueFrom(request);
  }
}

使用服务的组件

import {Component} from '@angular/core';
import {DoctorService} from "my-doc-util/src/lib/services/doctor-service/doctor.service";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular-testing';
  
  constructor(private readonly doctorService: DoctorService) {
  }
}

【问题讨论】:

  • 我认为你应该像 @NgModule({ providers: [ DoctorService] }) 一样将此服务导入到你的模块中

标签: javascript angular package


【解决方案1】:

我遇到了同样的错误(角度 11)

我不知道是不是同样的原因,但在我的情况下,结果证明该组件有一个未初始化的输入。

【讨论】:

    猜你喜欢
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    相关资源
    最近更新 更多