我正在尝试使用一个第三方库(自动数字)和类型记录+ Angular2。
我试过以下几种方法
/// <reference path="/typings/main/ambient/jquery/index.d.ts" />
declare interface JQuery {
autoNumeric: any;
}在init上,我正在尝试使用它。
ngOnInit() {
$('selector').autoNumeric('init', {options});
}但是,当我编译时,我会得到以下错误。
错误TS2339:类型'JQuery‘上不存在属性'autoNumeric’
我没有走错路。如能提供任何帮助,将不胜感激。
注意事项
我已经将autonumeric.js包含在index.html中了
发布于 2016-04-18 22:29:49
我做的和你完全一样,而且效果很好。您应该创建定义文件来扩展JQuery接口,并将对*.d.ts文件的引用添加到main.ts
/// <reference path="./typings/declarations.d.ts" />其中declarations.d.ts包含:
declare interface JQuery {
autoNumeric: any;
}发布于 2016-04-19 01:57:24
在我的例子中,我只是简单地将它声明为jQuery为any
import {Component, ElementRef, OnInit} from 'angular2/core';
declare var jQuery:any;
@Component({
selector: 'jquery-integration',
templateUrl: './components/jquery-integration/jquery-integration.html'
})
export class JqueryIntegration implements OnInit {
elementRef: ElementRef;
constructor(elementRef: ElementRef) {
this.elementRef = elementRef;
}
ngOnInit() {
jQuery(this.elementRef.nativeElement).draggable({containment:'#draggable-parent'});
}
}下面是完整的示例:http://www.syntaxsuccess.com/viewarticle/using-jquery-with-angular-2.0
https://stackoverflow.com/questions/36705353
复制相似问题