【问题标题】:Bootstrap 4 Carousel working in Angular 2? Error: Property 'carousel' does not exist on type 'JQuery'Bootstrap 4 Carousel 在 Angular 2 中工作?错误:“JQuery”类型上不存在属性“轮播”
【发布时间】:2017-06-26 11:08:01
【问题描述】:

我正在尝试让 Bootstrap 4 轮播在我的 Angular 2 应用程序中工作。具体来说,我正在尝试复制完成here 的全角效果。我已经到了最后一点,我必须将元素挂在轮播上:

$('.carousel').carousel({
  interval: 6000,
  pause: "false"
});

当我尝试编译时,出现以下错误:

错误:“JQuery”类型上不存在属性“carousel”

我知道有 ng-bootstrap 组件,但我无法以这种方式让那里的轮播完全全宽。

这是我的代码(Typescript、CSS、HTML):

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';

@Component({
  selector: 'app-home-carousel',
  templateUrl: './home-carousel.component.html',
  styleUrls: ['./home-carousel.component.css']
})
export class HomeCarouselComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  setupCarousel(): void {
    var $item = $('.carousel .item'); 
    var $wHeight = $(window).height();
    $item.eq(0).addClass('active');
    $item.height('400px'); 
    $item.addClass('full-screen');

    $('.carousel img').each(function() {
      var $src = $(this).attr('src');
      var $color = $(this).attr('data-color');
      $(this).parent().css({
        'background-image' : 'url(' + $src + ')',
        'background-color' : $color
      });
      $(this).remove();
    });

    $(window).on('resize', function (){
      $wHeight = $(window).height();
      $item.height($wHeight);
    });

    $('.carousel').carousel({  // <-- ERROR is on the .carousel
      interval: 6000,
      pause: "false"
    });    
  }
}
h3 {
  display: inline-block;
  padding: 10px;
}

.full-screen {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 300px;
}
<div id="mycarousel" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#mycarousel" data-slide-to="0" class="active"></li>
    <li data-target="#mycarousel" data-slide-to="1"></li>
    <li data-target="#mycarousel" data-slide-to="2"></li>
    <li data-target="#mycarousel" data-slide-to="3"></li>
    <li data-target="#mycarousel" data-slide-to="4"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=397" data-color="lightblue" alt="First Image">
        <div class="carousel-caption">
            <h3>First Image</h3>
        </div>
    </div>
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=689" data-color="firebrick" alt="Second Image">
        <div class="carousel-caption">
            <h3>Second Image</h3>
        </div>
    </div>
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=675" data-color="violet" alt="Third Image">
        <div class="carousel-caption">
            <h3>Third Image</h3>
        </div>
    </div>
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=658" data-color="lightgreen" alt="Fourth Image">
        <div class="carousel-caption">
            <h3>Fourth Image</h3>
        </div>
    </div>
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=638" data-color="tomato" alt="Fifth Image">
        <div class="carousel-caption">
            <h3>Fifth Image</h3>
        </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#mycarousel" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#mycarousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

这是我的 index.html 文件:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Website</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
        integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <div class="my-fluid-container">
    <div class="row">
      <div class="col-md-12 app-container">
        <app-root>Loading...</app-root>
      </div>
    </div>
  </div>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.imagesloaded/4.1.1/imagesloaded.pkgd.min.js"></script>
  <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"
          integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"
          integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"
          integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</body>
</html>

感谢您的帮助!

【问题讨论】:

    标签: angular carousel bootstrap-4


    【解决方案1】:

    第 1 步。确保在 angular-cli.json 的脚本部分中提到了 jQuery 和 Bootstrap.js

    "scripts": [
                "../node_modules/jquery/dist/jquery.min.js",
                "../node_modules/bootstrap/dist/js/bootstrap.min.js"
            ]
    

    第 2 步。在您的 home-carousel.component.ts 中,在 import { Component, OnInit } from '@angular/core'; 之后添加 declare var $:any;,如下所示:

    import { Component, OnInit } from '@angular/core';
    declare var $:any;
    

    第 3 步。虽然不是强制性的,但我发现像这样将 jQuery 和 JS 代码放在 ngOnInit() 中更安全:

    ngOnInit() {
        $('.carousel').carousel({
            interval: 2000
        });
    }
    

    我希望这行得通。

    【讨论】:

    • 对我来说 declare var $:any; 完成了这项工作,而 import * as $ from 'jquery'; 没有
    • scripts 的添加导致我的 prod 构建失败,但仅使用步骤 2 和 3 就可以在 angular 10 上为我工作
    【解决方案2】:

    我遇到了同样的问题,并且能够通过更改来解决它

    import * as $ from 'jquery';
    

    declare var jQuery: any;
    

    看起来第一个创建了一个新的 jQuery 实例;而第二个访问全局实例,该实例附加了引导函数(轮播,模态...等)。您可以通过在组件类中添加以下内容并在上述两个导入语句之间切换来确认这一点。

    console.log($.fn.carousel);
    console.log($.fn.modal);
    

    更多关于声明关键字:What does 'declare' do in 'export declare class Actions'?

    关于 TypeScript 导入语句:https://www.typescriptlang.org/docs/handbook/modules.html

    【讨论】:

      【解决方案3】:

      您的问题是您没有导入引导程序或没有正确导入它。在你的 index.html 中,确保你在 jQuery 之后导入 bootstrap,如下所示:

      <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="crossorigin="anonymous"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"></script>
      

      不是你应该能够使用轮播。

      【讨论】:

      • 我试过了,尽管我相信我确实正确引用了这些(首先是 jquery,然后是 bootstrap),但它没有任何区别。我仍然收到上面的打字稿错误。无论如何,我不确定这对打字稿有什么帮助,但我确实试了一下。谢谢
      • 好吧,我不能在评论中,它太长了。但我基本上已经完全从我在原始帖子中包含的 Codpen 链接复制了 HTML - codepen.io/SitePoint/pen/ZbGwqe。我使用相同的 CSS,并创建了一个名为 setupCarousel 的函数,我还从该 Codepen 中复制了所有 JS。在 JS 的底部是我调用轮播并使用类名将其附加到元素以选择它(“.carousel”)的地方。我通过npm安装了jquery和bootstrap,可以确认安装正确。
      • 您可以编辑您的问题并在那里插入您的代码。这样可以更轻松地帮助您。
      • 您介意添加您的 index.html。我只是试图重现错误,但是对于我的项目中的 Materialize,如果我从 Materialize 中删除 js 文件,我会得到完全相同的错误。
      • 好的,添加了 index.html。
      【解决方案4】:

      我现在遇到同样的问题,试图在我的应用上实现这一点:

      https://www.bootply.com/89193

      $('.carousel').carousel({ //

      这是我的 index.html 的样子:

          <html>
      <head>
          <meta charset="utf-8">
          <base href="/">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="icon" type="image/x-icon" href="favicon.ico">
          <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
          <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
          <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"
                  integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.imagesloaded/4.1.1/imagesloaded.pkgd.min.js"></script>
      </head>
      <body>
          <app-root>
              <div class="spinner">
                  <div class="double-bounce1"></div>
                  <div class="double-bounce2"></div>
              </div>
          </app-root>
      </body>
      </html>
      

      我认为我有正确的引导程序和 jquery

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-27
        • 2021-12-01
        • 2018-05-01
        • 1970-01-01
        • 2019-10-02
        • 1970-01-01
        • 2019-07-15
        • 1970-01-01
        相关资源
        最近更新 更多