【问题标题】:Unable to initialise Swiper in angular2 using npm无法使用 npm 在 angular2 中初始化 Swiper
【发布时间】:2017-09-27 14:58:28
【问题描述】:

我正在尝试在我的 angular2 + npm 应用程序中使用 swiper 库,但在尝试使用它时出现错误

错误是:

ERROR in vendor.95f9dda80938ded736ad.bundle.js from UglifyJs
Unexpected token: name (class_SwiperClass) [vendor.95f9dda80938ded736ad.bundle.js:16626,6]

我已将它包含在我的 package.json 中,并在我的组件中按如下方式使用它:

import Swiper from 'swiper/src/components/core/core-class';


  ngAfterViewInit() {
    let mySwiper : any = new Swiper('swiper-container', {
      autoplay: 1000,
      autoplayDisableOnInteraction : false,
      speed: 400,
      spaceBetween: 0,
      pagination : ".swiper-pagination"
    });
  }

我的angular-cli配置如下:

 {
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "prefix": "app",
      "styles": [
        "../node_modules/swiper/dist/css/swiper.css"
      ],
      "scripts": [
        "../node_modules/swiper/dist/js/swiper.js"
      ]
    }
  ]
}

【问题讨论】:

    标签: javascript angular typescript npm swiper


    【解决方案1】:

    我以这种方式在我的应用中使用 Swiper:

    1. 您必须使用 npm 安装 swiper --> npm install swiper@3.4.2 --save
    2. 您必须在您的 agular-cli.json 中添加: “样式”:[“../node_modules/swiper/dist/css/swiper.min.css”], “脚本”:[“../node_modules/swiper/dist/js/swiper.min.js”]
    3. 您必须在您的 typings.d.ts 中添加 --> 声明 var Swiper: any;
    4. 现在在您的 component.ts 中删除 import Swiper 并且您的滑块可以工作了!告诉我!

    【讨论】:

    • 你的意思是你必须在你的 typings.d.ts 中添加 --> 声明 var Swiper: any;我没有任何typings.d.ts
    • 当您使用 Javascript 库时,您必须为您的库下载类型(在本例中为 Swiper)。如果你需要使用的全局库没有全局类型,你也可以在 src/typings.d.ts 中手动声明为 any: declare var libraryName: any; (github.com/angular/angular-cli/wiki/stories-global-scripts)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    相关资源
    最近更新 更多