Angular 2快速入门404 GET /app/main.js

28

我正在尝试Angular 2的快速入门,但是我不知道它为什么不起作用。我已经找到了一些类似的问题,但是我没有找到答案。有人能帮助我吗?

这是我的代码。

app.component.ts

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }

main.ts

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'

bootstrap(AppComponent);

index.html

  <html>
   <head>
     <title>Angular 2 QuickStart</title>
     <meta name="viewport" content="width=device-width, initial-scale=1">    
     <link rel="stylesheet" href="styles.css">

     <!-- 1. Load libraries -->
     <!-- IE required polyfills, in this exact order -->
     <script src="node_modules/es6-shim/es6-shim.min.js"></script>
     <script src="node_modules/systemjs/dist/system-polyfills.js">   </script>
    <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>   

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>
  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>
</html>

package.json

 {
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
  "start": "concurrently \"npm run tsc:w\" \"npm run lite\" ",    
  "tsc": "tsc",
  "tsc:w": "tsc -w",
  "lite": "lite-server",
  "typings": "typings",
  "postinstall": "typings install" 
 },
 "license": "ISC",
 "dependencies": {
 "angular2": "2.0.0-beta.7",
 "systemjs": "0.19.22",
 "es6-promise": "^3.0.2",
 "es6-shim": "^0.33.3",
 "reflect-metadata": "0.1.2",
 "rxjs": "5.0.0-beta.2",
 "zone.js": "0.5.15"
 },
 "devDependencies": {
 "concurrently": "^2.0.0",
 "lite-server": "^2.1.0",
 "typescript": "^1.8.2",
 "typings":"^0.6.8"
 }
}

tscoding.json

{
  "compilerOptions": {
  "target": "ES6",
  "module": "system",
  "moduleResolution": "node",
  "sourceMap": true,
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "removeComments": false,
  "noImplicitAny": false
  },
  "exclude": [
  "node_modules",
  "typings/main",
  "typings/main.d.ts"
 ]
}

npm start 输出

> npm start

> angular2-quickstart@1.0.0 start /Users/dpcrespo/Google   Drive/projects/tutorials/angular/angular2-quickstart
> concurrently "npm run tsc:w" "npm run lite"

