mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
Updated the existing tutorials and added another about Using components. (#3191)
This commit is contained in:
committed by
Eugenio Romano
parent
eff3ea83b0
commit
68df6138c4
@@ -1,68 +1,38 @@
|
||||
---
|
||||
Level: Beginner
|
||||
---
|
||||
# Adding a new component
|
||||
|
||||
# Adding a new view
|
||||
By definition a *component* controls a patch of screen called a view. As an example, individual components define and control menus, tabs, forms, buttons and every simple or complex portion of layout of an application. In this tutorial you will learn how to create a component using [Angular CLI](https://cli.angular.io/). After the creation you will learn how to add it to an existing application.
|
||||
|
||||
Every application developed in Angular is a single page application where the concepts of *view* and *routing* play a key role in the user experience. Being a single page application, the navigation between the different layouts (called *views*) is enabled through the *routing*. In this tutorial you will learn how to create a new view into your application and how to have access to it using a defined endpoint.
|
||||
## Creating a component
|
||||
Starting from the root of your project, run the following command into a terminal.
|
||||
|
||||
## Creating a view
|
||||
ng generate component my-first-component
|
||||
|
||||
Into an Angular application, a view is implemented by a regular component. A view can use other views (so other components), but a view can be used to implement the full layout of your application. This is the reason why creating a view is the task than creating a component.
|
||||
If you are adding the component to an application with more than one module, you might want to specify it using the `--module` parameter. For example use `--module app` to add the new component to the root app of your application.
|
||||
|
||||
To create a view, run the following command into a terminal, starting from the root of your project.
|
||||
## Using the component
|
||||
Once done, wherever you will use `<app-my-first-component></app-my-first-component>` into the HTML file of another component, you will see the content of the new component rendered exactly in that place.
|
||||
|
||||
ng generate component my-first-view
|
||||
As an example, add `<app-my-first-component></app-my-first-component>` on top of the `app.component.html` file stored into the `src` folder, and run the application again. Directly in the browser you will see `my-first-component works!`, that shows exactly the place where the component is rendered in the layout.
|
||||
|
||||
See the [Adding a new component](new-component.md) tutorial for further details.
|
||||
## Anatomy of the component
|
||||
By default the new component is created into the `src/app` path and everything is stored in a folder named like the component itself. In this example a folder named with `my-first-component` is added to `src/app`, with inside the following content:
|
||||
|
||||
## Routing the view
|
||||
- `my-first-component.component.scss` containing the CSS used by the component. This file is created as empty.
|
||||
- `my-first-component.component.html` containing the HTML used to render the component. This file is created with a very basic message rendering the name of the component included in a `p` tag.
|
||||
- `my-first-component.component.spec.ts` containing the unit tests for the component.
|
||||
- `my-first-component.component.ts` containing the `MyFirstComponentComponent` class implementing the business logic in typescript.
|
||||
|
||||
An Angular application has one singleton instance of the `Router` service that is used to match the browser's URL with the corresponding component to display. The `Router` service must be configured in a typescript file (usually in the `imports` , in with a syntax similar to the
|
||||
following source code.
|
||||
To make the component usable, one or more modules should declare it (or import it). In this example the `app.module.ts` file stored into the `src/app` folder contains the following code.
|
||||
|
||||
import { MyFirstComponentComponent } from './my-first-component/my-first-component.component';
|
||||
|
||||
```ts
|
||||
const appRoutes: Routes = [
|
||||
{ path: 'path-in-the-app', component: ExistingComponent },
|
||||
{ path: '**', component: PageNotFoundComponent }
|
||||
];
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
RouterModule.forRoot(
|
||||
appRoutes,
|
||||
{ enableTracing: true } // <-- debugging purposes only.
|
||||
)
|
||||
// other imports here
|
||||
],
|
||||
...
|
||||
})
|
||||
```
|
||||
declarations: [
|
||||
...
|
||||
MyFirstComponentComponent
|
||||
],
|
||||
|
||||
To add the new view to the routing, change the `appRoutes` constant as follows:
|
||||
|
||||
```ts
|
||||
const appRoutes: Routes = [
|
||||
{ path: 'path-in-the-app', component: ExistingComponent },
|
||||
{ path: 'my-first-view', component: MyFirstViewComponent }, // <-- Add this!
|
||||
{ path: '**', component: PageNotFoundComponent }
|
||||
];
|
||||
```
|
||||
|
||||
And remember to import the component in the same file with the following syntax:
|
||||
|
||||
```ts
|
||||
import { MyFirstViewComponent } from './my-first-view/my-first-view.component';
|
||||
```
|
||||
|
||||
Be aware that the `Router` service can be declared in a file that can be stored in different places in the application's structure. Usually the place where the `Router` service is declared is closed to the file containing the root module.
|
||||
|
||||
## Testing the view
|
||||
|
||||
To render the new view through the application and check the user experience, restart the application and open a browser to the following URL.
|
||||
|
||||
http://<ip_address>:<port>/my-first-view
|
||||
|
||||
The result should be a very simple page with the following content.
|
||||
|
||||
my-first-view works!
|
||||
These are the very basic information you should be know about your brand new component. All you have read here is standard Angular, not customised or valid for ADF applications only.
|
||||
|
Reference in New Issue
Block a user