You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 17, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: docs/tooling/angular-cli.md
+70-61Lines changed: 70 additions & 61 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: Angular CLI
3
-
description: Learn how to use the Angular CLI in your NativeScript app.
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
4
position: 80
5
5
environment: angular
6
6
---
@@ -11,7 +11,7 @@ The **Angular CLI** makes it easy to create an Angular application and generate
11
11
12
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
13
14
-
The **NativeScript Schematics** extend the **Angular Schematics**, by providing NativeScript-specific functionality and changes to the default**component** and **module** generators.
14
+
The **NativeScript Schematics** extend the **Angular Schematics**, by providing NativeScript-specific functionality to the existing**Angular generators**.
15
15
16
16
## Installation
17
17
@@ -37,34 +37,47 @@ npm i --save-dev @angular/cli
37
37
npm i --save-dev @nativescript/schematics
38
38
```
39
39
40
-
## Extending existing NativeScript Projects with the Angular CLI
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
41
54
42
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:
43
56
44
57
```json
45
58
{
46
-
"version": 1,
47
-
"cli": {
48
-
"defaultCollection": "@nativescript/schematics"
49
-
},
50
-
"projects": {
51
-
"my-project-name": {
52
-
"root": "",
53
-
"sourceRoot": ".",
54
-
"projectType": "application",
55
-
"prefix": "app"
56
-
}
57
-
},
58
-
"defaultProject": "my-project-name"
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"
59
72
}
60
73
```
61
74
62
-
The **defaultCollection** is where you specify that you want to use the **NativeScript Schematics**, as the default **Schematics collection** for your project.
75
+
The **defaultCollection** is where you specify that you want to use the **NativeScript Schematics** when generating building blocks with the Angular CLI.
63
76
64
77
You could update:
65
78
66
-
***my-project-name** to match the name of your project, but that is not absolutely necessary,
67
-
***prefix** to match the prefix for your component selectors (i.e. `"prefix": "app"` => `selector: 'app-home'`).
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'`).
68
81
69
82
## New Project
70
83
@@ -82,18 +95,18 @@ ng new -c=@nativescript/schematics my-mobile-app
82
95
83
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.
84
97
85
-
> 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.
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.
86
99
87
100
### Additional Options
88
101
89
102
You can specify the following options when generating new applications:
90
103
91
-
| Option | Description | Default
92
-
| --- | --- | --- |
93
-
| prefix | The prefix to apply to generated selectors. |`app`|
94
-
| theme | Specifies whether the **NativeScript Core Theme** should be included. |`true`|
95
-
| style | Specifies whether to use `css` or `scss` files for styling. |`css`|
96
-
| webpack | Specifies whether the project should use webpack. | `true`
| 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`|
97
110
98
111
Here is an example on how you could provide a different value for each of the flags:
99
112
@@ -118,29 +131,27 @@ It allows you to generate **components**, **routes**, **services** and **pipes**
118
131
119
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.
120
133
121
-
In the **NativeScript Schematics** we have extended the schematics for generating **components** and **modules**. The schematics to generate **services**, **pipes**, etc remain the same.
122
-
123
134
### Component
124
135
125
136
To generate a new component, you need to use the **component schematic**. Like this:
126
137
127
138
```long
128
-
ng generate component my-name
139
+
ng generate component my-name
129
140
```
141
+
130
142
```short
131
-
ng g c my-name
143
+
ng g c my-name
132
144
```
133
145
134
146
#### NativeScript Only Project
135
147
136
-
In a NativeScript Only project (*not code-sharing*), this command:
137
-
138
-
* creates component files:
139
-
* **my-name.component.ts** - a class file,
140
-
* **my-name.component.html** - a template file,
141
-
* and **my-name.component.css** or **my-name.component.scss** - a style file,
142
-
* adds the component to the **AppModule Providers**,
143
-
* adds `moduleId: module.id` to the `@Component` decorator - which is the **modification introduced** by the NativeScript Schematics.
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.
144
155
145
156
The component files should be generated in the **app/my-name** folder, like this:
146
157
@@ -155,16 +166,15 @@ app
155
166
#### Code-Sharing Project
156
167
157
168
In a Code-Sharing Project, this command:
158
-
159
-
* creates **shared** and **platform-specific** component files:
160
-
* **my-name.component.ts** - a **shared** class file,
161
-
* **my-name.component.html** - a **web-specific** template file,
162
-
* **my-name.component.html** - a **NativeScript-specific** template file,
163
-
* **my-name.component.css** - a **web-specific** style file,
164
-
* and **my-name.component.tns.css** - a **NativeScript-specific** style file,
165
-
* adds the component to the **AppModule Providers** of both:
166
-
* **app.module.ts** - the **web-specific** AppModule file,
167
-
* and **app.module.tns.ts** - the **nativescript-specific** AppModule file.
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.
168
178
169
179
The component files should be generated in the **src/app/my-name** folder, like this:
170
180
@@ -184,10 +194,11 @@ src
184
194
To generate a new module, you need to use the **module schematic**. Like this:
185
195
186
196
```long
187
-
ng generate module my-name
197
+
ng generate module my-name
188
198
```
199
+
189
200
```short
190
-
ng g m my-name
201
+
ng g m my-name
191
202
```
192
203
193
204
#### NativeScript Only Project
@@ -210,8 +221,8 @@ export class MyNameModule { }
210
221
211
222
The most notable changes introduced by the NativeScript Schematics, are:
212
223
213
-
* the import of the **NativeScriptCommonModule**, as opposed to the **CommonModule** used by the Angular web projects,
214
-
* and the inclusions of the **NO_ERRORS_SCHEMA** schema, which is required to allow using the NativeScript specific selectors (i.e. **StackLayout**).
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**).
215
226
216
227
The module file should be generated in the **src/app/my-name** folder, like this:
217
228
@@ -225,9 +236,9 @@ app
225
236
226
237
In a Code-Sharing Project, this command creates:
227
238
228
-
***my-name.module.ts** - a **web-specific** module file, which imports the **CommonModule**
229
-
***my-name.module.tns.ts** - a **NativeScript-specific** module file, which imports the **NativeScriptCommonModule** and includes the **NO_ERRORS_SCHEMA** schema,
230
-
* 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.
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.
231
242
232
243
The module files should be generated in the **src/app/my-name** folder, like this:
233
244
@@ -240,7 +251,6 @@ src
240
251
└── my-name.module.tns.ts
241
252
```
242
253
243
-
244
254
### Bonus: Generate Components inside a Module
245
255
246
256
You can also generate components inside other modules. This works the same for the NativeScript Only and Code-Sharing projects.
@@ -259,7 +269,6 @@ These three commands will:
259
269
2. Generate a **cat component** inside the pets module and it will add it to the pets module declarations.
260
270
3. Generate a **dog component** inside the pets module and it will add it to the pets module declarations.
261
271
262
-
263
272
## Additional Template Generators
264
273
265
274
The **NativeScript Schematics** also come with additional templates that help you build your apps faster.
@@ -283,7 +292,7 @@ dogs
283
292
284
293
#### Options
285
294
286
-
| Option | Description
287
-
| --- | ---
288
-
| master | The name of the master component and the name of the module.
0 commit comments