问题描述
我正在尝试在我的Ionic 3应用程序中使用Algolia的instantsearch.js库。这是我遵循的教程。
I am trying to use the instantsearch.js library from Algolia within my Ionic 3 app. Here is the tutorial that I followed.
这是我得到的错误
_WEBPACK_IMPORTED_MODULE_3_instantsearch_js__.instantsearch is not a function
我已经使用以下命令通过npm导入了它。
I already imported it via npm with the following command.
npm install instantsearch.js --save
SearchPage.ts
SearchPage.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ALGOLIA_CONFIG } from './../../app/app.algolia.config';
import * as instantsearch from 'instantsearch.js';
@IonicPage()
@Component({
selector: 'page-search',
templateUrl: 'search.html',
})
export class SearchPage {
search: any;
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad SearchPage');
console.log(ALGOLIA_CONFIG);
console.log(instantsearch)
this.search = instantsearch(ALGOLIA_CONFIG);
// search box widget
this.search.addWidget(
instantsearch.widgets.searchBox({
container: '#search-box',
autofocus: false,
placeholder: 'Search for actors',
poweredBy: true
})
);
// initialize hits widget
this.search.addWidget(
instantsearch.widgets.hits({
container: '#hits',
templates: {
empty: 'No results',
item: `<img src=https://image.tmdb.org/t/p/w300{{image_path}} width="50px">
<strong>Result {{objectID}}</strong>:
{{{_highlightResult.name.value}}}`
},
escapeHits: true
})
);
this.search.addWidget(
instantsearch.widgets.stats({
container: '#stats'
})
);
this.search.addWidget(
instantsearch.widgets.pagination({
container: '#pagination',
maxPages: 20,
})
);
this.search.addWidget(
instantsearch.widgets.analytics({
pushFunction: (query, state, results) => {
console.log(query)
console.log(state)
console.log(results)
}
})
);
this.search.start();
}
}
search.html
search.html
<ion-header>
<ion-navbar>
<ion-title>Search</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<h1>Test Algolia</h1>
<div id="search-box">
<!-- SearchBox widget will appear here -->
</div>
<div id="stats">
<!-- stats widget will appear here -->
</div>
<div id="hits">
<!-- Hits widget will appear here -->
</div>
<div id="pagination">
<!-- pagination widget will appear here -->
</div>
</ion-content>
我尝试不用/ es导入它。
I have tried importing it without /es as well.
这是console.log(即时搜索)的输出
Here is the output of the console.log(instantsearch)
{__esModule: true, default: ƒ}
default
:
ƒ Factory()
createQueryString
:
ƒ (state, options)
version
:
"2.2.1"
connectors
:
[Exception: ReferenceError: You can't access to 'instantsearch.connectors' directly from the ES6 build. Import the connectors this way 'import {connectSearchBox} from "instantsearch.js/connectors"' at Function.get (http://localhost:8100/build/0.js:9985:11) at Function.remoteFunction (<anonymous>:2:14)]
length
:
0
name
:
"Factory"
prototype
:
EventEmitter {constructor: ƒ, addWidget: ƒ, start: ƒ, createURL: ƒ, _render: ƒ, …}
widgets
:
[Exception: ReferenceError: You can't access to 'instantsearch.widgets' directly from the ES6 build. Import the widgets this way 'import {SearchBox} from "instantsearch.js/widgets"' at Function.get (http://localhost:8100/build/0.js:9979:11) at Function.remoteFunction (<anonymous>:2:14)]
get connectors
:
ƒ get()
get widgets
:
ƒ get()
__proto__
:
ƒ InstantSearch(_ref)
[[FunctionLocation]]
:
to-factory.js:5
[[Scopes]]
:
Scopes[3]
__esModule
:
true
__proto__
:
Object
推荐答案
您可以尝试以这种方式导入即时搜索:
Can you try to import instantsearch that way:
import instantsearch from 'instantsearch.js/es';
import { searchBox } from 'instantsearch.js/es/widgets';
这样它将导入唯一需要的模块,它应该适用于Angular2 / Ionic应用程序。
That way it will import the only needed modules and it should work into an Angular2/Ionic application.
如果您想了解更多关于如何在Angular2 / Ionic应用程序中集成instantsearch.js的信息,您应该阅读我们的集成指南:
If you want to learn more on how to integrate instantsearch.js inside an Angular2/Ionic application you should read our integration guide over here: https://community.algolia.com/instantsearch.js/v2/guides/angular-integration.html
我们也在积极发布一个角度 - 实时搜索库,这将在未来几个月内发生。如果您有兴趣加入测试计划,请填写此文档:
We are also working actively to ship an angular-instansearch library, this will happen in the next months. If you are interested into joining the beta program can you fill this document please: https://docs.google.com/forms/u/2/d/e/1FAIpQLSeqEHS0VjnNlaKMp7Cm0y7pRe2pLz-39y3-cEAs5o-H0-HD6A/viewform?usp=send_form
这篇关于爱奥尼亚的Algolia Instantsearch.js 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!