覆盖字符串原型函数未在angular lib中编译

时间:2019-04-18 05:16:46

标签: angular angular7 angular-library ng-packagr

string-prototype.service.ts

String.prototype.equals = equals;
String.prototype.includes = includes;

interface String {
  equals: typeof equals;
  includes: typeof includes;
}

function equals(a):boolean {
  if(typeof (a) !== "string"){
    a = a.toString();
  }
  return this.toLowerCase() == a.toLowerCase();
}

function includes(searchString:string, position?:number):boolean {
  let stringS:any = searchString;
  if(typeof (stringS) !== "string"){
    searchString = stringS.toString();
  }
  let k = this.toLowerCase().match(searchString.toLowerCase());
  if (position && k) {
    return k.index === position;
  }
  return !!k;
}

现在我将ts文件导入到库模块中 angular-****。module.ts

import { DataTableComponent } from './data-table/data-table.component';
import {DragDropModule} from '@angular/cdk/drag-drop';
import { ClickOutsideModule } from 'ng4-click-outside';
import './string-prototype.service.ts';

现在,我在我的角度应用程序的 data-table.component 中使用了字符串原型函数,它可以正常工作。但是在转换为角度库并构建库后,出现以下错误:

BUILD ERROR
projects/angular-***-***/src/lib/data-table/data-table.component.ts(296,75): error TS2339: Property 'equals' does not exist on type 'String'.

Error: projects/angular-***-***/src/lib/data-table/data-table.component.ts(296,75): error TS2339: Property 'equals' does not exist on type 'String'.

    at Object.<anonymous> (D:\Projects\Angular 7\Library\angular-***-***-lib\node_modules\ng-packagr\lib\ngc\compile-source-files.js:65:19)
    at Generator.next (<anonymous>)
    at fulfilled (D:\Projects\Angular 7\Library\angular-***-***-lib\node_modules\ng-packagr\lib\ngc\compile-source-files.js:4:58)

如何解决该错误。奇怪的是,对于应用程序来说,它运行良好。但是对于Lib,它没有编译。

谢谢。

0 个答案:

没有答案