5 minute quick start开始,我一直在玩angular2测试版,遇到了一个让我困惑的问题.

下面是一个简单的版本,显示了我的问题.首先,这里有一个hello world应用程序运行得很好.

package.json

{
   "name": "...",
   "version": "0.0.1",
   "description": "...",
   "author": {
   "name": "...",
   "email": "..."
    },
    "scripts": {
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
    },
    "license": "ISC",
    "dependencies": {
    "angular2": "2.0.0-beta.0",
    "bootstrap": "^3.3.6",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "^0.1.2",
    "rxjs": "5.0.0-beta.0",
    "systemjs": "0.19.6",
    "zone.js": "^0.5.10"
    },
    "devDependencies": {
    "concurrently": "^1.0.0",
    "lite-server": "^1.3.1",
    "typescript": "^1.7.3"
    }
}

index.html

<head>
<title>Title</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link href="styles.css" rel="stylesheet" />

<!-- 1. Load libraries -->
<script src="node_modules/es6-shim/es6-shim.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/boot')
          .then(null, console.error.bind(console));
</script>

</head>

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

app/boot.ts

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {AccountService} from './accounts/account.service'

bootstrap(AppComponent);

app/app.component.ts

import {Component, View} from 'angular2/core';
import {RegisterFormComponent} from './accounts/register-form.component'
@Component({
    selector: 'my-app',
})
@View({
    template: 'hello world',
})
export class AppComponent {
}

我通常想调用我的Web Api服务,所以我试图遵循the docs for Http,我更新 bootstrap .详情如下:

new app/boot.ts

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {AccountService} from './accounts/account.service'
import {HTTP_PROVIDERS} from 'angular2/http';

bootstrap(AppComponent, [HTTP_PROVIDERS]);

这就是事情扼杀的地方.

Chrome给了我:

uncaught SyntaxError: Unexpected token < - http:1
uncaught SyntaxError: Unexpected token < - angular2-polyfills.js:138
    evaluating http://localhost:3000/angular2/http
    error loading http://localhost:3000/app/boot.js

如果我试图将HTTP_提供程序设置为组件上的viewProvider,它也会失败.

angular2还有谁有幸进入测试版吗?

  • Visual Studio 2015
  • node .JS&amp; node .用于VisualStudio的JS工具
  • 使用"NPM start"编译并运行

推荐答案

在使用SystemJS时,如果试图导入HTML中未包含的内容,则会发生此错误.像Webpack这样的模块绑定器可以为您处理所有这些.

对于您的情况,您必须添加HttpBundle 包,例如,它是一个单独的Bundle 包

<script src="https://code.angularjs.org/2.0.0-beta.0/http.dev.js"></script>

当你试图使用路由时,你会看到同样的错误,你忘记了添加路由包.

从@pkozlowski opensource的repos中查看这两种配置之间的差异

  • Using SystemJS:在这种情况下,如果他想使用http包或路由包,他必须添加它们.
  • Using Webpack:在这种情况下,Webpack会为您打包bundle.js文件中的所有内容.

很高兴这有帮助.

Typescript相关问答推荐

更新:Typescript实用程序函数合并对象键路径

为什么ESLint抱怨通用对象类型?

使用Angular和API请求在CRUD操作后更新客户端数据的良好实践

如何创建泛型类型组件来使用React Native的FlatList或SectionList?'

类型{...}的TypeScript参数不能赋给类型为never的参数''

类型脚本泛型最初推断然后设置

数组文字中对象的从键开始枚举

TypeScrip-将<;A、B、C和>之一转换为联合A|B|C

通过泛型函数本身推断受约束的泛型参数的类型脚本问题

在组件卸载时try 使用useEffect清除状态时发生冲突.react +打字

棱角分明-什么是...接口类型<;T>;扩展函数{new(...args:any[]):t;}...卑鄙?

如果判断空值,则基本类型Gaurd不起作用

打字错误TS2305:模块常量没有导出的成员

为什么在这种情况下,打字脚本泛型无法正确自动完成?

当传递带有或不带有参数的函数作为组件props 时,我应该如何指定类型?

无法缩小深度嵌套对象props 的范围

是否可以将类型参数约束为不具有属性?

通过函数传递确切的类型,但验证额外的字段

如何键入';v型';

如何判断路由或其嵌套路由是否处于活动状态?