Angular Bootstrap 手风琴动画在制作过程中无法正常工作

问题描述 投票:0回答:2

我已经在 Angular 中的应用程序上安装了 Bootstrap,但由于某种原因,当我在运行

ng build --configuration production
并将其打开到本地服务器后进行生产时,引导手风琴动画无法在关闭时工作。当我打开手风琴时,动画效果很好,但关闭时则不起作用。关于此案的任何解决方案。

这是我的

package.json

{
  "name": "theme-switcher",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~11.1.2",
    "@angular/common": "~11.1.2",
    "@angular/compiler": "~11.1.2",
    "@angular/core": "~11.1.2",
    "@angular/forms": "~11.1.2",
    "@angular/platform-browser": "~11.1.2",
    "@angular/platform-browser-dynamic": "~11.1.2",
    "@angular/router": "~11.1.2",
    "bootstrap": "^5.1.3",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.11.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1101.4",
    "@angular/cli": "~11.1.4",
    "@angular/compiler-cli": "~11.1.2",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.2.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.1.2"
  }
}

这是我的

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "cli": {
    "analytics": "7352546f-427c-4086-beae-9a75be929da6"
  },
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "theme-switcher": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/theme-switcher",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/style.scss"
            ],
            "scripts": [
              "./node_modules/bootstrap/dist/js/bootstrap.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "theme-switcher:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "theme-switcher:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "theme-switcher:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "theme-switcher:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "theme-switcher:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "theme-switcher"
}

这是我的

style.scss

@import '../node_modules/bootstrap/scss/bootstrap';

这是我的

app.component.html

<div class="container">
    <div class="row">
        <div class="accordion" id="accordionPanelsStayOpenExample">
    <div class="accordion-item">
      <h2 class="accordion-header" id="panelsStayOpen-headingOne">
        <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseOne" aria-expanded="true" aria-controls="panelsStayOpen-collapseOne">
          Accordion Item #1
        </button>
      </h2>
      <div id="panelsStayOpen-collapseOne" class="accordion-collapse collapse show" aria-labelledby="panelsStayOpen-headingOne">
        <div class="accordion-body">
          <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
        </div>
      </div>
    </div>
    <div class="accordion-item">
      <h2 class="accordion-header" id="panelsStayOpen-headingTwo">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseTwo" aria-expanded="false" aria-controls="panelsStayOpen-collapseTwo">
          Accordion Item #2
        </button>
      </h2>
      <div id="panelsStayOpen-collapseTwo" class="accordion-collapse collapse" aria-labelledby="panelsStayOpen-headingTwo">
        <div class="accordion-body">
          <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
        </div>
      </div>
    </div>
    <div class="accordion-item">
      <h2 class="accordion-header" id="panelsStayOpen-headingThree">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseThree" aria-expanded="false" aria-controls="panelsStayOpen-collapseThree">
          Accordion Item #3
        </button>
      </h2>
      <div id="panelsStayOpen-collapseThree" class="accordion-collapse collapse" aria-labelledby="panelsStayOpen-headingThree">
        <div class="accordion-body">
          <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
        </div>
      </div>
    </div>
  </div>
    </div>
</div>
html angular twitter-bootstrap bootstrap-5
2个回答
3
投票

看起来这是 Angular JavaScript 优化器中的一个错误。 我已经向 Angular 团队报告了一个问题:https://github.com/angular/angular-cli/issues/22635

在 Angular 团队修复此问题之前您拥有的选项:

  1. 从角度注入中排除 bootstrap.js 脚本,并自行在应用程序的主 html 文件中手动添加 bootstrap.js :
  • 编辑 angular.json 文件并更改
"scripts": [
   "./node_modules/bootstrap/dist/js/bootstrap.js"
]

至:

"scripts": [
  {"input" :"node_modules/bootstrap/dist/js/bootstrap.bundle.js",
   "inject": false
  }
]
  • Inject 标志设置为 false 会阻止文件包含在最终的 Angular JavaScript 文件中。您还可以从 angular.json 中删除 bootstrap.js,但这样在 Angular 中的任何地方都不会出现 bootstrap 的 JavaScript 文件的踪迹。留下该行将使您记住在 Angular 团队正确修复后恢复此更改。

  • 编辑您的主 html 文件(例如 index.html)并在其中添加 bootstrap.js。它必须添加到 html 文件的底部,因为它必须尽可能最新地加载:

...
</body>
    <script src="dist/bootstrap/js/bootstrap.bundle.min.js"></script>
</html>

使用这种方法,浏览器会额外请求 bootstra.ks 文件,但至少它有自己的优化版本供您参考(如上面的示例所示)。

  1. 用 ngBootstrap 包中的 [(ngCollapse)] 指令替换 Bootstrap 的折叠功能。我发现这个不受关闭动画错误的影响。为此,您必须安装对 Angular/Bootstrap 混合有效的 ngBootstrap 版本,并将“data-bs-toggle”属性替换为 [(ngCollapse)] 指令。更多相关信息可以在这里找到: https://ng-bootstrap.github.io/#/components/collapse/examples

0
投票

现在是 2024 年,我遇到了这个问题。 我在我的项目中使用最新的 Angular/Bootstrap 版本,所以我不敢相信他们的团队还没有修复这个问题...... :_(

© www.soinside.com 2019 - 2024. All rights reserved.