令人难以置信的是,在整个互联网上没有一个简单的例子来解决这个问题这是我的镜头(vanilla JS)
const algoliaAppId = 'APP_ID';
const algoliaApiKey = 'API_KEY';
const algoliaIndex = 'instant_search';
const search = instantsearch({
indexName: algoliaIndex,
algoliasearch(algoliaAppId, algoliaApiKey),
routing: {
router: instantsearch.routers.history({
createURL({ qsModule, location, routeState }) {
// current search params
const indexState = routeState[algoliaIndex] || {};
const { origin, pathname, hash, search } = location;
// grab current query string and convert to object
const queryParameters = qsModule.parse(search.slice(1)) || {};
// if there is an active search
if (Object.keys(indexState).length ){
// merge the search params with the current query params
Object.assign(queryParameters, routeState);
}else{
// remove the search params
delete queryParameters[algoliaIndex];
}
let queryString = qsModule.stringify(queryParameters);
if(queryString.length){
queryString = `?${queryString}`;
}
return `${origin}${pathname}${queryString}${hash}`;
},
})
}
});
search.start();