Angular4导入本地js文件表示找不到404

时间:2017-11-14 04:21:08

标签: javascript html angular

我使用“ng new xxx”设置了一个新项目,  只需在index.html中添加一行:

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

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">

</head>
<body>
  <app-root></app-root>
 <script type="text/javascript" src="./CitrixHTML5SDK.js"></script>
</body>
</html>

然后将JS文件放在与index.html

相同的目录中

use server side rules for URL re-writing

但是当我运行“ng server”时,浏览器总是显示:404

the same dir

有谁知道为什么?提前谢谢。

2 个答案:

答案 0 :(得分:1)

我在使用JSON文件时遇到了同样的问题,所以我所做的就是将我的JSON文件保存到assets文件夹中。并通过我的代码访问它。

此处Project file review您也可以验证它。

答案 1 :(得分:0)

如果要创建新文件夹,即&#34;数据&#34;。然后你将不得不添加&#34;数据&#34; in&#34; assets&#34; angular-cli.json文件的数组。请参阅下面的JSON数据: -

    "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "data",
        "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": [
        "styles.css"
      ],
      "scripts": ["data/sample.js"],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],

之后不要忘记重启服务器。否则您将收到404错误。

相关问题