简体   繁体   中英

Angular 4 index.html won't load any script

This is frustrating, I'm trying to load a simple script inside my index.html but I get 404.

index.html

<head>
  <meta charset="utf-8">
  <title>Login</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="./app/scripts/angular-cookies.js"></script>
</body>
</html>

This is my project structure

在此输入图像描述

This is the error I get - 404

在此输入图像描述

The file is there and it's not empty. I tried also:

<script type="text/javascript" src="../app/scripts/angular-cookies.js">

and

<script type="text/javascript" src="app/scripts/angular-cookies.js">

and

<script type="text/javascript" src="/app/scripts/angular-cookies.js">

Any help would be appreciated, thanks!

As I've mentioned in comment, any external resource should be placed in assets folder and for your case referenced like <script type="text/javascript" src="assets/scripts/angular-cookies.js"> considering assets contains a folder named scripts which contains your angular-cookies.js file. After building the code the resulted www folder will be like:

  • assets -> which contains the external files
  • build -> which contains your ts files transpiled into js files
  • index.html
  • other files

Try this and it will be fine:

<script type="text/javascript" src="/src/app/scripts/angular-cookies.js">

Or maybe

<script type="text/javascript" src="src/app/scripts/angular-cookies.js">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM