AngularJS2 学习笔记——TypeScript

一、 创建项目

mkdir angular-quickstart
cd angular-quickstart

二、 创建配置文件

  • package.json 标记本项目所需的 npm 依赖包。
  • tsconfig.json 定义了 TypeScript 编译器如何从项目源文件生成 JavaScript 代码
  • typings.json为那些 TypeScript 编译器无法识别的库提供了额外的定义文件
  • systemjs.config.js 为模块加载器提供了该到哪里查找应用模块的信息,并注册了所有必备的依赖包。 它还包括文档中后面的例子需要用到的包。

package.json

{
  "name": "angular-quickstart","version": "1.0.0","scripts": { "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ","lite": "lite-server","postinstall": "typings install","tsc": "tsc","tsc:w": "tsc -w","typings": "typings" },"license": "ISC","dependencies": { "@angular/common": "2.0.0","@angular/compiler": "2.0.0","@angular/core": "2.0.0","@angular/forms": "2.0.0","@angular/http": "2.0.0","@angular/platform-browser": "2.0.0","@angular/platform-browser-dynamic": "2.0.0","@angular/router": "3.0.0","@angular/upgrade": "2.0.0","core-js": "^2.4.1","reflect-Metadata": "^0.1.3","rxjs": "5.0.0-beta.12","systemjs": "0.19.27","zone.js": "^0.6.23","angular2-in-memory-web-api": "0.0.20","bootstrap": "^3.3.6" },"devDependencies": { "concurrently": "^2.2.0","lite-server": "^2.2.2","typescript": "^2.3.4","typings":"^1.3.2" } }

tsconfig.json

{
  "compilerOptions": { "target": "es5","module": "commonjs","moduleResolution": "node","sourceMap": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"removeComments": false,"noImplicitAny": false } }

typings.json

{
  "globalDependencies": { "core-js": "registry:dt/core-js#0.0.0+20160725163759","jasmine": "registry:dt/jasmine#2.2.0+20160621224255","node": "registry:dt/node#6.0.0+20160909174046" } }

systemjs.config.js

/** * System configuration for Angular samples * Adjust as necessary for your application needs. */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },// map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',// angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js','@angular/common': 'npm:@angular/common/bundles/common.umd.js','@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js','@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js','@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js','@angular/http': 'npm:@angular/http/bundles/http.umd.js','@angular/router': 'npm:@angular/router/bundles/router.umd.js','@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',// other libraries
      'rxjs':                       'npm:rxjs','angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',},// packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',defaultExtension: 'js'
      },rxjs: {
        defaultExtension: 'js'
      },'angular2-in-memory-web-api': {
        main: './index.js',defaultExtension: 'js'
      }
    }
  });
})(this);

执行命令

cnpm install

生成项目结构:

三、 创建项目

创建app目录

mkdir app
cd app

1. 创建app/app.module.ts

import { NgModule }      from '@angular/core';
import { browserModule } from '@angular/platform-browser';

@NgModule({
  imports:      [ browserModule ]
})
export class AppModule { }

每个Angular应用至少需要一个root module(根模块),实例中为AppModule。
上面从@angular/platform-browser中导入browserModule并添加到imports数组中。

2. 创建组件并添加到应用中

每个Angular应用都至少有一个根组件,实例中为AppComponent,
app.component.ts

import { Component } from '@angular/core';
@Component({
  selector: 'my-app',template: '<h1>我的第一个 Angular 应用</h1>'
})
export class AppComponent { }
  • 上面从@angular2/core引入了Component包。
  • @Component是Angular2的装饰器,它会把一份元数据关联到AppCmponent组件类上。
  • @view包含了一个template,告诉Angular如何渲染该组件的视图
  • export指定了组件可以在文件外使用。

3. 修改app.module.ts,导入新的AppComponent,并把它添加到NgModule装饰器的declarations和bootstrap字段中

import { NgModule }      from '@angular/core';
import { browserModule } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
@NgModule({
  imports:      [ browserModule ],declarations: [ AppComponent ],bootstrap:    [ AppComponent ]
})
export class AppModule { }

4. 启动应用

创建app/main.ts

import { platformbrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

const platform = platformbrowserDynamic(); //初始化平台
platform.bootstrapModule(AppModule);       //启动AppModule

5. 定义宿主页面

创建index.html

<html>
  <head>
    <title>Angular 2 实例 - 菜鸟教程(runoob.com)</title>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="styles.css">
    <!-- 1. 载入库 -->
    <!-- IE 需要 polyfill -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-Metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. 配置 SystemJS -->
    <script src="systemjs.config.js"></script>
    <script> System.import('app').catch(function(err){ console.error(err); }); </script>
  </head>
  <!-- 3. 显示应用 -->
  <body>
    <my-app>Loading...</my-app>
  </body>
</html>

定义样式文件

/* Master Styles */
h1 { color: #369; font-family: Arial,Helvetica,sans-serif; font-size: 250%; }
h2,h3 { color: #444; font-family: Arial,sans-serif; font-weight: lighter; }
body { margin: 2em; }

6. 编译运行

npm start

最终目录结构:

运行时报错:

Types of property 'lift' are incompatible.
Type '<T,R>(operator: Operator<T,R>) => Observable<T>' is not assignable to type '<R>(operator: Operator<T,R>) => Observable<R>'.
...

处理方法见:
https://stackoverflow.com/questions/44793859/rxjs-subject-d-ts-error-class-subjectt-incorrectly-extends-base-class-obs

简单的处理方式是修改tsconfig.json,加

"compilerOptions": {

    "skipLibCheck": true,}

运行结果:

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...