[0]
[0] > angular2-quickstart@1.0.0 tsc:w /Users/dpcrespo/Google     Drive/projects/tutorials/angular/angular2-quickstart
[0] > tsc -w
[0]
[1]
[1] > angular2-quickstart@1.0.0 lite /Users/dpcrespo/Google    Drive/projects/tutorials/angular/angular2-quickstart
[1] > lite-server
[1]
[0] Version 1.8.7
[0] Syntax:   tsc [options] [file ...]
[0]
[0] Examples: tsc hello.ts
[0]           tsc --out file.js file.ts
[0]           tsc @args.txt
[0]
[0] Options:
[0]  --allowJs                           Allow javascript files to be compiled.
[0]  --allowSyntheticDefaultImports      Allow default imports from modules with no default export. This does not affect code emit, just typechecking.
[0]  --allowUnreachableCode              Do not report errors on unreachable code.
[0]  --allowUnusedLabels                 Do not report errors on unused labels.
[0]  -d, --declaration                   Generates corresponding '.d.ts' file.
[0]  --experimentalDecorators            Enables experimental support for ES7 decorators.
[0]  --forceConsistentCasingInFileNames  Disallow inconsistently-cased references to the same file.
[0]  -h, --help                          Print this message.
[0]  --init                              Initializes a TypeScript project and creates a tsconfig.json file.
[0]  --jsx KIND                          Specify JSX code generation: 'preserve' or 'react'
[0]  --mapRoot LOCATION                  Specifies the location where debugger should locate map files instead of generated locations.
[0]  -m KIND, --module KIND              Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es2015'
[0]  --moduleResolution                  Specifies module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6).
[0]  --newLine NEWLINE                   Specifies the end of line sequence to be used when emitting files: 'CRLF' (dos) or 'LF' (unix).
[0]  --noEmit                            Do not emit outputs.
[0]  --noEmitOnError                     Do not emit outputs if any errors were reported.
[0]  --noFallthroughCasesInSwitch        Report errors for fallthrough cases in switch statement.
[0]  --noImplicitAny                     Raise error on expressions and declarations with an implied 'any' type.
[0]  --noImplicitReturns                 Report error when not all code paths in function return a value.
[0]  --noImplicitUseStrict               Do not emit 'use strict' directives in module output.
[0]  --outDir DIRECTORY                  Redirect output structure to the directory.
[0]  --outFile FILE                      Concatenate and emit output to single file.
[0]  --preserveConstEnums                Do not erase const enum declarations in generated code.
[0]  --pretty KIND                       Stylize errors and messages using color and context. (experimental)
[0]  -p DIRECTORY, --project DIRECTORY   Compile the project in the given directory.
[0]  --reactNamespace                    Specifies the object invoked for createElement and __spread when targeting 'react' JSX emit
[0]  --removeComments                    Do not emit comments to output.
[0]  --rootDir LOCATION                  Specifies the root directory of input files. Use to control the output directory structure with --outDir.
[0]  --sourceMap                         Generates corresponding '.map' file.
[0]  --sourceRoot LOCATION               Specifies the location where debugger should locate TypeScript files instead of source locations.
[0]  --suppressImplicitAnyIndexErrors    Suppress noImplicitAny errors for indexing objects lacking index signatures.
[0]  -t VERSION, --target VERSION        Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015' (experimental)
[0]  -v, --version                       Print the compiler's version.
[0]  -w, --watch                         Watch input files.
[0]  @<file>                             Insert command line options and files from a file.
[0] npm run tsc:w exited with code 0
[1] Did not detect a `bs-config.json` or `bs-config.js` override file. Using lite-server defaults...
[1] ** browser-sync options **
[1] { injectChanges: false,
[1]   files: [ './**/*.{html,htm,css,js}' ],
[1]   server: { baseDir: './', middleware: [ [Function], [Function] ] } }
[1] [BS] Access URLs:
[1]  ------------------------------------
[1]        Local: http://localhost:3000
[1]     External: http://192.168.0.9:3000
[1]  ------------------------------------
[1]           UI: http://localhost:3001
[1]  UI External: http://192.168.0.9:3001
[1]  ------------------------------------
[1] [BS] Serving files from: ./
[1] [BS] Watching files...
[1] 16.03.16 11:30:42 304 GET /index.html
[1] 16.03.16 11:30:42 304 GET /styles.css
[1] 16.03.16 11:30:42 304 GET /node_modules/es6-shim/es6-shim.min.js
[1] 16.03.16 11:30:42 304 GET /node_modules/systemjs/dist/system-    polyfills.js
[1] 16.03.16 11:30:42 304 GET /node_modules/angular2/bundles/angular2-polyfills.js
[1] 16.03.16 11:30:42 304 GET /node_modules/systemjs/dist/system.src.js
[1] 16.03.16 11:30:42 304 GET /node_modules/angular2/es6/dev/src/testing/shims_for_IE.js
[1] 16.03.16 11:30:42 304 GET /node_modules/rxjs/bundles/Rx.js
[1] 16.03.16 11:30:42 304 GET /node_modules/angular2/bundles/angular2.dev.js
[1] 16.03.16 11:30:42 404 GET /app/main.js

在我的浏览器中

在此输入图片描述

好像没有将main.ts文件转换为main.js

提前致谢。


2
“tscoding.json”是问题中的错别字,还是在您的项目中实际叫这个名字?如果是后者,那么这就是您的问题——它应该被称为“tsconfig.json”。否则,“tsc”将无法识别它。 - Joe Clay
抱歉,问题中有错别字。我开始了一个新项目来进行快速启动,现在它可以工作了,但是我需要先重新加载页面,否则我会得到相同的错误404 GET /app/main.js。似乎主文件main.js没有编译好。在这一点上,我看到了另一个问题,并说这是一个错误,并向谷歌报告了此问题。 - Daniel Perez
我遇到了相同的错误,但是我的项目文件名都是正确的,如tsconfig.json和main.ts。但是之后出现了找不到app/main.js的错误。 - Priyanka Ahire
我也使用这个项目:https://github.com/praetorxyn/angular2-interactive-map - user1767316
21个回答

