3

我正在尝试在我的 Angular 2 应用程序中实现 owl-carousel。

我按照这个例子如何在Angular2中使用owl-carousel?它实际上适用于我的项目是异步更改(ng-​​content async change)的唯一问题。

当我的 owl-courosel 的内容发生变化(推荐者或批评者)时,通过在 plnkr 上实施解决方案,插件不会重新加载。所以我只看到一个项目列表,但它们不会滚动。

所以我有 nps-comments.component.html 调用轮播组件的地方:

<section class="purchasers comments" *ngIf="comments.promoters.length || comments.detractors.length">
  <carousel class="promoters" *ngIf="comments.promoters.length" [options]="{ items: 1 }">
    <p *ngFor="let promoter of comments.promoters">{{promoter}}</p>
  </carousel>
  <carousel class="detractors" *ngIf="comments.detractors.length" [options]="{ items: 1 }">
    <p *ngFor="let detractor of comments.detractors">{{detractor}}</p>
  </carousel>
</section>

然后是实际的 carousel.component.ts

import {
  Component,
  Input,
  ElementRef
} from '@angular/core';

import 'jquery';
import 'owl-carousel';

@Component({
  moduleId: module.id,
  selector: 'carousel',
  templateUrl: 'carousel.component.html',
  styleUrls: ['carousel.component.css']
})

export class CarouselComponent {
  @Input() options: Object;

  private $carouselElement: any;

  private defaultOptions: Object = {};

  constructor(private el: ElementRef) { }

  ngAfterViewInit() {
    for (let key in this.options) {
      if (this.options.hasOwnProperty(key)) {
        this.defaultOptions[key] = this.options[key];
      }
    }

    let outerHtmlElement: any = $(this.el.nativeElement);
    this.$carouselElement = outerHtmlElement.find('.owl-carousel').owlCarousel(this.defaultOptions);
  }

  ngOnDestroy() {
    this.$carouselElement.trigger('destroy.owl.carousel');
    this.$carouselElement = null;
  }
}

这是 carousel.component.html:

<div class="owl-carousel owl-theme">
  <ng-content></ng-content>
</div>

任何帮助将非常感激。谢谢你。

4

1 回答 1

2

我正在分享我将 owl owl.carousel@2.1.4 与 angular 2.0.0 + webpack 一起使用的解决方法。

首先,您需要通过 npm 或类似方式安装上述^ 软件包。

然后 --> npm install imports-loader

(对于在组件中使用 owl ,否则您将获得未定义的函数。由于第三方模块依赖于全局变量,如 $ 或 this 是窗口对象。)。

我正在使用 webpack,所以本节适用于 webpack 用户:

进口装载机如下:

{test: /bootstrap\/dist\/js\/umd\//, loader: 'imports?jQuery=jquery'}

你也可以使用 jQuery 作为(webpack):

var ProvidePlugin = require('webpack/lib/ProvidePlugin');

用作插件:

plugins: [
       new webpack.ProvidePlugin({
            jQuery: 'jquery',
            $: 'jquery',
            jquery: 'jquery',
            'window.jQuery': 'jquery'
        })
    ]

对于图像加载器:

{
   test: /\.(png|jpe?g|gif|ico)$/,
   loader: 'file?name=public/img/[name].[hash].[ext]'
}

*public/img -- 图片文件夹

CSS 加载器:

{
   test: /\.css$/,
   include: helpers.root('src', 'app'),
   loader: 'raw'
}

vendor.js 文件应导入以下内容:

import 'jquery';
import 'owl.carousel';
import 'owl.carousel/dist/assets/owl.carousel.min.css';

请注意owl.carousel 2 仍然使用andSelf () 已弃用的jQuery 函数,因此我们需要用新版本的addBack () 替换它们。

转到 owl 包 dist/owl.carousel.js 中的 node_modules 文件夹:将所有出现的andSelf () 替换为 --> addBack ()。

现在是角度 2 部分:

猫头鹰-carousel.ts:

import {Component} from '@angular/core';

@Component({
    selector: 'carousel',
    templateUrl: 'carousel.component.html',
    styleUrls: ['carousel.css']
})
export class Carousel {
    images: Array<string> = new Array(10);
    baseUrl: string = './../../../../public/img/650x350/';
}

carousel.component.ts:

import { Component, Input, ElementRef, AfterViewInit, OnDestroy } from '@angular/core';

@Component({
    selector: 'owl-carousel',
    template: `<ng-content></ng-content>`
})
export class OwlCarousel implements OnDestroy, AfterViewInit{
    @Input() options: Object;

    $owlElement: any;

    defaultOptions: Object = {};

    constructor(private el: ElementRef) {}

    ngAfterViewInit() {
        for (var key in this.options) {
            this.defaultOptions[key] = this.options[key];
        }
        var temp :any;
        temp = $(this.el.nativeElement);

        this.$owlElement = temp.owlCarousel(this.defaultOptions);
    }

    ngOnDestroy() {
        this.$owlElement.data('owlCarousel').destroy();
        this.$owlElement = null;
    }
}

carousel.component.html:

<owl-carousel class="owl-carousel"[options]="{navigation: true, pagination: true, rewindNav : true, items:2, autoplayHoverPause: true, URLhashListener:true}">
    <div class="owl-stage" *ngFor="let img of images; let i=index">
        <div class="owl-item">
            <a href="#"><img src="{{baseUrl}}{{i+1}}.png"/></a>
        </div>
    </div>
</owl-carousel>

确保引导 app.module 中的所有内容:

import { NgModule } from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { AppComponent } from './app.component';
import  {OwlCarousel} from './components/carousel/carousel.component';
import  {Carousel} from './components/carousel/owl-carousel';


@NgModule({
    imports: [
        BrowserModule,
        NgbModule,
    ],
    declarations: [
        AppComponent,
        OwlCarousel,
        Carousel
    ],
    providers: [appRoutingProviders],
    bootstrap: [ AppComponent ]
})
export class AppModule { }

现在您可以在整个应用程序的 template/templateUrl 部分中使用指令/组件,无需导入任何内容。

请按照上述操作,因为所有步骤都是完成 angular 2.0.0 最终版本和 owl.carousel 2.1.4 版本之间的集成所必需的。

于 2016-09-25T19:15:15.890 回答