我知道有一个模块,但我想通过其 CDN 在我的 Angular 应用程序中使用 Swiper js。
我已将脚本包含在我的
head
的 index.html
中。
然后在我想使用它的组件中,我这样声明它:
import { Component, OnInit } from '@angular/core';
import { MenuService } from '../_services/menu.service';
import { ContentfulService } from '../_services/contentful.service';
import { Entry } from 'contentful';
declare let Swiper: any;
/* and initialised in the constructor*/
constructor(
public menu: MenuService,
private contentfulService: ContentfulService
) {
new Swiper('.swiper-container', {
// Optional parameters
direction: 'vertical',
loop: true,
});
}
我没有收到任何错误,但它根本不起作用。例如,箭头也被加载,但没有绑定到它们的事件。非常感谢任何帮助。
只需更改index.html中的脚本和CSS链接https://stackblitz.com/edit/angular-d21ywf
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.2/css/swiper.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.2/js/swiper.min.js"></script>
然后将你的刷卡器带到 afterinit
import { Component ,AfterViewInit} from '@angular/core';
declare let Swiper: any;
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements AfterViewInit {
name = 'Angular';
constructor() {
}
ngAfterViewInit() {
new Swiper('.swiper-container', {
pagination: '.swiper-pagination',
paginationClickable: true,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',
autoplay: 3000,
spaceBetween: 30,
direction: 'vertical',
loop: true
});
}
}