【问题标题】:Hello World Angular2 app with typescript带有打字稿的 Hello World Angular2 应用程序
【发布时间】:2017-09-12 00:14:18
【问题描述】:

我在 angular2 中练习我的第一个程序,但与预期的输出有些偏差。

typings.json:

{
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
    "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#5c182b9af717f73146399c2485f70f1e2ac0ff2b"
  }
}

tsconfig.json

{

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

package.json

{
  "name": "angular2-helloworld-example",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && 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.15",
    "systemjs": "0.19.26",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.6.10"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.2.0",
    "typescript": "^1.8.10",
    "typings":"^0.7.12"
  }
}

index.html

<!DOCTYPE html>
<html>  
  <head>
    <title>My First Angular 2 App</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- 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>

在app文件夹里面有2个文件:

app.component.ts

import { Component } from 'angular2/core';

@Component({
  selector: 'my-app',
  templateUrl: '<h1> Hello </h1>'
})
export class AppComponent {     
}

ma​​in.ts

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

bootstrap(AppComponent); 

当我运行命令时,npm start 服务器正在运行,没有任何错误,但输出是: 加载中...

我的预期输出是:

'<h1> Hello </h1>'

我错过了什么?

【问题讨论】:

  • 你已经严重过时了。您正在使用一年前的 Angular 2 包。升级!请注意,升级将非常痛苦,但如果您不这样做,将没有人能够提供任何支持,因为事情已经发生了根本性的变化。
  • @AluanHaddad :请给我发一份更新配置的适当参考学习资料。上面的例子是我最后得到的。
  • 老实说,我会从 angular.io/docs/ts/latest/quickstart.html 重新开始,这听起来不像你有很多代码。他们介绍了 manymany 的概念、大量的复杂性、与 ES Modules 一起存在的他们自己的模块系统、更改了 3 次路由等等等。

标签: node.js angular typescript


【解决方案1】:

我找到了解决办法,

app.component.ts 我必须使用 template 而不是 templateUrl

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-23
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多