【问题标题】:Owl carousel in Angular 7Angular 7中的猫头鹰旋转木马
【发布时间】:2018-12-09 12:24:55
【问题描述】:

我在 Angular 7 中使用 Owl 轮播。 首先,我使用

安装
 npm install ngx-owl-carousel owl.carousel jquery --save

然后添加

 "scripts": ["./node_modules/jquery/dist/jquery.min.js",
 "./node_modules/owl.carousel/dist/owl.carousel.min.js"]

在 angular.json 文件中 并添加

import { OwlModule } from 'ngx-owl-carousel';
imports: [
 BrowserModule,
 OwlModule
]

在 app.module.ts 文件中 然后我用了

public ngOnInit()
{
 /*----------------------------
        Slideshow
    ------------------------------ */
    $('.slideshow').owlCarousel({
      items: 6,
      autoPlay: 3000,
      singleItem: true,
      navigation: true,
      navigationText: ['<i class="fa fa-chevron-left"></i>', '<i class="fa fa-chevron-right"></i>'],
      pagination: true
  });
}

在 app.component.ts 文件中,但我有错误

core.js:14576 ERROR TypeError: jquery__WEBPACK_IMPORTED_MODULE_2__(...).owlCarousel 不是函数

我错在哪里?请帮我。 谢谢。

【问题讨论】:

标签: angular owl-carousel


【解决方案1】:

导入 OwlModule 后,您可以在 Angular 模板中使用其导出的组件,如下所示(示例取自 github repo):

   <owl-carousel
     [options]="{items: 3, dots: false, navigation: false}"
     <!-- If images array is dynamically changing pass this array to [items] 
     input -->
    [items]="images"
    <!-- classes to be attached along with owl-carousel class -->
      [carouselClasses]="['owl-theme', 'row', 'sliding']">
    <div class="item" *ngFor="let image of images;let i = index">
       <div class="thumbnail-image" [ngStyle]="{'background': 'url(abc.jpg) 
       no-repeat scroll center center / 80px 80px'}"></div>
    </div>
   </owl-carousel>

【讨论】:

    【解决方案2】:

    安装 jquery 和 owl carousel

    npm install --save jquery
    
    npm install --save owl.carousel
    

    在需要使用owl carousel的组件ts文件中导入库

    import * as $ from 'jquery';
    import "owl.carousel/dist/owl.carousel.min"
    declare var $:any;
    

    在全局样式表-styles.css中导入猫头鹰轮播样式表

    @import "~owl.carousel/dist/assets/owl.carousel.min";
    

    【讨论】:

    猜你喜欢
    • 2022-06-28
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多