2017-09-18 110 views
0

我是新的aurelia,我有点丢失使用一些jquery插件或像jquery ui。jquery autocomplete with aurelia.js怎么样?

我安装了jQuery用户界面:npm install jquery jquery-ui --save

我输入:

import $ from 'jquery'; 
import $ from 'jquery-ui'; 


    attached(){ 
    var aTags = ["ask","always", "all", "alright", "one", "foo", "blackberry", "tweet","force9", "westerners", "sport"]; 

     $("#tags").autocomplete({ 
      source: aTags 
     }); 
    } 

HTML:

<input type='text' title='Tags' id='tags' /> 

错误:

Uncaught TypeError: $(...).autocomplete is not a function 
    at <anonymous>:3:18 
(anonymous) @ VM10535:3 
+0

尝试添加'aurelia.use .plugin(” ./脚本/ Jquery的/ jquery的-1.12。0-ui');'(替换你的版本)在'configure'方法内 – adiga

+0

请添加一些你的项目的信息:Webpack/JSPM?打字稿/ ES?如果没有任何调查,请尝试仅导入'jquery''而不是导入。 –

回答

1

由于@Marc沙伊布指出,您正在遗漏很多来自你的问题,这将有助于答案。不知道这些信息,这里有一种方法可以让它工作。

使用奥里利亚的CLI:

au new uiautocomplete

  • 选择选项2(默认打字稿)
  • 选择选项1(是的,创建项目)
  • 选择选项1(是的,安装依赖)

au install jquery jquery-ui这会更新您的package.json文件以包含这些软件包,在本地安装软件包并使用这些项目的引用更新您的aurelia_project/aurelia.json文件。

尽管cli会尝试将正确的文件添加到aurelia.json文件中,但要使自动填充小工具正常工作,您需要更新其放置在其中的某个值。在依赖关系部分应该看起来像这样的条目:

 { 
     "name": "jquery-ui", 
     "main": "ui/widget.js", 
     "path": "../node_modules/jquery-ui", 
     "resources": [] 
     } 

这需要更新为:

 { 
     "name": "jquery-ui", 
     "main": "ui/widgets/autocomplete.js", 
     "path": "../node_modules/jquery-ui", 
     "resources": [] 
     } 

对于你的榜样,我创建了一个“属性的资源”。我创建了一个文件autocomplete.ts,并将其放在src目录中。

autocomplete.ts

import { customAttribute, inject } from 'aurelia-framework' 
import * as $ from 'jquery'; 
import 'jquery-ui'; 

@customAttribute("autocomplete") 
@inject(Element) 
export class Autocomplete { 
    constructor(private element: Element) { 
    } 

    public attached() { 
    var aTags = ["ask","always", "all", "alright", "one", "foo", "blackberry", "tweet","force9", "westerners", "sport"]; 
     $(this.element).autocomplete({source: aTags}); 
    } 
} 

然后我更新app.html包含:

<template> 
    <require from="autocomplete"></require> 
    <input autocomplete type="text"> 
</template> 

希望这有助于!

1

随着奥里利亚jQuery的UI我工作的解决方案是使用jQuery的用户界面的组件版本:

npm install components-jqueryui 
jspm install npm:components-jqueryui 

,然后,例如:

import { datepicker } from 'components-jqueryui'; 
attached(){ 
    $("#datepicker").datepicker(); 
}