我已经在Angular Application中创建了一个自定义搜索框小部件,并且在开发模式下可以正常工作。建立项目(ng build --prod)的那一刻,我收到以下错误:
类型“对象”上不存在属性“优化”。
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();
}
}
CustomWidget..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;
}
答案 0 :(得分:0)
已解决。我很糟糕,我在这里错过了一部分文档:
https://community.algolia.com/angular-instantsearch/guides/customize-widgets.html-第3节。从状态渲染
以下内容需要添加到自定义小部件中。
state: {
refine: (value: string) => void;
}