Angular 2 CLI – Build Angular 2 apps using Command Line Interface
来源:互联网 发布:传奇物品数据库文件名 编辑:程序博客网 时间:2024/05/17 22:00
Angular 2 CLI aka Command Line Interface is developed to get started quickly for building Angular 2 apps, especially when the entire community felt that setting up Angular 2 development environment was cumbersome.
With introduction of Angular CLI, it’s now easier than ever to create, run builds, do E2E (end to end) tests, run apps and deploy Angular 2 application.
In this article, I will build a very basic Angular 2 application exclusively using CLI. So lets started.
What we will learn here?
- Installing Angular 2 CLI using NPM.
- Creating an Angular 2 application using command line interface
- Examine CLI created project structure.
- Serve or Run Angular 2 application.
- Create models and services to work with data.
- Create Dashboard page using router
Installing Angular 2 CLI using NPM
Ensure you have latest NPM and Node installed on your machine. After that run this command to install Angular 2 CLI globally. After installation just verify as shown in figure
Creating an Angular 2 application using command line interface
We will be creating a simple “OurPlanets” application displaying list of our solar system planets
Run the below command in prompt to create new Angular 2 app. “OurPlanets” is application, the –prefix option tells us that “Planets” will be added as prefix for project files.
- CLI command – ng new to create application
- List of files created using CLI command
- The newly created application is now GIT repository by default.
- As the package.json is already created, CLI command restore the packages. It takes few minutes to restore packages.
Examine CLI created “OurPlanets” project structure in Visual Studio Code
Open Visual Studio code, load this project. We will check out project structure got created for us.
- “Config” folder containing configuration related environment, karma, protractor
- “e2e” folder containing test files, configurations for performing end to end testing.
- “node_modules” folder contains all packages restored as per package.json
- “public” folder is location to hold images, static assets
- “src” folder is the main application development folder containing template HTML files, TS files, components.
- “packages.json” essential file which contains reference to all packages needed for running Angular 2 app. See .gitignore file also. Automatically “OurPlanets” application is GIT repo, which we can push it if needed.
Isn’t it amazing just by running “ng new” command of Angular 2 CLI gives us so much stuff to get started.
Serve or run “OurPlanets” Angular 2 apps
Now that we have app with all dependencies, build it and run as shown in figure.
Note: ng build command creates “dist/”, a folder containing compiled, minified (if applied) your Angular 2 application.
- ng serve command runs and listens on location, but doesn’t open any browser. If you wish you can use lite-server for it.
- “public” folder has semantic.min.css and themes folder which are moved to “dist” folder. Its not provided by Angular 2 CLI steps, I have added explicitly.
Create Planets model and PlanetService using CLI
As “OurPlanets” application is about solar system planets, it’s time to create model and service to get planets list and its details in form of planet.
Model refers to class structure containing properties just like C#, Java.
Run the following commands to create “planet.model” model class and “planet.service” service.
Note: class generating command lets have suffix with ‘model’, service generating command creates file with ‘service’ suffix.
CLI also generates spec TS files used for unit testing
Open planets.model.ts file & copy below code, its really simple class with four fields.
Open planets.service.ts file to copy below code; it imports ‘planets.model’, getPlanets method which returns list of planets data. Nothing fancy, but still good enough
Create Dashboard page using Router
Until now, we created Angular 2 app, added planets.model and planets.service. It’s time to create router. Run the following command to create and later see the magic !!
The above shows only files created by ng generate route command, but CLI magic takes place in following files
- our-planets.component.html — router-outlet is added.
- system-config.ts — Dashboard is added to Angular 2 barrels (It helps to keep clean component code)
- our-planets.components.ts – Major magic happens here, Angular 2 CLI adds required router imports, adds directives, providers in @component, sets path in @Routes. Check out git difference of this file.
Now we are almost there, we have Angular 2 app setup with components, a sample service with model, a router to display planets. Lets add up all code required file by file.
our-planets.component.ts
our-planets.component.html
dashboard.component.html – Displays UI on click on Dashboard link
dashboard.component.ts – imports Planets, PlanetsService to get list of planets
We finished our sample Angular 2 app coding, now lets run ng build and ng serve to see it working. We can do live editing with CLI
There are many more Angular 2 CLI commands with interesting options, will explore them.
原文链接: http://www.mithunvp.com/build-angular-apps-using-angular-2-cli/
- Angular 2 CLI – Build Angular 2 apps using Command Line Interface
- Angular CLI简介2
- 如何在 WebStorm 使用 Angular CLI 建立 Angular 2
- 使用Angular-cli搭建Angular Material 2应用示例
- angular-cli安装及build项目
- angular-cli安装及build项目
- 简单的CLI(command line interface)
- 3DSlicer13:Command Line Interface(CLI)
- Angular CLI is here for AngularJS 2
- Angular-cli
- angular-cli
- angular-cli
- angular-cli
- post, get using Angular 2[Preview]
- Protecting Routes using Guards in Angular 2
- Angular 2 CRUD application using Nodejs
- angular-cli(material.angular.io)
- angualar angular-cli angular命令
- hd1236 排名
- 第三周第二题--基于Secondary Sort
- GDataXMLNode头文件报错
- 【嵌入式学习日记】2016年7月18日
- .net {"已添加项。字典中的关键字:“**”所添加的关键字:“**”"}
- Angular 2 CLI – Build Angular 2 apps using Command Line Interface
- iOS开中设置UITabBar的背景图片以及各个item的选中图片和未选中
- MVVM的简单使用
- GPIO:推挽(Push-Pull) vs 开漏(Open-Drain)
- Spring中使用xml配置bean的细节
- 评估邮件网关是否优质的五个标准
- ScrollView嵌套ListView滑动冲突解决
- poj3294Life Forms-后缀数组
- iOS中调用相机时选取图片的时候将因问改为中文的方法