如何在Angular2中使用owlcarousel?

时间:2017-03-15 21:22:22

标签: angular owl-carousel

我是angular2的新手,现在我正在尝试使用猫头鹰旋转木马

我找到了这个教程,但它给了我很多错误,我可以弄清楚什么是问题,所以有人可以再解释一下。谢谢

How to use owl-carousel in Angular2?

这是我的代码

Home Component html

<owl-carousel [options]="{navigation: false, pagination: true, rewindNav : false}">
   <div *ngFor="let img of images">
     <img src="http://lorempixel.com/400/200/{{img}}"/>
   </div>
</owl-carousel>

Home Component ts

import { Component, OnInit } from '@angular/core';
import { OwlCarousel } from '../owl-carousel.component';


@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

constructor() { }

ngOnInit() {
 }
 images: Array<string> = ['sports', 'abstract', 'people', 'transport',      'city', 'technics', 'nightlife', 'animals'];
}

我也在app.module.ts中导入

owl.component.ts

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 OwlCarousel {
 @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;
  }
 }

错误

  

无法找到模块'jquery'。   找不到模块:错误:无法解析'owl-carousel'

1 个答案:

答案 0 :(得分:-1)

我建议使用类似ng2-bootstrap的内容,而不是使用jQuery

长话短说:

  1. 安装

    npm install ng2-bootstrap
    
  2. 导入轮播模块:

    import { CarouselModule } from 'ng2-bootstrap';
    
    @NgModule({
       imports:      [ CarouselModule, ...],
       declarations: [ AppComponent, ... ],
       bootstrap:    [ AppComponent ]
    }) 
    
  3. 使用它:

    <carousel>
       <slide>
         <img src="https://i.ytimg.com/vi/qh7LLydY8eo/maxresdefault.jpg" alt="First slide">
       </slide>
       <slide>
         <img src="http://coolwildlife.com/wp-content/uploads/galleries/post-3004/Fox%20Picture%20003.jpg" alt="Second slide">
       </slide>
       <slide>
         <img src="https://wallpaperbrowse.com/media/images/81.jpg" alt="Third slide">
       </slide>
     </carousel>
    
  4. 这是一个有效的进展:

    https://plnkr.co/edit/1NMmcUW1KDnxApnVUz3P?p=preview