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

Posted

技术标签:

【中文标题】脚本不从 .angular-cli.json 加载【英文标题】:Scripts don't load from .angular-cli.json 【发布时间】:2018-04-05 13:51:54 【问题描述】:

我将新脚本添加到我的 .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"
      
    
  ]

然后我像这样将引导程序中的轮播添加到我的 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" >
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://ocdn.eu/pulscms-transforms/1/jQxktkpTURBXy83MjQ2M2M3NjAzZmU5MTA0MTZjOWJiYzVlY2U0NzUzNy5wbmeRkwLNAyYA" >
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://ocdn.eu/pulscms-transforms/1/jQxktkpTURBXy83MjQ2M2M3NjAzZmU5MTA0MTZjOWJiYzVlY2U0NzUzNy5wbmeRkwLNAyYA" >
    </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 进行构建,重新启动服务,但没有帮助。

【问题讨论】:

您找到解决方案了吗?我有同样的问题.. 这个问题运气好吗? 【参考方案1】:

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

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

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

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

【讨论】:

以上是关于脚本不从 .angular-cli.json 加载的主要内容,如果未能解决你的问题,请参考以下文章

Angular cli按需加载脚本

Angular总结二:Angular 启动过程

jQuery自动完成不从文本加载源

缺少 .angular-cli.json 文件:Angular

angular-cli.json中node_modules路径的智能感知?

Angular-cli.json 、 webpack.conf 和 tsconfig.json 之间的区别