angular-cn/aio/content/guide/component-overview.md

184 lines
7.1 KiB
Markdown
Raw Normal View History

# Angular Components Overview
Components are the main building block for Angular applications. Each component consists of:
* An HTML template that declares what renders on the page
* A Typescript class that defines behavior
* A CSS selector that defines how the component is used in a template
* Optionally, CSS styles applied to the template
This topic describes how to create and configure an Angular component.
<div class="alert is-helpful">
To view or download the example code used in this topic, see the <live-example></live-example>.
</div>
## Prerequisites
To create a component, verify that you have met the following prerequisites:
1. [Install the Angular CLI.](guide/setup-local#install-the-angular-cli)
1. [Create an Angular workspace](guide/setup-local#create-a-workspace-and-initial-application) with initial application.
If you don't have a project, you can create one using `ng new <project-name>`, where `<project-name>` is the name of your Angular application.
## Creating a component
The easiest way to create a component is with the Angular CLI. You can also create a component manually.
### Creating a component using the Angular CLI
To create a component using the Angular CLI:
1. From a terminal window, navigate to the directory containing your application.
1. Run the `ng generate component <component-name>` command, where `<component-name>` is the name of your new component.
By default, this command creates the following:
* A folder named after the component
* A component file, `<component-name>.component.ts`
* A template file, `<component-name>.component.html`
* A CSS file, `<component-name>.component.css`
* A testing specification file, `<component-name>.component.spec.ts`
Where `<component-name>` is the name of your component.
<div class="alert is-helpful">
You can change how `ng generate component` creates new components.
For more information, see [ng generate component](cli/generate#component-command) in the Angular CLI documentation.
</div>
### Creating a component manually
Although the Angular CLI is the easiest way to create an Angular component, you can also create a component manually.
This section describes how to create the core component file within an existing Angular project.
To create a new component manually:
1. Navigate to your Angular project directory.
1. Create a new file, `<component-name>.component.ts`.
1. At the top of the file, add the following import statement.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.ts"
region="import">
</code-example>
1. After the `import` statement, add a `@Component` decorator.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.ts"
region="decorator-skeleton">
</code-example>
1. Choose a CSS selector for the component.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.ts"
region="selector">
</code-example>
For more information on choosing a selector, see [Specifying a component's selector](#specifying-a-components-css-selector).
1. Define the HTML template that the component uses to display information.
In most cases, this template is a separate HTML file.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.ts"
region="templateUrl">
</code-example>
For more information on defining a component's template, see [Defining a component's template](#defining-a-components-template).
1. Select the styles for the component's template.
In most cases, you define the styles for your component's template in a separate file.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.ts"
region="decorator">
</code-example>
1. Add a `class` statement that includes the code for the component.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.ts"
region="class">
</code-example>
## Specifying a component's CSS selector
Every component requires a CSS _selector_. A selector instructs Angular to instantiate this component wherever it finds the corresponding tag in template HTML. For example, consider a component `hello-world.component.ts` that defines its selector as `app-hello-world`. This selector instructs Angular to instantiate this component any time the tag `<app-hello-world>` appears in a template.
Specify a component's selector by adding a `selector` statement to the `@Component` decorator.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.ts"
region="selector">
</code-example>
## Defining a component's template
A template is a block of HTML that tells Angular how to render the component in your application.
You can define a template for your component in one of two ways: by referencing an external file, or directly within the component.
To define a template as an external file, add a `templateUrl` property to the `@Component` decorator.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.ts"
region="templateUrl">
</code-example>
To define a template within the component, add a `template` property to the `@Component` decorator that contains the HTML you want to use.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.1.ts"
region="template">
</code-example>
If you want your template to span multiple lines, you can use backticks (<code> ` </code>).
For example:
<code-example
path="component-overview/src/app/component-overview/component-overview.component.2.ts"
region="templatebacktick">
</code-example>
<div class="alert is-helpful">
An Angular component requires a template defined using `template` or `templateUrl`. You cannot have both statements in a component.
</div>
## Declaring a component's styles
You can declare component styles uses for its template in one of two ways: by referencing an external file, or directly within the component.
2021-02-03 17:42:55 +08:00
To declare the styles for a component in a separate file, add a `styleUrls` property to the `@Component` decorator.
<code-example
path="component-overview/src/app/component-overview/component-overview.component.ts"
region="decorator">
</code-example>
To declare the styles within the component, add a `styles` property to the `@Component` decorator that contains the styles you want to use.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/cli/index.md # aio/content/guide/accessibility.md # aio/content/guide/ajs-quick-reference.md # aio/content/guide/angular-compiler-options.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/architecture-components.md # aio/content/guide/architecture-modules.md # aio/content/guide/architecture-next-steps.md # aio/content/guide/architecture-services.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/cli-builder.md # aio/content/guide/comparing-observables.md # aio/content/guide/component-styles.md # aio/content/guide/creating-libraries.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection-navtree.md # aio/content/guide/dependency-injection-providers.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/deprecations.md # aio/content/guide/displaying-data.md # aio/content/guide/dynamic-component-loader.md # aio/content/guide/elements.md # aio/content/guide/file-structure.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ivy-compatibility.md # aio/content/guide/language-service.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/module-types.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule-vs-jsmodule.md # aio/content/guide/ngmodules.md # aio/content/guide/observables-in-angular.md # aio/content/guide/pipes.md # aio/content/guide/providers.md # aio/content/guide/releases.md # aio/content/guide/route-animations.md # aio/content/guide/router-tutorial.md # aio/content/guide/router.md # aio/content/guide/rx-library.md # aio/content/guide/schematics-authoring.md # aio/content/guide/schematics-for-libraries.md # aio/content/guide/service-worker-config.md # aio/content/guide/service-worker-getting-started.md # aio/content/guide/structural-directives.md # aio/content/guide/styleguide.md # aio/content/guide/template-syntax.md # aio/content/guide/template-typecheck.md # aio/content/guide/testing.md # aio/content/guide/typescript-configuration.md # aio/content/guide/upgrade-setup.md # aio/content/guide/user-input.md # aio/content/guide/using-libraries.md # aio/content/guide/web-worker.md # aio/content/guide/workspace-config.md # aio/content/marketing/docs.md # aio/content/marketing/events.html # aio/content/navigation.json # aio/content/start/index.md # aio/content/start/start-data.md # aio/content/start/start-deployment.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt4.md # aio/package.json # aio/src/app/app.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/shared/custom-icon-registry.ts # aio/src/environments/environment.stable.ts # aio/tools/stackblitz-builder/builder.js # aio/tools/transforms/angular-content-package/index.js # aio/tools/transforms/templates/api/lib/memberHelpers.html # aio/yarn.lock # integration/ng_elements/e2e/app.e2e-spec.ts # integration/ng_elements/src/app.ts # packages/common/src/pipes/date_pipe.ts # packages/core/src/metadata/directives.ts # packages/core/src/metadata/ng_module.ts # packages/core/src/render/api.ts # packages/forms/src/directives/ng_model.ts # packages/forms/src/form_builder.ts # packages/forms/src/model.ts # packages/platform-browser/src/browser.ts # packages/router/src/config.ts # packages/router/src/directives/router_link.ts # packages/router/src/directives/router_link_active.ts # packages/router/src/events.ts # packages/router/src/router.ts # packages/router/src/router_module.ts # packages/router/src/router_state.ts
2020-11-28 12:50:51 +08:00
<code-example
path="component-overview/src/app/component-overview/component-overview.component.3.ts"
region="styles">
</code-example>
The `styles` property takes an array of strings that contain the CSS rule declarations.
## Next steps
* For an architectural overview of components, see [Introduction to components and templates](guide/architecture-components).
* For additional options you can use when creating a component, see [Component](api/core/Component) in the API Reference.
* For more information on styling components, see [Component styles](guide/component-styles).
* For more information on templates, see [Template syntax](guide/template-syntax).
@reviewed 2021-03-18