您必须使用如下链接您的myapp.js
文件type="module"
<script type="module" src="myapp.js"></script>
然后myapp.js
你必须js-search
使用相对路径导入,node_modules
因为你没有使用任何像 webpack 这样的捆绑器。在您的myapp.js
文件中,您可以使用js-search
如下所示
import * as JsSearch from './node_modules/js-search/dist/esm/js-search.js';
var theGreatGatsby = {
isbn: '9781597226769',
title: 'The Great Gatsby',
author: {
name: 'F. Scott Fitzgerald'
},
tags: ['book', 'inspirational']
};
var theDaVinciCode = {
isbn: '0307474275',
title: 'The DaVinci Code',
author: {
name: 'Dan Brown'
},
tags: ['book', 'mystery']
};
var angelsAndDemons = {
isbn: '074349346X',
title: 'Angels & Demons',
author: {
name: 'Dan Brown',
},
tags: ['book', 'mystery']
};
var search = new JsSearch.Search('isbn');
search.addIndex('title');
search.addIndex(['author', 'name']);
search.addIndex('tags')
search.addDocuments([theGreatGatsby, theDaVinciCode, angelsAndDemons]);
console.log(search.search('The')); // [theGreatGatsby, theDaVinciCode]
console.log(search.search('scott')); // [theGreatGatsby]
console.log(search.search('dan')); // [angelsAndDemons, theDaVinciCode]
console.log(search.search('mystery')); // [angelsAndDemons, theDaVinciCode]