环境搭建&开发前准备
Node
安装 Node.js,用到其包管理工具:npm
// 查看镜像
npm config list
// 修改镜像
npm config set registry https://registry.npm.taobao.org
angular-cli
- 安装脚 手架工具:angular-cli
npm install -g @angular/cli
cli 工具的使用
新建工程
ng new PROJECT-NAME
新建模块
// Module 模块
ng g module my-module
// Component 组件
ng g component my-new-component
// Directive 指令
ng g directive my-new-directive
// Pipe 管道
ng g pipe my-new-pipe
// Service 服务
ng g service my-new-service
ES6简单语法介绍
this
箭头函数 =>
为什么叫Arrow Function?因为它的定义用的就是一个箭头:
x => x * x
上面的箭头函数相当于:
function (x) {
return x * x;
}
箭头函数相当于匿名函数,并且简化了函数定义。箭头函数有两种格式,一种像上面的,只包含一个表达式,连{ … }和return都省略掉了。还有一种可以包含多条语句,这时候就不能省略{ ... }
和return
:
x => {
if (x > 0) {
return x * x;
}
else {
return - x * x;
}
}
如果参数不是一个,就需要用括号()括起来:
// 两个参数:
(x, y) => x * x + y * y
// 无参数:
() => 3.14
// 可变参数:
(x, y, ...rest) => {
var i, sum = x + y;
for (i=0; i<rest.length; i++) {
sum += rest[i];
}
return sum;
}
如果要返回一个对象,就要注意,如果是单表达式,这么写的话会报错:
// SyntaxError:
x => { foo: x }
因为和函数体的{ … }有语法冲突,所以要改为:
// ok:
x => ({ foo: x })