简体   繁体   English

使用aurelia.js进行jQuery自动完成的方式如何?

[英]jquery autocomplete with aurelia.js how?

I am new with aurelia and I am kinda lost to use some jquery plugins or like jquery ui. 我是aurelia的新手,我有点迷失于使用一些jquery插件或喜欢jquery ui。

I installed jquery ui with : npm install jquery jquery-ui --save 我安装了jQuery UI: npm install jquery jquery-ui --save

I am importing: 我正在导入:

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: 的HTML:

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

error: 错误:

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

As @Marc Scheib notes, you are leaving out a lot of information from you question that would help with an answer. 正如@Marc Scheib所指出的那样,您从问题中遗漏了很多信息,这些信息将有助于您给出答案。 Not knowing that information, here is one way you could get it to work. 不知道这些信息,这是使它起作用的一种方法。

Using aurelia's cli: 使用aurelia的CLI:

au new uiautocomplete

  • choose Option 2(default Typescript) 选择选项2(默认打字稿)
  • choose Option 1(yes, create project) 选择选项1(是,创建项目)
  • choose Option 1(yes, install dependencies) 选择选项1(是,安装依赖项)

au install jquery jquery-ui This will update your package.json file to include these packages, install the packages locally and update your aurelia_project/aurelia.json file with references for these projects as well. au install jquery jquery-ui这将更新您的package.json文件以包括这些软件包,在本地安装这些软件包,并使用这些项目的引用更新您的aurelia_project/aurelia.json文件。

Even though the cli tries to add the correct file to your aurelia.json file, for the autocomplete widget to work, you will need to update one of the values it puts there. 即使cli尝试将正确的文件添加到aurelia.json文件中, aurelia.json使自动完成小部件正常工作,您仍需要更新它放置在其中的值之一。 In the dependencies section there should be an entry that looks like this: 在“依赖关系”部分,应该有一个类似于以下内容的条目:

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

This needs to be updated to: 这需要更新为:

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

For your example, I created an 'attribute resource'. 对于您的示例,我创建了一个“属性资源”。 I created a file autocomplete.ts and put it in the src dir. 我创建了一个文件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});
  }
}

Then I updated app.html to contain: 然后,我将app.html更新为包含:

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

Hope this helps! 希望这可以帮助!

With jquery-ui in Aurelia my working solution has been to use a component version of jquery-ui: 在Aurelia中使用jquery-ui时,我的工作解决方案是使用jquery-ui的组件版本:

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

and then, for example: 然后,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM