Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate angular2-useful-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)

It is giving me this error after doing all process. Dont understand what happening. Also the GitHub repo in this particular has a lot of errors.

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

like image 933
Abhishek Avatar asked Jan 06 '23 06:01

Abhishek


1 Answers

In order to achieve it, follow the following steps (explained also in angular2-useful-swiper documentation):

npm install --save angular2-useful-swiper

npm install --save [email protected]

then add the js and css to angular-cli.json:

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

Afterwards, import the SwiperModule at the appropriate level in your app.

For example in 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 { }

Then, insert the swiper component to the component to create a slider and add the content:

<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>

Set the config for the swiper in the component and bind it to the component config property as above:

export class MyComponent implements OnInit {

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

Set the height and width of the component. It can be targeted by its name, swiper.

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

After following the previous steps, if you get:

ERROR ReferenceError: Swiper is not defined

Just add manually the Swiper 3.4.2 to the single page:

<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> 
like image 121
Bárbara Peres Avatar answered Jan 13 '23 15:01

Bárbara Peres