Jquery 将侦探犬导入Angular 2 CLI项目
我正在尝试将Bloodhound.js实现到使用Angular 2 CLI的Angular 2项目中。目前,我让jQuery通过以下方法进行工作:Jquery 将侦探犬导入Angular 2 CLI项目,jquery,angular,typeahead,bloodhound,Jquery,Angular,Typeahead,Bloodhound,我正在尝试将Bloodhound.js实现到使用Angular 2 CLI的Angular 2项目中。目前,我让jQuery通过以下方法进行工作: npm安装jquery--保存 npm安装@types/jquery--保存开发 然后将“../node_modules/jquery/dist/jquery.min.js”添加到angular-cli.json中的脚本数组中 我对angular-cli.json中的Bloodhound.js也做了同样的操作,包括: “./node_modules/
有没有办法直接导入.js文件或在本地添加导入?以下是我的解决方案。希望有人能帮上忙 使用以下命令安装typehead.js类型定义
npm install --save @types/typeahead
将以下文件也添加到angular-cli.json文件中
"assets/js/bloodhound.min.js",
"assets/js/typeahead.bundle.min.js"
在组件的OnInit()方法中执行以下操作
const suggestions = [{
value: 'string1'
}, {
value: 'string2'
}, {
value: 'string3'
}, {
value: 'string4'
}, {
value: 'string5'
}];
let bloodhoundSuggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
sufficient: 3,
local: this.suggestions
});
$('#tagsInput .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'suggestions',
displayKey: 'value',
source: bloodhoundSuggestions
});
我也安装了jQuery
请确保将以下内容添加到app.component.ts
import * as $ from 'jquery';
并确保在组件文件中导入以下内容
declare const Bloodhound;
declare const $;
组件Html文件
<div id="tagsInput">
<input class="typeahead" type="text" placeholder="States of USA">
</div>
您找到解决方案了吗