【问题标题】:Angular - Bootstrap modal showing but not as dialogAngular - Bootstrap 模态显示但不显示为对话框
【发布时间】:2020-04-08 20:53:10
【问题描述】:

在处理我的第一个 Angular 项目时,我正在尝试实现一个对话框,要求在删除项目之前进行确认。我使用 ng-bootstrap 和 docs 中的示例作为起点。

我遇到的问题:我可以按预期打开和关闭模式,但模式没有显示为对话框窗口。它在其余内容下方以“普通”div 的形式打开。

我创建了一个“ConfirmComponent”组件,我正在尝试在另一个组件中使用它。

confirm.component.ts

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

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

  @Input() name;

  constructor(public activeModal: NgbActiveModal) { }

  ngOnInit(): void {
  }

}

confirm.component.html

<div class="modal-header">
    <h4 class="modal-title">Hi there!</h4>
    <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
</div>
<div class="modal-body">
  <p>Hello, {{name}}!</p>
</div>
<div class="modal-footer">
  <button type="button" class="btn btn-outline-dark" (click)="activeModal.close('Close click')">Close</button>
</div>

这是我尝试打开对话框的组件的 html

game-list.component.html

<div class="container">
    <h1>Available Games</h1>

    <ul *ngIf="games">
        <li *ngFor="let game of games">
            <a href="#" routerLink="/games/{{ game.pk }}" class="btn btn-secondary">{{ game.pk }}</a>
            <a class="btn btn-xs btn-outline-danger text-danger" (click)="open()"><mat-icon>delete_outlined</mat-icon></a>
        </li>
    </ul>
    <div class="card">
        <div class="card-body">
            <button class="btn btn-primary" (click)="createGame()">New Game</button>
        </div>
    </div>
</div>

以及对应的组件

game-list.component.ts

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

import { Game } from '../game';
import { GameServiceService } from '../game-service.service';
import { ConfirmComponent } from '../confirm/confirm.component';

@Component({
  selector: 'app-game-list',
  templateUrl: './game-list.component.html',
  styleUrls: ['./game-list.component.scss']
})
export class GameListComponent implements OnInit {

  games: Game[] = []

  constructor(private gameService: GameServiceService, private modalService: NgbModal) { }

  ngOnInit(): void {
    this.getGames();
  }

  getGames(): void {
    this.gameService.getGames().subscribe(games => this.games = games);
  }

  createGame(): void {
    this.gameService.createGame().subscribe(game => this.games.push(game));
  }

  open() {
    const modalRef = this.modalService.open(ConfirmComponent);
    modalRef.componentInstance.name = 'World';
  }

}

我认为我的 app.module.ts 中的导入是正确的:

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { GameListComponent } from './game/game-list/game-list.component';
import { HttpClientModule } from '@angular/common/http';
import { GameDetailComponent } from './game/game-detail/game-detail.component';
import { LoginComponent } from './auth/login/login.component';
import { NavBarComponent } from './nav-bar/nav-bar.component';
import { PlayerComponent } from './game/player/player.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { DragDropModule } from '@angular/cdk/drag-drop';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { MatIconModule } from '@angular/material/icon';
import { ConfirmComponent } from './game/confirm/confirm.component';


@NgModule({
  declarations: [
    AppComponent,
    GameListComponent,
    GameDetailComponent,
    LoginComponent,
    NavBarComponent,
    PlayerComponent,
    ConfirmComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,
    DragDropModule,
    NgbModule,
    MatIconModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我的怀疑(可能是错误的)是缺少一些重要的样式文档。在我的 Firefox 浏览器控制台中,我收到警告 Style document could no be loaded: http://myurl/node_modules/bootstrap/scss/_type.scss - 不知道这是否相关,因为我无法在该文档的源代码中发现任何与对话框相关的样式。

【问题讨论】:

  • 是的,这正是问题所在。
  • 您需要在文件夹中下载 bootstrap.min.css(仅 .css)并添加 angular.json -in the "tag" styles -before "src/styles.css"
  • @Eliseo:感谢您的快速回复。 bootstrap.min.css 已经在我的“node_modules/bootstrap/dist”文件夹中了。从未手动编辑过 angular.json:我确实有多个名为“style”的标签,但没有名为“css”的标签。不太确定在哪里添加引导 css。我猜在“构建”选项的“样式”标签以及“服务”选项的样式标签中。

标签: angular typescript bootstrap-4 angular-ui-bootstrap


【解决方案1】:

感谢@Eliseo 的评论,我找到了解决方案。将以下行添加到全局 style.scss 文件后,它按预期工作:

@import '~bootstrap/scss/bootstrap';

更多信息可以在here找到。

【讨论】:

  • Cris,你可以添加 style.scc 或者编辑你的 angular.json(这两个选项都有效)。您需要在 angular.json 中更改的“标签”在项目中-->ClientApp-->architect-->build-->options-->styles,你有一些像["src/styles.cscc"],你可以添加,所以样式变成了像["bootstrap/scss/bootstrap/bootstrap.scss","src/styles.cscc"] (*)真的我不知道你的“bootstrap.scss”在哪里,认为你需要把路径放在你的项目文件夹中
  • @Eliseo,感谢您的额外解释。出于某种原因,angular.json 方法对我不起作用,也许我走错了路。不管怎样,你让我走上了正轨,多亏了你的帮助,我在挣扎了几个小时后在短时间内解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 2016-02-25
  • 2021-10-18
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多