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

标签 javascript angular typescript angular-directive angular8

我已经完成了以下问题,但没有找到任何解决方案。

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

ERROR in : Can't resolve all parameters for UniquePermalinkValidatorDirective in E:/Manish/Projects/ampleAdmin/src/app/shared/permalink-validation.directive.ts: ([object Object], ?).

permalink-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);
  }
}

page.component.ts

import { Component, OnInit, TemplateRef } from '@angular/core';
import * as _ from 'lodash';
import { NotifierService } from 'angular-notifier';
import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { IPage } from 'src/app/client-schema';
import { Utils } from 'src/app/shared/utils';
import { PageService } from 'src/app/services/page.service';
import { UniquePermalinkValidator } from 'src/app/shared/permalink-validation.directive';

@Component({
  selector: 'app-page',
  templateUrl: './page.component.html',
  styleUrls: ['./page.component.css']
})
export class PageComponent implements OnInit {

  private notifier: NotifierService;

  pageForm: FormGroup;
  pageDetail: IPage;

  isAddFold = false;
  isEditFold = false;
  editFoldIndex = -1;

  constructor(
    private pageService: PageService,
    private notifierService: NotifierService,
    private modalService: BsModalService,
    private formBuilder: FormBuilder,
  ) {
    this.notifier = notifierService;
  }

  initPageForm() {
    this.pageForm = this.formBuilder.group({
      name: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(250)]],
      permalink: ['', [Validators.required], UniquePermalinkValidator(this.pageService, this.pageDetail)],
      folds: [
        []
      ],
      data: null,
      status: true
    });
  }
}

我对添加/编辑页面使用单一表单,因此我必须要求记录详细信息以允许在编辑页面时使用永久链接。

有什么方法可以将当前页面的详细信息传递给指令吗?

最佳答案

给定

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

给定

@Directive({
  selector: '[appUniquePermalink]',
  providers: [{ provide: NG_ASYNC_VALIDATORS, useExisting: UniquePermalinkValidatorDirective, multi: true }]
})
export class UniquePermalinkValidatorDirective implements AsyncValidator {    
  constructor(private pageService: PageService, private page: IPage) {}
  // ...
}

并且鉴于 IPage

定义
export interface IPage {
  id: number;
  // ...
}

然后 UniquePermalinkValidatorDirective 将无法按定义工作,以所述方式失败。

接口(interface) 仅在类型 空间中定义某些内容,而不是在 空间中,因此没有任何运行时表现形式。这意味着它不能用于值位置。

本质上,Angular 的依赖注入(inject)系统读取构造函数参数的类型,当 空间中有相应命名的声明时,它将使用相应命名的声明值作为注入(inject) token 。

例如下面的

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

@Injectable() export class Service {
    constructor(http: Http) {}
}

也可以这样写

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

export class Service {
    constructor(@Inject(Http) http: ThisTypeIsArbitraryWithRespectToInjection) {}
}

意思是一样的

注意 Http 如何作为参数传递给 Inject。但是 Inject(IPage),其中 IPage 是一个 interface,格式不正确。

@Inject(ProviderToken) 的主要目的是允许在像您这样的情况下,将提供者正交注入(inject)到装饰参数的类型。

因此,你需要类似的东西

constructor(@Inject(pageToken) page) {}

这意味着需要定义一个 token 并使用它来注册一个可以注入(inject)的提供者。

一个人可以,也应该,仍然写

constructor(@Inject(pageToken) page: IPage) {}

为了给参数一个类型,但类型与为参数注入(inject)的值无关。

例如

import {InjectionToken, NgModule} from '@angular/core';

export const pageToken = new InjectionToken<IPage>('Page');

@NgModule({
  providers: [
    {
      provide: pageToken,
      useFactory: functionReturningAPage
    }
  ]
 }) export class // ...

关于javascript - 在 ng build --prod 期间无法解析验证指令中的所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56746850/

相关文章:

javascript - 在javascript中通过另一个数组更改对象数组的值的更好方法

javascript - 使用 Angular 添加事件状态到列表项

javascript - 如何使用 Jasmine 测试表单

angular - 在 Angular 组件中配置 Revolution Slider

基于以前的 Form 控件的 Angular 5 动态表单

javascript - event.preventDefault() 或 event.stopPropagation() 不适用于 keyUp 事件

typescript :类型 ConstructorParameters 不接受泛型

javascript - Tablesorter:对捷克字母表进行排序

javascript - document.domain 相同但仍然出现同源错误

javascript - Angular 2+ : change detection, 属性更改和 View 更新之间的状态