版本:1.7.2
命令列表
名称 | 作用 |
---|---|
ng init | 在当前目录下创建应用 |
ng new | 创建新的目录并在新目录下运行ng init命令 |
ng serve | 启动 |
ng doc | 在浏览器中打开Angular文档并搜索当前关键字 |
ng e2e | 使用 protractor 在当前应用中运行e2e测试 |
ng format | 使用 clang-format 格式化当前项目代码 |
ng generate | 在项目中构建新代码 |
ng g | ng generate的简写 |
ng build | 构建项目 |
ng github-pages:deploy [options] | 将项目部署到GitHub Pages |
ng version | 输出cli版本, node 版本和操作系统信息 |
ng set | 设置环境 |
任何指令后面都可以加--help以查看帮助
ng new
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--style | string | 'css' | 使用 ('css', 'less' 还是 'scss')编写css |
--prefix | string | 'app' | 生成新组件的前缀 |
--skip-tests | boolean | false | 不创建测试文件 |
--dry-run | boolean | false | 只输出要创建的文件和执行的操作,实际上并没有创建项目 |
--force | boolean | false | 强制重写 |
--verbose | boolean | false | 输出详细信息 |
--skip-install | boolean | false | 在项目第一次创建时不执行npm install |
--skip-git | boolean | false | 不执行Git repository初始化工作 |
--directory | string | 目录名 | 默认与项目名称一样 |
--source-dir | string | 'src' | 源文件目录名 |
--routing | boolean | false | 添加带路由的模块(module)并import到main app module中 |
--inline-style | boolean | false | 生成新应用的时候使用inline styles(内联,不生成单独的css文件)方式 |
--inline-template | boolean | false | 生成新应用的时候使用 inline templates(内联,不生成单独的html文件)方式 |
--view-encapsulation | string | 'Emulated' | 指定 view encapsulation(样式封装) 的策略。None:没有Shadow Dom,样式没有封装,全局可以使用;Emulated:angular2的默认值,模仿 Shadow Dom,通过在标签上增加标识,来固定样式的作用域;Native:使用原生的Shadow Dom |
--skip-package-json | boolean | false | 不添加 dependencies 到 package.json 文件中 |
ng serve
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--host | string | 12.0.0.1 | 自定义配置 IP |
--disable-host-check | -- | -- | 跟在--host后面,允许使用localhost以外的host打开项目 |
--port | number | 4200 | 自定义端口号 |
--live-reload-port | number | 4200 | 时时重载端口号 |
--hmr | -- | -- | 热替换 |
ng generate支持的类型
名称 | 示例 |
---|---|
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 |
Class | ng g class my-new-class |
Interface | ng g interface my-new-interface |
Enum | ng g enum my-new-enum |
Module | ng g module my-module |
Route | ng g route my-route 当前已禁用 |
命令参数
ng generate class
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--spec | boolean | false | 生成对应的单元测试文件 |
ng generate component
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--flat | boolean | false | 在src/app中生成component文件,而不是在当前目录下 |
--inline-template | boolean | false | 使用inline template,而不是独立的HTML文件 |
--inline-style | boolean | false | 使用inline styles,而不是独立的CSS文件 |
--prefix | boolean | true | 使用在angular-cli.json指定的前缀来生成component selector(前缀表示selector的第一个单词) |
--spec | boolean | true | 生成对应的单元测试文件 |
ng generate directive
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--flat | boolean | false | 在src/app中生成component文件,而不是在当前目录下 |
--prefix | boolean | true | 默认true, 使用在angular-cli.json指定的前缀来生成component selector(前缀表示selector的第一个单词) |
--spec | boolean | true | 生成对应的单元测试文件 |
ng generate module
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--routing | boolean | false | 新增一个与module对应的路由模块,并import到module中 |
--spec | boolean | false | 用于检测模块是否存在 |
ng generate service
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--flat | boolean | true | 在src/app中生成component文件,而不是在当前目录下 |
--spec | boolean | true | 生成单元测试文件 |
ng build
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--aot | string | 无 | 预编译 |
--base-href | string | ‘/’ | 设置index.html文件中的base href参数 |
--environment | string | dev | 在哪个环境中使用,prod代表生成环境 |
--output-path | string | dist | 文件输出目标路径 |
--target | string | development | development:默认值,不压缩混淆,production:压缩混淆代码 |
--watch | boolean | false | 在哪个环境中使用 |
ng github-pages:deploy
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--message | string | 无 | 构建的时候提交的message,必须写在引号中 |
--target | string | development | 使用的环境 |
--user-page | boolean | false | 是否以user/org页面部署到GitHub上 |
--skip-build | boolean | false | 跳过构建直接部署 |
--gh-token | string | 无 | 使用Github的token |
--gh-username | string | 无 | 使用Github用户名 |
--base-href | string | '/' | 在index.html文件中使用的base href值 |
ng set
参数名 | 类型 | 默认值 | 作用 |
---|---|---|---|
--global | -- | 无 | 是否设置全局 |
packageManager | string | 无 | 可设置为npm\cnpm\yarn |
更多命令参数,可参考官网:https://github.com/angular/angular-cli/wiki