|
| 1 | +--- |
| 2 | +title: Angular CLI |
| 3 | +description: Learn how to use the Angular CLI in your NativeScript app with @nativescript/schematics. The extension to Angular Schematisc allows you to use the Angular CLI in both NativeScript Angular projects and web+mobile code-sharing projects. |
| 4 | +position: 80 |
| 5 | +environment: angular |
| 6 | +--- |
| 7 | + |
| 8 | +# Angular CLI |
| 9 | + |
| 10 | +The **Angular CLI** makes it easy to create an Angular application and generate core building blocks, like **components**, **modules**, **services**, etc. |
| 11 | + |
| 12 | +With the help of the [NativeScript Schematics](https://www.npmjs.com/package/@nativescript/schematics), you can also reap the benefits of the [Angular CLI](https://www.npmjs.com/package/@angular/cli) in your **Angular NativeScript** projects. |
| 13 | + |
| 14 | +The **NativeScript Schematics** extend the **Angular Schematics**, by providing NativeScript-specific functionality to the existing **Angular generators**. |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +In order to use the Angular CLI in a NativeScript project, you need both the **Angular CLI** and the **NativeScript Schematics**. |
| 19 | + |
| 20 | +> Note, you need to use **@angular/cli** version **6.1** or newer. |
| 21 | +
|
| 22 | +### Global Installation |
| 23 | + |
| 24 | +You can either install them globally, which would allow you to create new projects: |
| 25 | + |
| 26 | +```bash |
| 27 | +npm i -g @angular/cli |
| 28 | +npm i -g @nativescript/schematics |
| 29 | +``` |
| 30 | + |
| 31 | +### Local Installation |
| 32 | + |
| 33 | +Or you could install them as a part of each project: |
| 34 | + |
| 35 | +```bash |
| 36 | +npm i --save-dev @angular/cli |
| 37 | +npm i --save-dev @nativescript/schematics |
| 38 | +``` |
| 39 | + |
| 40 | +## Default configuration |
| 41 | + |
| 42 | +All Angular projects created with the **NativeScript CLI 5.0** come preconfigured and ready to work with the **NativeScript Schematics** and the **Angular CLI**. |
| 43 | + |
| 44 | +This is done by added: |
| 45 | + |
| 46 | +- the **@nativescript/schematics** node module to **package.json** |
| 47 | +- an **angular.json** file configured to work with **@nativescript/schematics** |
| 48 | + |
| 49 | +However, you will still need to have the **Angular CLI** installed either [globally](./angular-cli#global-installation) or [locally](./angular-cli#local-installation). |
| 50 | + |
| 51 | +### Extending existing NativeScript Projects with the Angular CLI |
| 52 | + |
| 53 | +For projects created with the NativeScript CLI v4 or older, you will need to |
| 54 | + |
| 55 | +To introduce the Angular CLI to an **existing** NativeScript Angular Project, you need to add an **angular.json** file (which is used by the Angular CLI to understand the structure of the project) to the root of the project. The **angular.json** file should have the following content: |
| 56 | + |
| 57 | +```json |
| 58 | +{ |
| 59 | + "version": 1, |
| 60 | + "cli": { |
| 61 | + "defaultCollection": "@nativescript/schematics" |
| 62 | + }, |
| 63 | + "projects": { |
| 64 | + "my-project-name": { |
| 65 | + "root": "", |
| 66 | + "sourceRoot": "src", |
| 67 | + "projectType": "application", |
| 68 | + "prefix": "ns" |
| 69 | + } |
| 70 | + }, |
| 71 | + "defaultProject": "my-project-name" |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +The **defaultCollection** is where you specify that you want to use the **NativeScript Schematics** when generating building blocks with the Angular CLI. |
| 76 | + |
| 77 | +You could update: |
| 78 | + |
| 79 | +- **my-project-name** to match the name of your project, but that is not absolutely necessary, |
| 80 | +- **prefix** to match the prefix for your component selectors (i.e. `"prefix": "app"` => `selector: 'app-home'`). |
| 81 | + |
| 82 | +## New Project |
| 83 | + |
| 84 | +To create a new NativeScript Angular project with the Angular CLI, you need to call the `ng new` command with the `--collection=@nativescript/schematics` parameter. Like this: |
| 85 | + |
| 86 | +```bash |
| 87 | +ng new --collection=@nativescript/schematics my-mobile-app |
| 88 | +``` |
| 89 | + |
| 90 | +or with the `-c` shorthand: |
| 91 | + |
| 92 | +```shorthand |
| 93 | +ng new -c=@nativescript/schematics my-mobile-app |
| 94 | +``` |
| 95 | + |
| 96 | +This notifies the Angular CLI that it should use the **ng-new** schematic from the **@nativescript/schematics** npm package. Which in turn creates a new NativeScript Angular project and performs an npm install. |
| 97 | + |
| 98 | +> Note, the NativeScript projects created with the Angular CLI come with the Angular CLI and NativeScript Schematics **already configured**. There is no need to add the **angular.json** file manually. |
| 99 | +
|
| 100 | +### Additional Options |
| 101 | + |
| 102 | +You can specify the following options when generating new applications: |
| 103 | + |
| 104 | +| Option | Description | Default | |
| 105 | +| ------- | --------------------------------------------------------------------- | ------- | |
| 106 | +| prefix | The prefix to apply to generated selectors. | `app` | |
| 107 | +| theme | Specifies whether the **NativeScript Core Theme** should be included. | `true` | |
| 108 | +| style | Specifies whether to use `css` or `scss` files for styling. | `css` | |
| 109 | +| webpack | Specifies whether the project should use webpack. | `true` | |
| 110 | + |
| 111 | +Here is an example on how you could provide a different value for each of the flags: |
| 112 | + |
| 113 | +```bash |
| 114 | +ng new -c=@nativescript/schematics my-app-name --prefix=my --no-theme --style=scss --no-webpack |
| 115 | +``` |
| 116 | + |
| 117 | +### Code-Sharing Projects |
| 118 | + |
| 119 | +You can create a new **code-sharing project**, which allows you to build for both web and mobile, by providing a `--shared` flag. Like this: |
| 120 | + |
| 121 | +```bash |
| 122 | +ng new -c=@nativescript/schematics my-app-name --prefix=my --no-theme --style=scss --no-webpack |
| 123 | +``` |
| 124 | + |
| 125 | +You can learn more about code sharing in the [code-sharing section of the docs](../code-sharing/intro). |
| 126 | + |
| 127 | +## Generators |
| 128 | + |
| 129 | +One of the biggest benefits of the Angular CLI is the rapid scaffolding of the application. |
| 130 | +It allows you to generate **components**, **routes**, **services** and **pipes** with a simple `ng generate` (or `ng g` for short) command. |
| 131 | + |
| 132 | +> Feel free to check out the [official Angular CLI documentation for the generate command](https://github.com/angular/angular-cli/wiki/generate), as there is a lot of available options. |
| 133 | +
|
| 134 | +### Component |
| 135 | + |
| 136 | +To generate a new component, you need to use the **component schematic**. Like this: |
| 137 | + |
| 138 | +```long |
| 139 | +ng generate component my-name |
| 140 | +``` |
| 141 | + |
| 142 | +```short |
| 143 | +ng g c my-name |
| 144 | +``` |
| 145 | + |
| 146 | +#### NativeScript Only Project |
| 147 | + |
| 148 | +In a NativeScript Only project (_not code-sharing_), this command: |
| 149 | + |
| 150 | +- creates component files: |
| 151 | + _ **my-name.component.ts** - a class file, |
| 152 | + _ **my-name.component.html** - a template file, \* and **my-name.component.css** or **my-name.component.scss** - a style file, |
| 153 | +- adds the component to the **AppModule Providers**, |
| 154 | +- adds `moduleId: module.id` to the `@Component` decorator - which is the **modification introduced** by the NativeScript Schematics. |
| 155 | + |
| 156 | +The component files should be generated in the **app/my-name** folder, like this: |
| 157 | + |
| 158 | +``` |
| 159 | +app |
| 160 | +└── my-name |
| 161 | + ├── my-name.component.css |
| 162 | + ├── my-name.component.html |
| 163 | + └── my-name.component.ts |
| 164 | +``` |
| 165 | + |
| 166 | +#### Code-Sharing Project |
| 167 | + |
| 168 | +In a Code-Sharing Project, this command: |
| 169 | + |
| 170 | +- creates **shared** and **platform-specific** component files: |
| 171 | + _ **my-name.component.ts** - a **shared** class file, |
| 172 | + _ **my-name.component.html** - a **web-specific** template file, |
| 173 | + _ **my-name.component.html** - a **NativeScript-specific** template file, |
| 174 | + _ **my-name.component.css** - a **web-specific** style file, \* and **my-name.component.tns.css** - a **NativeScript-specific** style file, |
| 175 | +- adds the component to the **AppModule Providers** of both: |
| 176 | + _ **app.module.ts** - the **web-specific** AppModule file, |
| 177 | + _ and **app.module.tns.ts** - the **nativescript-specific** AppModule file. |
| 178 | + |
| 179 | +The component files should be generated in the **src/app/my-name** folder, like this: |
| 180 | + |
| 181 | +``` |
| 182 | +src |
| 183 | +└── app |
| 184 | + └── my-name |
| 185 | + ├── my-name.component.css |
| 186 | + ├── my-name.component.tns.css |
| 187 | + ├── my-name.component.html |
| 188 | + ├── my-name.component.ts |
| 189 | + └── my-name.component.tns.ts |
| 190 | +``` |
| 191 | + |
| 192 | +### Module |
| 193 | + |
| 194 | +To generate a new module, you need to use the **module schematic**. Like this: |
| 195 | + |
| 196 | +```long |
| 197 | +ng generate module my-name |
| 198 | +``` |
| 199 | + |
| 200 | +```short |
| 201 | +ng g m my-name |
| 202 | +``` |
| 203 | + |
| 204 | +#### NativeScript Only Project |
| 205 | + |
| 206 | +In a NativeScript Only project, this command creates the module file: **my-name.module.ts** with the following contents: |
| 207 | + |
| 208 | +``` |
| 209 | +import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core'; |
| 210 | +import { NativeScriptCommonModule } from 'nativescript-angular/common'; |
| 211 | +
|
| 212 | +@NgModule({ |
| 213 | + imports: [ |
| 214 | + NativeScriptCommonModule |
| 215 | + ], |
| 216 | + declarations: [], |
| 217 | + schemas: [NO_ERRORS_SCHEMA] |
| 218 | +}) |
| 219 | +export class MyNameModule { } |
| 220 | +``` |
| 221 | + |
| 222 | +The most notable changes introduced by the NativeScript Schematics, are: |
| 223 | + |
| 224 | +- the import of the **NativeScriptCommonModule**, as opposed to the **CommonModule** used by the Angular web projects, |
| 225 | +- and the inclusions of the **NO_ERRORS_SCHEMA** schema, which is required to allow using the NativeScript specific selectors (i.e. **StackLayout**). |
| 226 | + |
| 227 | +The module file should be generated in the **src/app/my-name** folder, like this: |
| 228 | + |
| 229 | +``` |
| 230 | +app |
| 231 | +└── my-name |
| 232 | + └── my-name.module.ts |
| 233 | +``` |
| 234 | + |
| 235 | +#### Code-Sharing Project |
| 236 | + |
| 237 | +In a Code-Sharing Project, this command creates: |
| 238 | + |
| 239 | +- **my-name.module.ts** - a **web-specific** module file, which imports the **CommonModule** |
| 240 | +- **my-name.module.tns.ts** - a **NativeScript-specific** module file, which imports the **NativeScriptCommonModule** and includes the **NO_ERRORS_SCHEMA** schema, |
| 241 | +- and **my-name.common.ts** - a **shared** file with constants that allow you to easily share the declarations for the components, providers and routes used by both modules. |
| 242 | + |
| 243 | +The module files should be generated in the **src/app/my-name** folder, like this: |
| 244 | + |
| 245 | +``` |
| 246 | +src |
| 247 | +└── app |
| 248 | + └── my-name |
| 249 | + ├── my-name.common.ts |
| 250 | + ├── my-name.module.ts |
| 251 | + └── my-name.module.tns.ts |
| 252 | +``` |
| 253 | + |
| 254 | +### Bonus: Generate Components inside a Module |
| 255 | + |
| 256 | +You can also generate components inside other modules. This works the same for the NativeScript Only and Code-Sharing projects. |
| 257 | + |
| 258 | +To do that just provide the module name before the component name, like this: |
| 259 | + |
| 260 | +```bash |
| 261 | +ng g m pets |
| 262 | +ng g c pets/cat |
| 263 | +ng g c pets/dog |
| 264 | +``` |
| 265 | + |
| 266 | +These three commands will: |
| 267 | + |
| 268 | +1. Generate a **pets module**. |
| 269 | +2. Generate a **cat component** inside the pets module and it will add it to the pets module declarations. |
| 270 | +3. Generate a **dog component** inside the pets module and it will add it to the pets module declarations. |
| 271 | + |
| 272 | +## Additional Template Generators |
| 273 | + |
| 274 | +The **NativeScript Schematics** also come with additional templates that help you build your apps faster. |
| 275 | + |
| 276 | +### Master Detail template |
| 277 | + |
| 278 | +To generate a Master Detail template, you can use the following command: |
| 279 | +`ng g master-detail --master=dogs --detail=dog` |
| 280 | + |
| 281 | +This command generates the following file structure: |
| 282 | + |
| 283 | +``` |
| 284 | +dogs |
| 285 | +└── dog-detail |
| 286 | +| └── <dog-detail component files> |
| 287 | +├── dogs |
| 288 | +| └── <dogs component files> |
| 289 | +├── data.service.ts |
| 290 | +└── dogs.module.ts |
| 291 | +``` |
| 292 | + |
| 293 | +#### Options |
| 294 | + |
| 295 | +| Option | Description | |
| 296 | +| ------ | ------------------------------------------------------------ | |
| 297 | +| master | The name of the master component and the name of the module. | |
| 298 | +| detail | The name of the detail component | |
0 commit comments