如何整合angular2-useful-swiper

时间:2016-10-08 13:37:26

标签: angular swiper

    ORIGINAL EXCEPTION: ReferenceError: Swiper is not defined
ORIGINAL STACKTRACE:
ReferenceError: Swiper is not defined
    at SwiperComponent.ngOnInit (http://localhost:3000/main.bundle.js:20818:28)
    at DebugAppView._View_Home0.detectChangesInternal (Home.ngfactory.js:8303:84)
    at DebugAppView.AppView.detectChanges (http://localhost:3000/vendor.bundle.js:57290:15)
    at DebugAppView.detectChanges (http://localhost:3000/vendor.bundle.js:57396:45)

在完成所有过程后,它给了我这个错误。不明白发生了什么。此特别的GitHub回购也有很多错误。

https://www.npmjs.com/package/angular2-useful-swiper

3 个答案:

答案 0 :(得分:3)

为了实现它,请遵循以下步骤(在angular2-useful-swiper文档中也有说明):

npm install --save angular2-useful-swiper

npm install --save swiper@3.4.2

然后将js和css添加到angular-cli.json:

"styles": [
    "styles.css",
    "../node_modules/swiper/dist/css/swiper.css"        
],
"scripts": [
    "../node_modules/swiper/dist/js/swiper.js"                
],

然后,在您的应用程序中的适当级别上导入SwiperModule。

例如在app.module.ts中:

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

import { SwiperModule } from 'angular2-useful-swiper';

import { AppComponent }   from './app.component';
import { DemoComponent }   from './demo.component';

@NgModule({
    imports: [
        SwiperModule
    ],
    declarations: [
        AppComponent,
        DemoComponent
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

然后,将swiper组件插入该组件以创建一个滑块并添加内容:

<my-component>
   <swiper [config]="config">
    <div class="swiper-wrapper">
        <div class="swiper-slide">HERE GOES SELECTOR 1</div>
            <div class="swiper-slide">HERE GOES SELECTOR 2</div>
            <div class="swiper-slide">HERE GOES SELECTOR 3</div>
        </div>
        <!-- Add Pagination -->
        <div class="swiper-pagination"></div>
    </swiper>
</my-component>

为组件中的刷卡器设置配置,并将其绑定到组件的config属性,如上:

export class MyComponent implements OnInit {

    config: SwiperOptions = {
        pagination: '.swiper-pagination',
        paginationClickable: true,
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        spaceBetween: 30
    };

设置组件的高度和宽度。可以通过其名称 swiper ()来定位。

swiper{
    height: 300px;
    width: 400px;
}

按照上述步骤操作后,您将得到:

  

错误ReferenceError:未定义刷卡器

只需将Swiper 3.4.2手动添加到单个页面:

<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.2/css/swiper.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.2/js/swiper.js"></script> 

答案 1 :(得分:1)

第1步

将Swiper添加到您的单页

<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.3.1/css/swiper.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.3.1/js/swiper.js"></script>

...

<强> Plunker Example

答案 2 :(得分:0)

安装

npm install --save angular2-useful-swiper

<强>设置

将Swiper添加到您的单页

  <link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.3.1/css/swiper.min.css" rel="stylesheet">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.3.1/js/swiper.js"></script> 

之后在 app.module.ts

中声明它
@NgModule({
    imports: [
        // your other imports
        SwiperModule
    ],
    declarations: [
        // your declarations
        DemoComponent
    ],
    bootstrap: [AppComponent]
})

现在应该工作

https://www.npmjs.com/package/angular2-useful-swiper