欢迎跟我一起学习Angular2

本文根据angular2官网手动敲码得来:
本文地址:http://blog.csdn.net/sushengmiyan
本文作者:苏生米沿
- 开发环境搭建
- 配置文件
- 安装依赖包
- *创建基础应用
- 创建组件
- 创建启动页面
- 编译启动

环境搭建

安装 Node.js and npm 。

  1. nodejs 下载地址 https://nodejs.org/download输入 node - v 显示当前版本号

  2. 新版的NodeJS已经集成了npm,所以之前npm也一并安装好了。同样可以使用cmd命令行中键入 npm –v

配置文件

  1. package.json
  2. tsconfig.json
  3. typings.json
  4. 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","@angulartp": "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.0.2","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:dtde#6.0.0+20160909174046" } }

typings.json代码块

如下:

/** * 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/corendles/core.umd.js','@angular/common': 'npm:@angular/commonndles/common.umd.js','@angular/compiler': 'npm:@angular/compilerndles/compiler.umd.js','@angular/platform-browser': 'npm:@angular/platform-browserndles/platform-browser.umd.js','@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamicndles/platform-browser-dynamic.umd.js','@angulartp': 'npm:@angulartpndlestp.umd.js','@angular/router': 'npm:@angular/routerndles/router.umd.js','@angular/forms': 'npm:@angular/formsndles/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);

安装依赖包

在工程目录文件下运行npm install。
中间会比较慢,等待。没有出现npm ERR!就说明安装成功。
程序会自动安装typings。
即在项目文件夹下可以看到多出来的node_modules和typings文件夹。如果typings没有自动安装,则运行 npm run typings install 手动安装。

创建基础模块

在工程目录下创建一个app文件夹,创建一个根模块suos.module.ts文件,angular2的每个应用应该有一个根模块。
Suos.module.ts内容如下:

import { NgModule } from '@angular/core';
import{browserModule}from'@angular/platform-browser';
@NgModule({
  imports:      [ browserModule ]
})
export class SuosModule { }

创建基础组件

每个angular2应用至少由一个根组件。这里我们创建一个SuosComponent。在app目录下创建suos.component.ts 代码如下:

import { Component } from '@angular/core';
@Component({
  selector: 'suosstart',template: '<h1>Suos样例工程</h1>'
})
export class SuosComponent { }

创建启动类

import { platformbrowserDynamic } from '@angular/platform-browser-dynamic';
import { SuosModule } from './suos.module';
const platform = platformbrowserDynamic();
platform.bootstrapModule(SuosModule);

创建启动页面

index.html

<html>
  <head>
    <title>Suos启动测试</title>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="style.css">
    <!-- 1. Load libraries -->
     <!-- polyfill(s) for older browsers -->
    <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. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script> System.import('app').catch(function(err){ console.error(err); }); </script>
  </head>
  <!-- 3. display the application -->
  <body>
    <suosstart>加载中...</suosstart>
  </body>
</html>

其中style.css样式内容如下:

/* 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; }

启动应用

npm start 会自动启动浏览器,看到出现结果页面如下即表明启动成功:

目录

  • 欢迎跟我一起学习Angular2
    • 环境搭建
    • 配置文件
      • packagejson代码块
      • tsconfigjson代码块
      • typingsjson代码块
      • typingsjson代码块
    • 安装依赖包
    • 创建基础模块
    • 创建基础组件
    • 创建启动类
    • 创建启动页面
    • 启动应用
      • 目录

第一个Angular2的样例的更多相关文章

  1. Android Studio是否支持用于Android UI设计的AngularJS?

    我对AndroidStudio有疑问:AS在设计XML文件时是否支持AngularJS代码,例如:对于小动画或效果?

  2. android – 如何使用ClientID和ClientSecret在Phonegap中使用Angularjs登录Google OAuth2

    我正尝试使用Angularjs(使用IonicFramework)通过GoogleOAuth2从我的Phonegap应用程序登录.目前我正在使用http://phonegap-tips.com/articles/google-api-oauth-with-phonegaps-inappbrowser.html进行登录.但是当我使用Angular-UI-RouterforIonic时,它正在创建非常

  3. 利用require.js与angular搭建spa应用的方法实例

    这篇文章主要给大家介绍了关于利用require.js与angular搭建spa应用的方法实例,文中通过示例代码给大家介绍的非常详细,对大家的理解和学习具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。

  4. 详解Angular动态组件

    本文主要介绍了Angular动态组件,对此感兴趣的同学,可以亲自实验一下。

  5. 详解如何使用webpack+es6开发angular1.x

    本篇文章主要介绍了详解如何使用webpack+es6开发angular1.x,具有一定的参考价值,有兴趣的可以了解一下

  6. angular2系列之路由转场动画的示例代码

    本篇文章主要介绍了angular2系列之路由转场动画的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. 一种angular的方法级的缓存注解(装饰器)

    本篇文章主要介绍了一种angular的方法级的缓存注解(装饰器),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. 动手写一个angular版本的Message组件的方法

    本篇文章主要介绍了动手写一个angular版本的Message组件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  9. angular forEach方法遍历源码解读

    这篇文章主要为大家详细了angular forEach方法遍历源码,forEach()方法用于遍历对象或数组,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Angular的MVC和作用域

    本文主要Angular的MVC和作用域进行详细分析介绍,具有一定的参考价值,下面跟着小编一起来看下吧

随机推荐

  1. Angular2 innerHtml删除样式

    我正在使用innerHtml并在我的cms中设置html,响应似乎没问题,如果我这样打印:{{poi.content}}它给了我正确的内容:``但是当我使用[innerHtml]=“poi.content”时,它会给我这个html:当我使用[innerHtml]时,有谁知道为什么它会剥离我的样式Angular2清理动态添加的HTML,样式,……

  2. 为Angular根组件/模块指定@Input()参数

    我有3个根组件,由根AppModule引导.你如何为其中一个组件指定@input()参数?也不由AppModalComponent获取:它是未定义的.据我所知,你不能将@input()传递给bootstraped组件.但您可以使用其他方法来做到这一点–将值作为属性传递.index.html:app.component.ts:

  3. angular-ui-bootstrap – 如何为angular ui-bootstrap tabs指令指定href参数

    我正在使用角度ui-bootstrap库,但我不知道如何为每个选项卡指定自定义href.在角度ui-bootstrap文档中,指定了一个可选参数select(),但我不知道如何使用它来自定义每个选项卡的链接另一种重新定义问题的方法是如何使用带有角度ui-bootstrap选项卡的路由我希望现在还不算太晚,但我今天遇到了同样的问题.你可以通过以下方式实现:1)在控制器中定义选项卡href:2)声明一个函数来改变控制器中的散列:3)使用以下标记:我不确定这是否是最好的方法,我很乐意听取别人的意见.

  4. 离子框架 – 标签内部的ng-click不起作用

    >为什么标签标签内的按钮不起作用?>但是标签外的按钮(登陆)工作正常,为什么?>请帮我解决这个问题.我需要在点击时做出回复按钮workingdemo解决方案就是不要为物品使用标签.而只是使用divHTML

  5. Angular 2:将值传递给路由数据解析

    我正在尝试编写一个DataResolver服务,允许Angular2路由器在初始化组件之前预加载数据.解析器需要调用不同的API端点来获取适合于正在加载的路由的数据.我正在构建一个通用解析器,而不是为我的许多组件中的每个组件设置一个解析器.因此,我想在路由定义中传递指向正确端点的自定义输入.例如,考虑以下路线:app.routes.ts在第一个实例中,解析器需要调用/path/to/resourc

  6. angularjs – 解释ngModel管道,解析器,格式化程序,viewChangeListeners和$watchers的顺序

    换句话说:如果在模型更新之前触发了“ng-change”,我可以理解,但是我很难理解在更新模型之后以及在完成填充更改之前触发函数绑定属性.如果您读到这里:祝贺并感谢您的耐心等待!

  7. 角度5模板形式检测形式有效性状态的变化

    为了拥有一个可以监听其包含的表单的有效性状态的变化的组件并执行某些组件的方法,是reactiveforms的方法吗?

  8. Angular 2 CSV文件下载

    我在springboot应用程序中有我的后端,从那里我返回一个.csv文件WheniamhittingtheURLinbrowsercsvfileisgettingdownloaded.现在我试图从我的角度2应用程序中点击此URL,代码是这样的:零件:服务:我正在下载文件,但它像ActuallyitshouldbeBook.csv请指导我缺少的东西.有一种解决方法,但您需要创建一个页面上的元

  9. angularjs – Angular UI-Grid:过滤后如何获取总项数

    提前致谢:)你应该避免使用jQuery并与API进行交互.首先需要在网格创建事件中保存对API的引用.您应该已经知道总行数.您可以使用以下命令获取可见/已过滤行数:要么您可以使用以下命令获取所选行的数量:

  10. angularjs – 迁移gulp进程以包含typescript

    或者我应该使用tsc作为我的主要构建工具,让它解决依赖关系,创建映射文件并制作捆绑包?

返回
顶部