在 ng build --prod 期间无法解析验证指令中的所有参数

我做了一个自定义验证指令来验证唯一的永久链接。这段代码工作正常,但是当我尝试为生产创建构建时,它给了我以下错误:-


错误:无法解析 E:/Manish/Projects/ampleAdmin/src/app/shared/permalink-validation.directive.ts: ([object Object], ?) 中 UniquePermalinkValidatorDirective 的所有参数。


永久链接-validation.directive.ts


import { Directive } from '@angular/core';

import { AsyncValidator, AbstractControl, ValidationErrors, NG_ASYNC_VALIDATORS, AsyncValidatorFn } from '@angular/forms';

import { Observable, of } from 'rxjs';

import { map } from 'rxjs/operators';

import * as qs from 'qs';

import { PageService } from '../services/page.service';

import { IPage } from '../client-schema';


export function UniquePermalinkValidator(pageService: PageService, page: IPage): AsyncValidatorFn {

  return (ctrl: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> => {

    if (!(ctrl && ctrl.value)) { return null; }


    const cond: any = {

      where: {

        permalink: ctrl.value

      }

    };


    if (page && page.id) {

      cond.where.id = { nin: [page.id]};

    }

    const query = qs.stringify(cond, { addQueryPrefix: true });


    return pageService.getPageCount(query).pipe(

      map(res => {

        return res && res.count ? { uniquePermalink: true } : null;

      })

    );

  };

}


@Directive({

  selector: '[appUniquePermalink]',

  providers: [{ provide: NG_ASYNC_VALIDATORS, useExisting: UniquePermalinkValidatorDirective, multi: true }]

})

export class UniquePermalinkValidatorDirective implements AsyncValidator {


  constructor(private pageService: PageService, private page: IPage) { }


  validate(ctrl: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> {

    return UniquePermalinkValidator(this.pageService, this.page)(ctrl);

  }

}


慕田峪4524236
浏览 170回答 1
1回答

墨色风雨

给定的export function UniquePermalinkValidator(pageService: PageService, page: IPage): AsyncValidatorFn {&nbsp; // ...}并给予@Directive({&nbsp; selector: '[appUniquePermalink]',&nbsp; providers: [{ provide: NG_ASYNC_VALIDATORS, useExisting: UniquePermalinkValidatorDirective, multi: true }]})export class UniquePermalinkValidatorDirective implements AsyncValidator {&nbsp; &nbsp;&nbsp;&nbsp; constructor(private pageService: PageService, private page: IPage) {}&nbsp; // ...}鉴于它仅由以下IPage定义export interface IPage {&nbsp; id: number;&nbsp; // ...}然后UniquePermalinkValidatorDirective根据定义将无法工作,以描述的方式失败。Aninterface只在类型空间中定义了一些东西,而不是在值空间中,因此没有任何运行时表现形式。这意味着它不能用于值位置。本质上,Angular 的依赖注入系统读取构造函数参数的类型,当值空间中有对应命名的声明时,它会使用对应命名的值作为注入标记。例如,以下import {Injectable} from '@angular/core';@Injectable() export class Service {&nbsp; &nbsp; constructor(http: Http) {}}也可以写import {Inject} from '@angular/core';export class Service {&nbsp; &nbsp; constructor(@Inject(Http) http: ThisTypeIsArbitraryWithRespectToInjection) {}}这意味着同样的事情请注意如何Http作为参数传递给Inject. 但是Inject(IPage), where IPageis an interface, 格式错误。的主要目的@Inject(ProviderToken)是允许在像您这样的情况下将提供者与装饰参数的类型正交地注入。因此,您需要类似的东西constructor(@Inject(pageToken) page) {}这意味着需要定义一个令牌并使用它来注册一个可以注入的提供者。一个人可以而且应该仍然写constructor(@Inject(pageToken) page: IPage) {}为了给参数一个类型,但类型与为参数注入的值无关。例如import {InjectionToken, NgModule} from '@angular/core';export const pageToken = new InjectionToken<IPage>('Page');@NgModule({&nbsp; providers: [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; provide: pageToken,&nbsp; &nbsp; &nbsp; useFactory: functionReturningAPage&nbsp; &nbsp; }&nbsp; ]&nbsp;}) export class // ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript