我在Angular应用程序中创建了一个自定义的搜索框小部件,这在开发模式下运行良好。当我构建项目(ng-build-prod(时,我收到以下错误:
类型"object"上不存在属性"refine"。
CustomWidget.ts
import { Component, Inject, forwardRef, OnInit } from '@angular/core';
import { BaseWidget, NgAisInstantSearch, } from 'angular-instantsearch';
import { connectSearchBox } from 'instantsearch.js/es/connectors';
@Component({
selector: 'app-search-box',
templateUrl: './search-box.component.html',
styleUrls: ['./search-box.component.css']
})
export class SearchBoxComponent extends BaseWidget {
constructor(
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchParent
) {
super('SearchBox');
}
public ngOnInit() {
this.createWidget(connectSearchBox);
super.ngOnInit();
}
}
自定义小部件。。html
<div class="relative p-4 px-8">
<input type="search" (input)="state.refine($event.target.value)">
</div>
注意-在connectSearchBox导入上,从后面有3个小点。悬停时,出现以下消息:
[ts]找不到模块的声明文件'instantsearch.js/es/connectors'。'/node_modules/instantsearch.js/es/connectors/index.js'隐式具有"任意"类型。
在节点模块中查找instantSearch.js/es/connectors,然后是每个组件类型的其他文件夹,然后是的外观,例如"搜索框">
BaseWidget.d.ts
import { OnDestroy, OnInit } from '@angular/core';
export declare class Widget {
init: () => void;
getConfiguration: () => object;
render: (params: {
templatesConfig: object;
state: object;
results: {}[];
createURL: (value: any) => string;
instantSearchInstance: object;
}) => void;
dispose: (params: {
helper: object;
state: object;
}) => object | void;
}
export declare type Connector = (renderFn: (state: object, isFirstRendering: boolean) => void, unmountFn: () => void) => (widgetOptions?: object) => Widget;
export declare class BaseWidget implements OnInit, OnDestroy {
instantSearchParent: any;
autoHideContainer?: boolean;
widget?: Widget;
state?: object;
cx: Function;
constructor(widgetName: string);
createWidget(connector: Connector, options?: object): void;
ngOnInit(): void;
ngOnDestroy(): void;
updateState: (state: {}, isFirstRendering: boolean) => void | Promise<void>;
getItemClass(item: {
isRefined?: boolean;
}): any;
}
已解决。就我而言,这太可怕了,我错过了这里的一节文档:
https://community.algolia.com/angular-instantsearch/guides/customize-widgets.html-第3节。从状态渲染
以下内容需要添加到自定义小部件中。
state: {
refine: (value: string) => void;
}