17

我也遇到了这个错误。

在我的情况下,我错误地命名了tsconfig.json文件。当我改成正确的tsconfig.json文件名后,重新运行npm installnpm start后,问题就解决了。


2
谢谢你!我也把tsconfig.json错误地命名为tsconfig.js :) - Devendra Verma
我是一个新手,我下载了一个演示代码,将其部署到我们的IIS中,执行了NPM安装以下载包,然后不知道该怎么做,尝试了ng build,但出现错误,指出最新的cli未安装。我按照其他答案在这里更改'app'为'./app',但没有解决问题。我拼图的最后一块缺失是npm start。非常感谢。 - Esen

11

尝试在配置文件中更改地图。在我的情况下,我有

System.config({
    transpiler: 'typescript',
    typescriptOptions: { emitDecoratorMetadata: true },
    packages: {'app': {defaultExtension: 'ts'}},
    map: { 'app': './app' }
  });

/app是组件(.ts)创建的文件夹


1
谢谢你挽救了我的一天。www.tutorialspoint.com上有错误的信息。 - krishn Patel

8

在您的index.html文件中引用了app/main。您的main.ts文件是在名为app的文件夹中还是在根目录中?


我认为快速入门教程中有一个错误,你需要在system.config.ts中更改main.ts的路径 => main: 'app/main.js'。 - ThomasP1988
2
我遇到了同样的错误。在 typescript 教程 中的 systemjs.config.js 文件中,有一行代码 'app': { main: 'main.js', defaultExtension: 'js' },。但是将其改为 main.ts 并不能解决我的问题。 - A.W.
4
@Guus - main: './app.main.js' 对我解决了这个问题。 - JohnnyFaldo

7

tsconfig.js 应该改为 tsconfig.json


4

我认为这是系统配置部分的问题。映射值为'./angular2/src/app',这是错误的。以下代码对我有帮助。

<script>
  System.config({
    transpiler: 'typescript',
    typescriptOptions: { emitDecoratorMetadata: true },
    packages: {'app': {defaultExtension: 'ts'}},
    map: { 'app': './app' }
  });
  System.import('app/hello_world_main')
        .then(null, console.error.bind(console));
</script>

映射: {'app': './app'} - 确保组件在应用程序的app文件夹下搜索。


2
当命令
npm start

main.ts会被转译成.js文件,并且在每个文件名前面添加"app"。但是在systemjs.configs.js中,它调用的是main.js而不是app.main.js

在我的情况下,我进行了更改。

'app':                        { main: 'main.js',  defaultExtension: 'js' },

(该内容在angular官方页面/angular-2-quickstart上声明)

'app':                        { main: 'app.main.js',  defaultExtension: 'js' },

它运行良好。

不确定这是最佳解决方案...(请给我反馈。)


2

在您的systemjs.config.js文件中,将应用程序的映射更改为正确的文件路径,因此...修改以下行:

'app': 'app', // 'dist',

修改为:

'app': '您的路径在此处',留空以使用相对路径。


对我来说是救命稻草。 - Ubiquitous Developers

2

检查你是否在cmd中全局安装了typingslite-server,输入typings,如果没有安装,请使用npm install -g typings lite-server来安装它们。

然后在app目录下的main.ts文件顶部添加以下代码:

/// <reference path="../typings/browser.d.ts" />

现在运行 npm install && npm run

1
在你的tsconfig.json文件中,如果你已经添加了:
"outDir":"yourOutDir" 

您需要在system.config.js中更改映射位置。

// our app is within the app folder
app: 'yourOutDir/app',

1

Add this:

  // DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
    transpiler: 'ts',
    typescriptOptions: {
      tsconfig: true
    },
    meta: {
      'typescript': {
        "exports": "ts"
      }
    },

在文件systemjs.config.js中编译main.ts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接