Angular 小试牛刀[1]-Getting Started
首先,Angular2 与 Angular1.x 版本没有多大关系,甚至可以说是两个完全不一样的框架,故 Angular 指的是 Angular2 及以上的版本。而 Angular 与 TypeScript 的完美结合,对于一个 .NET 开发者来说,实在是找不到不用它的理由了,更多的优势来不及说了,快上车吧。 使用 angular-cli 初始化项目Angular CLI 是一个用于构建 Angular 项目的命令行界面工具,它可以创建项目、添加文件以及执行一大堆开发任务,比如测试、打包和发布。 设置开发环境在开始工作之前,我们必须设置好开发环境。 如果你的机器上还没有 Node.js?和npm,请先安装它们。
安装 angular-clinpm install -g @angular/cli@latest
查看 cli 命令ng help
生成项目模版ng new zero-admin-web --style=scss cd zero-admin-web ng server --运行 生成模版时常用的一些参数:
启动开发服务器进入项目目录,并启动开发服务器 cd zero-admin-web ng serve --open 默认端口是4200,在浏览器中打开 并且 生成发布包ng build --prod
build 用来生成压缩,优化后的JS,用来部署到生产环境,默认放成在 angular-cli常用指令可以使用 ng generate component my-component1 #生成一个名称为my-component1的组件 使用
项目结构先看看项目模版里的文件结构,以及他们都是做什么的: ┌── e2e │ ├── app.e2e-spec.ts │ ├── app.po.ts │ └── tsconfig.e2e.json ├── src │ ├── app │ │ ├── app.component.css │ │ ├── app.component.html │ │ ├── app.component.spec.ts │ │ ├── app.component.ts │ │ └── app.module.ts │ ├── assets │ │ └── .gitkeep │ ├── environments │ │ ├── environment.prod.ts │ │ └── environment.ts │ ├── favicon.ico │ ├── index.html │ ├── main.ts │ ├── polyfills.ts │ ├── styles.css │ ├── test.ts │ ├── tsconfig.app.json │ ├── tsconfig.spec.json │ └── typings.d.ts ├── .angular-cli.json ├── .editorconfig ├── .gitignore ├── karma.conf.js ├── package.json ├── protractor.conf.js ├── README.md ├── tsconfig.json └── tslint.json e2e/在 src/我们的应用代码位于 package.json这是项目的基本定义文件,所有的基于nodejs的项目都会有一个package.json文件,里面会定义项目名称、版本、依赖的库,以及脚本。 .angular-cli.jsonAngular CLI 的配置文件。 在这个文件中,可以设置一系列默认值,还可以配置项目编译时要包含哪些文件等。 karma.conf.js给 Karma 的单元测试配置,当运行 protractor.conf.js给*Protractor** 使用的端到端测试配置文件,当运行 tsconfig.jsonTypeScript 编译器的配置,IDE会借助它来为我们提供更好的代码提示,语法检查等。 tslint.json给 TSLint 和 Codelyzer 用的配置信息,当运行 Angular MaterialMaterial Design,中文名:材料设计语言,是由Google推出的全新的设计语言,谷歌表示,这种设计语言旨在为手机、平板电脑、台式机和“其他平台”提供更一致、更广泛的“外观和感觉”。 据谷歌介绍,Material Design基于“真实的触感,灵感源自对纸和墨水的研究,” 能够让用户 “理解那些用于替代真实世界的可视线索”,“而又不违背力学原理”。另外,光线、表面和移动的基本原理是表现对象如何移动、交互和相互关联地存在于空间中的关键。逼真的光影效果可以显示区块间的接缝、划分空间、以及标识移动的部件。更多详细的介绍可以看 Material 的官网 https://material.io。 而 Angular Material 则是由 Angular 团队完全基于 Material 规范,完美实现的一套 Angular2+ UI框架。 安装配置步骤第一步:安装使用 npm install --save @angular/material @angular/cdk
注: CDK是(Component Dev Kit)的缩写,Angular 的组件开发包,用于分离组件开发与 Material 的耦合(目前大多还耦合在一起),以方便使用其它风格的设计来开发组件。 第二步:动画有一些 Angular Material 组件依赖于 Angular 动画模块,以便能进行一些高级的 transitions。如果你希望使用这些动画,则需要安装 npm install --save @angular/animations
导入动画模块 import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; @NgModule({ ... imports: [BrowserAnimationsModule],... }) export class AppModule { } 第三步:导入导入你想要使用的 Angular Material 模块 import {MdButtonModule,MdCheckboxModule} from '@angular/material'; @NgModule({ ... imports: [MdButtonModule,MdCheckboxModule],... }) export class AppModule { } 第四步:主题定制一个主题,所有的组件都将遵循于这个主题。Angular Material 内置了几种主题,我们可以在 @import "~@angular/material/prebuilt-themes/indigo-pink.css"; 更多主题的介绍以及如何自定义主题,则可以参考 theming guide。 第五步:手势有些组件,如( 使用 npm install --save hammerjs
导入到根模块中: import 'hammerjs'; 第六步:图标如果你想通过 添加 @font-face { font-family: 'Material Icons'; font-style: normal; font-weight: 400; src: local('Material Icons'),local('MaterialIcons-Regular'),url(../fonts/MaterialIcons-Regular.woff2) format('woff2'),/* Super Modern Browsers */ url(../fonts/MaterialIcons-Regular.woff) format('woff'),/* Super Modern Browsers */ url(../fonts/MaterialIcons-Regular.ttf) format('truetype'),/* Safari,Android,iOS */ url(../fonts/MaterialIcons-Regular.svg) format('svg'); /* Legacy iOS */ } .material-icons { font-family: 'Material Icons'; font-weight: normal; font-style: normal; font-size: 24px; /* Preferred icon size */ display: inline-block; line-height: 1; text-transform: none; letter-spacing: normal; word-wrap: normal; white-space: nowrap; direction: ltr; /* Support for all WebKit browsers. */ -webkit-font-smoothing: antialiased; /* Support for Safari and Chrome. */ text-rendering: optimizeLegibility; /* Support for Firefox. */ -moz-osx-font-smoothing: grayscale; /* Support for IE. */ font-feature-settings: 'liga'; } /* Rules for sizing the icon. */ .material-icons.md-18 { font-size: 18px; } .material-icons.md-24 { font-size: 24px; } .material-icons.md-36 { font-size: 36px; } .material-icons.md-48 { font-size: 48px; } /* Rules for using icons as black on a light background. */ .material-icons.md-dark { color: rgba(0,0,0.54); } .material-icons.md-dark.md-inactive { color: rgba(0,0.26); } /* Rules for using icons as white on a dark background. */ .material-icons.md-light { color: rgba(255,255,1); } .material-icons.md-light.md-inactive { color: rgba(255,0.3); } 然后在 <!-- Icons --> <link href="assets/css/material-icons.css" rel="stylesheet"> 字体文件下载地址为:material-design-icons。 不过更加推荐的是直接使用 Google 提供的 CDN 服务(现在国内访问速度非常快),只需要在 index.html 中添加如下引用即可: <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 总结本文简单描述了如何使用 angular-cli 初始化项目和 material2 UI框架的配置,比起 webpack 那一大堆配置,angular-cli 则要简洁的多,而 angular-cli 生成的那么多文件,对于新手来说,可能有些还是不太理解,这个在后续会慢慢介绍,不要着急,前期不应过于执着细节,而是要先愉快的跑起来。而下一章则会介绍一下Angular项目的持续集成。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |