javascript - 无法在 ng build --prod 期间解析验证指令中的所有参数
我已经浏览了以下问题,但没有找到任何解决方案。
- Angular 4 指令错误:无法解析指令的所有参数
- 无法解析自定义指令的所有参数
我已经创建了一个自定义验证指令来验证唯一的永久链接。这段代码运行良好,但当我尝试为生产环境创建构建时,它会给我以下错误:
错误:无法解析 E:/Manish/Projects/ampleAdmin/src/app/shared/permalink-validation.directive.ts 中 UniquePermalinkValidatorDirective 的所有参数:([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
});
}
}
我在添加/编辑页面使用单一表单,所以我必须要求记录详细信息以允许在编辑页面时使用永久链接。
有什么方法可以将当前页面的详情传递给指令吗?
答案#1
给定
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 的依赖注入系统会读取构造函数参数的 类型,当在 值 空间中存在对应名称的声明时,它将使用该对应名称的值作为注入令牌。
例如,以下
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) 的一个主要用途是在像你这样的情况中,允许人们以与被装饰参数类型正交的方式注入提供者。
因此,你需要类似的东西
constructor(@Inject(pageToken) page) {}
这意味着需要定义一个令牌,并使用它来注册一个可以被注入的提供者。
人们仍然可以,而且应该,去写作
constructor(@Inject(pageToken) page: IPage) {}
为了给参数指定类型,但该类型与注入到参数中的值无关。
例如
import {InjectionToken, NgModule} from '@angular/core';
export const pageToken = new InjectionToken<IPage>('Page');
@NgModule({
providers: [
{
provide: pageToken,
useFactory: functionReturningAPage
}
]
}) export class // ...
