【发布时间】:2023-03-16 22:35:01
【问题描述】:
我正在尝试使用带有 angular 4 typescript 类的 jQuery 插件,但不幸的是我没有成功实现我的目标。
我尝试了很多方法,但 Angular 4 html 组件视图不允许 jQuery 就绪/其他事件,那么我该如何使用它们?
我目前已经创建了一个组件 typescript 类,并尝试在其中使用 jquery 函数但无法成功。
请在下面查看我的代码:
import { Component, Input, ElementRef, HostBinding } from '@angular/core';
import * as $ from 'jquery';
import 'owl-carousel';
@Component({
selector: 'owl-carousel',
template: `<ng-content></ng-content>`
})
export class OwlCarouselComponent {
@HostBinding('class') defaultClass = 'owl-carousel';
@Input() options: Object;
$owlElement: any;
defaultOptions: any = {};
constructor(private el: ElementRef) {}
ngAfterViewInit() {
// use default - empty
// for (var key in this.options) {
// this.defaultOptions[key] = this.options[key];
// }
this.$owlElement = $(this.el.nativeElement).owlCarousel(this.defaultOptions);
}
ngOnDestroy() {
this.$owlElement.data('owlCarousel').destroy();
this.$owlElement = null;
}
}
Visual Studio 编译器在这一行显示红色错误。
this.$owlElement = $(this.el.nativeElement).owlCarousel(this.defaultOptions);
错误信息是:
严重性代码描述项目文件行抑制状态
错误 TS2339 构建:“JQuery”类型上不存在属性“owlCarousel”。 On.Store.UI L:\ESaleStore\On.Store.UI\src\On.Store.UI\wwwroot\app\shared\OwlCarousel.component.ts 24
错误 TS2339 属性“owlCarousel”在类型“JQuery”上不存在。 On.Store.UI (tsconfig 项目) L:\ESaleStore\On.Store.UI\src\On.Store.UI\wwwroot\app\shared\OwlCarousel.component.ts 24 活动
这是我的 tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"compileOnSave": true,
"exclude": [
"node_modules"
]
}
这是我的 systemjs.config.js 文件
`/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
jquery: 'npm:@angular/jquery/dist/jquery.min.js',
'owl-carousel': 'npm:@angular/owl.carousel/dist/owl.carousel.min.js'
},
meta: {
'owl-carousel': {
deps: ['jquery']
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
`
请指导我。谢谢你
【问题讨论】:
标签: jquery angular typescript typescript-typings