2

我在我的 Angular 应用程序中创建了一个自定义搜索框小部件,这在开发模式下运行良好。在我构建项目(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 导入中,from 后面有 3 个小点。悬停时,出现以下消息:

[ts] 找不到模块“instantsearch.js/es/connectors”的声明文件。'/node_modules/instantsearch.js/es/connectors/index.js' 隐含了一个 'any' 类型。

在节点模块中查看 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;
}
4

1 回答 1

1

解决。就我而言,我很糟糕,我在这里错过了一部分文档:

https://community.algolia.com/angular-instantsearch/guides/customize-widgets.html - 第 3 节。从状态渲染

需要将以下内容添加到自定义小部件中。

state: {
   refine: (value: string) => void;
}
于 2018-12-04T10:03:39.790 回答