• angularjs 2.0 快速开始


    前言

    angularjs2.0 如果发布,公司的项目会基于2.0开发,在1.0的时候就踩了好多坑,趁这2.0还没正式发布,赶紧踩下坑。 这篇文章是参考angularjs2.0 官方文档写的,开发环境需要机器上有 nodejs环境,另外2.0 是基于typescript 开发,微软开发一种语言 ,附上中文手册链接 大家可以自行研究下..

    开发环境的搭建

    1.创建项目文件夹

    由于我是在window 上开发,机器上装了Git Bash 所以我直接用命令来构建

    mkdir angular2-quickstart
    cd angular2-quickstart
    
    2.添加一个 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"
      ]
    }
    

    这个文件是 typescript编译指南文件,也就是说这事 ts 的编译配置文件,关于更多 Typescript Configuration

    2.添加一个 typings.json 文件
    {
      "ambientDependencies": {
        "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
        "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
      }
    }
    

    一些js库扩展了JavaScript的特性和语法,但是TypeScript编译器并不识别,因此需要在typings.json文件中配置TypeScript类型定义文件(文件名后缀为.d.ts)

    2.添加一个 package.json 文件
    {
      "name": "angular2-quickstart",
      "version": "1.0.0",
      "scripts": {
        "start": "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.13",
        "systemjs": "0.19.25",
        "es6-shim": "^0.35.0",
        "reflect-metadata": "0.1.2",
        "rxjs": "5.0.0-beta.2",
        "zone.js": "0.6.6"
      },
      "devDependencies": {
        "concurrently": "^2.0.0",
        "lite-server": "^2.1.0",
        "typescript": "^1.8.9",
        "typings":"^0.7.11"
      }
    }
    

    angularjs 用 npm 安装所有的依赖包,我们用 npm install 来安装 。安装过程可能会出错,建议FQ,如果npm出现 关于 typings 的错误,可能是你没有安装,这时候我们可以用命令npm install typings --global 去安装

    package.json 文件中定义了一些脚本

    • npm start 在观察模式下 ,运行编译和启动服务
    • npm run tsc 运行 typescript 编译器
    • npm run tsc:w 在观察模式下运行typescript编译器(监控 后缀.ts 文件的变化,文件一旦变化就会重新编译)
    • npm run lite 运行 lite-server 一个轻量级的静态文件服务器,用于支撑angular 的路由
    • npm run typings 运行 typings tool
    • npm run postinstall npm 安装成功后自动调用 ,这个脚本安装 定义在 typings.json 文件当中

    第一个angular组件( Angular2.0 Hello World)

    创建一个app文件夹,并添加 app.component.ts 文件
    import {Component} from 'angular2/core';     //导入一个模块
    //组件是一个装饰功能,它需要一个元数据对象.元数据告诉angular如何创建和使用这个组件。      
    //通过用前缀@调用:
    @Component({
    	selector:'my-app',     //值为一个css选择器,组件元素的名称为my-app,Angular会为HTML中的my-app元素创建并显示AppComponent实例
    	template:'<h1>Angular 2 hello wold</h1>' //指定对应的模板,以及数据绑定
    })
    
    export class AppComponent { }  //导出 AppComponent 
    

     每个angular的应用都有至少一根组件,通常叫AppComponent. 组件是angular应用程序的基本构建块。

    创建main.ts 文件
    import {bootstrap}    from 'angular2/platform/browser';    //导入bootstrap 模块,用来启动angular
    import {AppComponent} from './app.component';   //导入我们应用的根组件
    bootstrap(AppComponent);   //传入我们的根组件启动angular
    
    创建index.html 文件
    <html>
      <head>
        <title>Angular 2 hello world</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">    
        <!-- 1. Load libraries (加载一些公用的js) -->
        <!-- 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 (配置系统js) -->
        <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>
    

    启动

    运行我们的命令npm start ,浏览器会自动帮我们打开 地址localhost:3000
    我们就能看到 我们的程序了,是不是很激动呢。

    结束语

    angular2.0 相比1.0 简单了好多,使我们入门更加容易,大家赶快来试试吧,有哪里走不通的,欢迎留言,我及时解答,源码没上传github,如需源码 ,请@我。

  • 相关阅读:
    剑指office--------重建二叉树
    剑指office--------二进制中1的个数
    剑指office--------最小的K个数 (待补充)
    剑指office--------二维数组的查找
    剑指office--------替换空格
    Redis集群
    一致性hash算法
    Zab协议(转)
    Redis线程模型
    Http Cookie和session
  • 原文地址:https://www.cnblogs.com/qiaojie/p/5355307.html
Copyright © 2020-2023  润新知