angular7常用命令总结

项目搭建步骤

  • 安装nodejs
  • 安装cnpm即淘宝镜像
  • 使用npm/cnpm命令安装angular/cli(只需要安装一次)
  • 创建项目的两种方式:
    • 直接安装 ng new angulardemo
    • 跳过npm i安装:ng new angulardeom --skip-install,之后手动安装依赖
  • ng serve --open 启动服务
错误处理:
1. npm安装angular/cli失败
	解决方案:用cnpm安装
2. 创建项目npm i 的时候失败
	解决方案1: ctrl+c结束项目,cd到项目里面,用cnpm i安装依赖
	解决方案2:创建项目的时候, --skip-install
		ng new my-app --skip-install
		cd my-app
		cnpm install

常用命令

  1. 查看angular-cli的版本号:ng -v
  2. 创建项目:ng new app(项目名称)
  3. 下载依赖包:npm install
  4. 删除依赖包:
    • npm install rimraf -g
    • rimraf node_modules
  5. 创建组件:ng g c MyComponent(组件名称)
	// 创建组件的时候也可以带路径,比如:ng generate component mydir/MyComponent
  1. 创建service服务 :ng g s MyService
  2. 动态生成服务,并把这个服务提供给依赖注入系统: ·ng g service 服务名 --module=app·
  3. 动态生成类文件: ng g class 类名
  4. 动态生成接口文件: ng g i 接口名
  5. 创建module : ng g m teacher
  6. 动态生成管道,并把这个管道在module中声明: ng g p 管道名
  7. 新建路由文件: ng g module trade --routing
    10.动态生成组件,并把这个组件导入module中: ng g c 组件名
  8. 不生成对应的单元测试文件: ng g c core/header --spec false
  9. 创建guard(一般用于写权限拦截): ng g g service/login/login
  10. 自动创建指令:ng g d MyDirective
  11. 运行项目: ng serve
  12. 停止项目:ctrl +c 输入y
  13. 构建项目:ng build
  14. 如果你想构建最终的产品版本(进行优化,包压缩等),可以用 ng build –prod
  15. 添加路由: ng generate module app-routing --flat --module=app

版权声明:本文为yw00yw原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。