angular应用程序中的ngb-carousel无法在页面刷新时正确加载
创始人
2024-10-30 15:00:28
0

这个问题通常是由于轮播组件的初始化未完成而导致的。为了保证轮播组件在页面刷新时能够正确加载,可以采用以下方法:

1.在组件的ngOnInit()生命周期函数中调用轮播组件初始化方法:

import { Component, OnInit, ViewChild } from '@angular/core';
import { NgbCarousel } from '@ng-bootstrap/ng-bootstrap';

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

   @ViewChild('carousel') carousel: NgbCarousel;//获取ngb-carousel实例

   constructor() { }

   ngOnInit() {
      this.carousel.pause();//暂停轮播
      this.carousel.cycle();//开始轮播
   }

}

2.在模板中使用ngAfterViewInit()生命周期钩子方法:


   ...


import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { NgbCarousel } from '@ng-bootstrap/ng-bootstrap';

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

  @ViewChild('carousel') carousel: NgbCarousel;

  constructor() {}

  ngAfterViewInit() {
    this.carousel.pause();//暂停轮播
    this.carousel.cycle();//开始轮播
  }

}

通过以上两种方法,我们可以确保轮播组件在页面刷新时能够正常加载并启动轮播。

相关内容

热门资讯

刚刚!wepoker有透视功能... 刚刚!wepoker有透视功能吗(透视)卡五星辅助工具(原来真的是有辅助下载)-哔哩哔哩1、让任何用...
透视私人局!pokermast... 透视私人局!pokermaster修改器(透视)邳州友友辅助(其实有辅助软件)-哔哩哔哩1、许多玩家...
透视了解!we poker插件... 透视了解!we poker插件(透视)福建宁德麻将辅助软件(切实存在有辅助工具)-哔哩哔哩1、每一步...
做出回应!wepoker脚本(... 做出回应!wepoker脚本(透视)微信小程序游戏破解器(果然是真的辅助安装)-哔哩哔哩1、起透看视...
透视插件!德州局脚本(透视)友... 透视插件!德州局脚本(透视)友间十三道有什么窍门(原来真的有辅助插件)-哔哩哔哩1、友间十三道有什么...
此事迅速冲上热搜!wepoke... 此事迅速冲上热搜!wepoker怎么增加运气(透视)微友助手怎么破解(真是有辅助app)-哔哩哔哩微...
透视玄学!aapoker怎么设... 透视玄学!aapoker怎么设置抽水(透视)边锋干橙眼辅助器下载(都是是有辅助平台)-哔哩哔哩1、边...
透视挂!xpoker透视辅助(... 透视挂!xpoker透视辅助(透视)天天贵阳大瓮安插件(好像真的有辅助app)-哔哩哔哩1、完成天天...
刚刚!hh poker辅助有用... 刚刚!hh poker辅助有用吗(透视)海盗来了辅助器无限炮(一贯真的是有辅助安装)-哔哩哔哩1、超...
据文件显示!德普之星私人局透视... 据文件显示!德普之星私人局透视(透视)反杀新大厅辅助(切实真的有辅助修改器)-哔哩哔哩暗藏猫腻,小编...