【问题标题】:Angular2 with Typescript - Error loading angular2/platform/browser带有 Typescript 的 Angular2 - 加载 angular2/平台/浏览器时出错
【发布时间】:2016-06-02 04:18:21
【问题描述】:

我在这方面花费了太多时间,但我被困住了,无法弄清楚如何让 Angular2 与 Typescript 一起运行。我不断收到组件的 404:

http://localhost:5000/appScripts/boot.jshttp://localhost:5000/angular2/platform/browser 加载为“angular2/platform/browser”时出错

这是我的文件夹结构:

我的 index.html 是:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Angular 2 with ASP.NET Core</title>
<!-- 1. Load libraries -->
<script src="libs/es6-shim/es6-shim.min.js"></script>
<script src="libs/zone.js/dist/zone.js"></script>
<script src="libs/reflect-metadata/Reflect.js"></script>
<script src="libs/systemjs/dist/system.src.js"></script>


<!-- 2. Configure SystemJS -->
<script src="./appScripts/config.js"></script>
<script>

    System.import('appScripts/boot')
          .then(null, console.error.bind(console));
</script>

</head>
<body>
<my-app>Loading...</my-app>
</body>
</html>

boot.ts:

 /// <reference path="../node_modules/angular2/typings/browser.d.ts" />
    import {bootstrap} from 'angular2/platform/browser'
    import {AppComponent} from './app'
    bootstrap(AppComponent);

app.ts:

    /// <reference path="../typings/jquery/jquery.d.ts" />
    /// <reference path="../typings/angularjs/angular-route.d.ts" />
    /// <reference path="../typings/angularjs/angular.d.ts" />

    import {Component} from 'angular2/core';
    @Component({
    selector: 'my-app',
    template: 'My First Angular 2 App'
    })
    export class AppComponent { }

config.js

(function (global) {

    // map tells the System loader where to look for things
    var map = {
        'app': 'appScripts', // 'dist',
        'rxjs': 'libs/rxjs',
        'angular2-in-memory-web-api': 'libs/angular2-in-memory-web-api',
        '@angular': 'libs/@angular'
    };

    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'app': { main: 'boot.js', defaultExtension: 'js' },
        'rxjs': { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { defaultExtension: 'js' },
    };

    var packageNames = [
      '@angular/common',
      '@angular/compiler',
      '@angular/core',
      '@angular/http',
      '@angular/platform-browser',
      '@angular/platform-browser-dynamic',
      '@angular/router',
      '@angular/router-deprecated',
      '@angular/testing',
      '@angular/upgrade',
    ];

    // add package entries for angular packages in the form '@angular/common': { main: 'index.js', defaultExtension: 'js' }
    packageNames.forEach(function (pkgName) {
        packages[pkgName] = { main: 'index.js', defaultExtension: 'js' };
    });

    var config = {
        map: map,
        packages: packages
    }

    // filterSystemConfig - index.html's chance to modify config before we register it.
    if (global.filterSystemConfig) { global.filterSystemConfig(config); }

    System.config(config);

})(this);   

这里有更多的文件夹结构,让您更好地了解项目结构。

这是我的 package.json

{
  "version": "1.0.0",
  "name": "ASP.NET",
  "private": true,
  "dependencies": {
    "@angular/common": "2.0.0-rc.1",
    "@angular/compiler": "2.0.0-rc.1",
    "@angular/core": "2.0.0-rc.1",
    "@angular/http": "2.0.0-rc.1",
    "@angular/platform-browser": "2.0.0-rc.1",
    "@angular/platform-browser-dynamic": "2.0.0-rc.1",
    "@angular/router": "2.0.0-rc.1",
    "@angular/router-deprecated": "2.0.0-rc.1",
    "@angular/upgrade": "2.0.0-rc.1",
    "systemjs": "0.19.27",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12",
    "angular2-in-memory-web-api": "0.0.7"
  },
  "devDependencies": {
    "gulp": "^3.9.0",
    "gulp-autoprefixer": "~3.1.0",
    "gulp-concat": "~2.6.0",
    "gulp-imagemin": "~2.4.0",
    "imagemin-pngquant": "~4.2.0",
    "jshint": "2.9.2",
    "gulp-jshint": "2.0.0",
    "jshint-stylish": "~2.1.0",
    "rimraf": "~2.5.1",
    "gulp-minify-css": "~1.2.4",
    "gulp-sass": "2.2.0",
    "gulp-uglify": "~1.5.1",
    "gulp-sourcemaps": "~1.6.0",
    "gulp-plumber": "1.1.0",
    "gulp-notify": "2.2.0",
    "beepbeep": "1.2.0",
    "gulp-rename": "1.2.2",
    "gulp-sourcemap": "1.0.1",
    "gulp-clean-css": "2.0.6",
    "main-bower-files": "2.13.0",
    "gulp-filter": "4.0.0",
    "typescript": "^1.8.10",
    "gulp-typescript": "^2.13.1",
    "live-server": "1.0.0",
    "typings": "^1.0.4",
    "gulp-tsc": "^1.1.5"
  }
}

提前感谢您的帮助,如果我可以提供更多详细信息,请告诉我。

【问题讨论】:

    标签: angular


    【解决方案1】:

    您正在使用 rc.1 版本的 angular,但尝试导入 beta 版本。

    你需要像这样使用@angular/..

    import { bootstrap } from '@angular/platform-browser-dynamic';
    
    import { Component } from '@angular/core'; // instead of 'angular2/core'
    ....
    

    有关更多详细信息,另请参阅更新日志https://github.com/angular/angular/blob/master/CHANGELOG.md#200-rc0-2016-05-02

    要导入各种符号请调整以下路径 方式:

    angular2/core -> @angular/core
    angular2/compiler -> @angular/compiler
    angular2/common -> @angular/common
    angular2/platform/browser -> @angular/platform-browser + @angular/platform-browser-dynamic
    angular2/platform/server -> @angular/platform-server
    angular2/testing -> @angular/core/testing
    angular2/upgrade -> @angular/upgrade
    angular2/http -> @angular/http
    angular2/router -> @angular/router-deprecated (from beta.17 for backwards compatibility)
    new package: @angular/router - component router with several breaking changes
    

    【讨论】:

    • 谢谢 Yurzui.... 你的意思是在 boot.ts 中,我应该把这行 import {bootstrap} from 'angular2/platform/browser' 改成 import { bootstrap } from '@angular/platform-browser-dynamic'
    • @Matt Knight 是的
    • 我可能将 RC1 与 beta 混为一谈。我没有想到这一点。但是,仅凭这种变化并不能完全做到这一点。我仍然得到:localhost:6722/angular2/core 加载资源失败:服务器响应状态为 404(未找到)zone.js:323 错误:错误:XHR 错误(404 未找到)正在加载 localhost:6722/angular2/core(...) 再次感谢您的帮助。
    • 您必须将所有带有angular2 的地方更改为@angular 按照链接angular.io/docs/ts/latest/quickstart.htmlimport { Component } from '@angular/core';
    • 抱歉这么密集,但你是说现在对 angular2 的每个引用都应该是 @angular?我没有看到任何明显的候选人。
    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2016-06-19
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多