【问题标题】:How to use jquery plugins (like Owl Carousel 2) with angular 4 in typescript class?如何在 typescript 类中使用 angular 4 的 jquery 插件(如 Owl Carousel 2)?
【发布时间】: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


    【解决方案1】:

    我想知道是否正确包含了您的 OwlCarousel。 在 npm (here) 上有一个 OwlCarousel 的预构建模块,试试看它是否适合你。

    此外,这篇文章是关于 Angular 4 的,因此您应该将标签更改为 angular 而不是 angularjs

    【讨论】:

    • 感谢阿德里安。我尝试用 nmp 安装它并得到了这个。为什么不推荐使用? .npm [On.Store.UI] install ng2-owl-carousel --save npm 警告已弃用 ng2-owl-carousel@0.1.15:警告:此项目已重命名为 angular-owl-carousel。改为使用 npm install --save angular-owl-carousel 安装。 angular-quickstart@1.0.0 L:\ESaleStore\On.Store.UI\src\On.Store.UI +-- UNMET PEER DEPENDENCY @angular/compiler@4.0.1 +-- UNMET PEER DEPENDENCY @angular/core@ 4.0.1 +-- UNMET 对等依赖 @angular/platform-b​​rowser@4.0.1 +-- UNMET 对等依赖 @angular/platform-b​​rowser-
    • dynamic@4.0.1 +-- ng2-owl-carousel@0.1.15 -- protractor@4.0.14 -- @types/node@6.0.78 npm WARN 可选跳过可选依赖:fsevents@^1.0。 0 (node_modules\chokidar\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.1 不支持的平台: 想要 {"os":"darwin","arch":"any"} (当前: {" os":"win32","arch":"x64"})
    • npm WARN enoent ENOENT:没有这样的文件或目录,打开 'L:\ESaleStore\On.Store.UI\src\On.Store.UI\node_modules\Cartjs\package.json' npm WARN @angular/upgrade@2.1.2 需要 @angular/core@2.1.2 的对等点,但没有安装。 npm WARN @angular/upgrade@2.1.2 需要 @angular/compiler@2.1.2 的对等点,但没有安装。 npm WARN @angular/upgrade@2.1.2 需要 @angular/platform-b​​rowser@2.1.2 的对等点,但没有安装。
    • npm WARN @angular/upgrade@2.1.2 需要 @angular/platform-b​​rowser-dynamic@2.1.2 的对等点,但没有安装。 [On.Store.UI] install ng2-owl-carousel --save 成功完成
    • 所以看起来这里有几个问题,但大多只是按照 npm 的说明进行操作。使用: npm install --save angular-owl-carousel 进行安装。尝试通过 npm 安装 Angular。另外,你在使用 angular-cli 吗?依赖问题看起来像你的 Angular 安装不是由 npm 管理的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2017-10-11
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多