簡體   English   中英

錯誤類型錯誤:無法讀取 null 的屬性“3”

[英]ERROR TypeError: Cannot read property '3' of null

我正在使用帶有 angular 9 的 owlCarousel 來顯示視頻。 加載時返回錯誤Cannot read property '3' of null

這是我的 html 頁面,用於加載 OwlCarousel

<div class="owl-carousel owl-theme">
  <div class="item-video" data-merge="1">
    <a class="owl-video" href="{{data.imageUrl}}"></a>
  </div>
</div>

typescript 檔案:

ngOnInit(): void {
 $(document).ready(function(){
     $('.owl-carousel').owlCarousel({
       nav:true,
       items:1,
       autoWidth: true,
       video:true
     })
   });
 }

在此處輸入圖像描述

我會很感激一些幫助。

在我看來,您的輪播在可用之前正在初始化。 為此,您可以對屬性使用*ngIf ,當屬性為真時,它將被初始化。

<div *ngIf="somePropToBeTruthy" class="owl-carousel owl-theme">
   ...
</div>

您可以像這樣在 angular 中使用 carousel 而不是使用 jquery 使用 angular 的功能

<owl-carousel-o [options]="customOptions">
<ng-template carouselSlide *ngFor="let data of sliderImages">
    <div class="slider-item">
        <img [src]="data.image" alt="data.image">
    </div>
</ng-template>
customOptions: OwlOptions = {
    loop: true,
    mouseDrag: true,
    touchDrag: true,
    pullDrag: true,
    autoplay: true,
    rtl: true,
    dots: false,
    navSpeed: 700,
    items: 1,
    responsive: {
        0: {
            items: 1
        },
    },
    navText: ['', ''],
    nav: true,
    autoHeight:true
};


 sliderImages = [
    {
       'image':'abc.png'
    }, {
       'image':'abc1.png'
    }]

我假設 jquery 已正確集成。 然后,您應該將代碼放入ngAfterViewInit中,如下所示(ngAfterViewInit 是文檔就緒事件的替換),

ngAfterViewInit(): void {
 
     $('.owl-carousel').owlCarousel({
       nav:true,
       items:1,
       autoWidth: true,
       video:true
     })
  
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM