脚本不会从.angular-cli.json加载

时间:2017-10-24 16:55:16

标签: javascript angular twitter-bootstrap bootstrap-4

我在.angular-cli.json文件中添加了新脚本,如下所示:

    "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "styles.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/popper.js/dist/popper.min.js",
        "../node_modules/bootstrap/dist/js/bootstrap.min.js"
      ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ]

然后我将bootstrap中的carousel添加到我的app.component.html文件中(只需从文档中复制):

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" style="background: #000000;">
  <ol class="carousel-indicators">
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src="https://ocdn.eu/pulscms-transforms/1/jQxktkpTURBXy83MjQ2M2M3NjAzZmU5MTA0MTZjOWJiYzVlY2U0NzUzNy5wbmeRkwLNAyYA" alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://ocdn.eu/pulscms-transforms/1/jQxktkpTURBXy83MjQ2M2M3NjAzZmU5MTA0MTZjOWJiYzVlY2U0NzUzNy5wbmeRkwLNAyYA" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://ocdn.eu/pulscms-transforms/1/jQxktkpTURBXy83MjQ2M2M3NjAzZmU5MTA0MTZjOWJiYzVlY2U0NzUzNy5wbmeRkwLNAyYA" alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

当我尝试从我的index.html文件加载脚本时(一切正常):

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

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>

</head>
<body>
  <app-root></app-root>
</body>
</html>

有什么解决方案吗?我通过ng build,restar ng serve进行构建,但它没有帮助。

1 个答案:

答案 0 :(得分:1)

我在 angular-cli 应用中遇到了类似的问题。我终于发现我的index.html遗漏了一个包含:

<script type="text/javascript" src="scripts.bundle.js">

添加该脚本包括为我修复它。

仅供参考我的服务器设置没有使用生成的index.html ...我可能复制并粘贴了index.html脚本包含,然后添加了脚本部分,导致问题。