Home > Web Front-end > JS Tutorial > How to build components in Angular? 3 methods introduced

How to build components in Angular? 3 methods introduced

青灯夜游
Release: 2021-08-12 10:32:56
forward
6860 people have browsed it

How to build components in Angular? This article will introduce you to Angular methods to create projects and three ways to create components in Angular.

How to build components in Angular? 3 methods introduced

#1. What is the difference between angular and angularjs?

  • Naming changes, Angular2 and later will be officially named Angular, and versions before 2.0 are called AngualrJS. [Related tutorial recommendation: "angular tutorial"]

  • 1.x is used by introducing the js file of AngularJS to the web page. After 2.0, it is completely different. The difference between the two is similar to Java and JavaScript.

How to build components in Angular? 3 methods introduced

2. Create a project

1. Install the global Angular CLI command line tool

npm install -g @angular/cli
Copy after login

2. Create a project

ng new angular-tour-of-heroes
Copy after login

Note: The node version needs to be above 12, otherwise it will prompt: "'ng' is not an internal or external command, nor is it an operable program or batch file. ”

3. Run the project

cd angular-tour-of-heroes
ng serve --open
Copy after login

How to build components in Angular? 3 methods introduced

3. The first way to create components:

# Create a new file under the ##1.src file and name it hello-world.component.ts

import { Component } from "@angular/core";

@Component({
    selector: 'hello-world组件',
    // templateUrl: './app.component.html',
    // styleUrls: ["./app.component.scss"]
    template: `<h1>{{text}}</h1>`

})
export class HellowordComponent {
    constructor() {}
    text = "第一个模板";
}
Copy after login

How to build components in Angular? 3 methods introduced

2.app.component.html or Add component tag

##

// 引入ng核心包和组件
import { Component } from &#39;@angular/core&#39;;
@Component({
  selector: &#39;app-root&#39;,//当前组件的引用名称
  template:
    ` 
 <hello-world></hello-world>//x新增组件标签

    `  ,
  // templateUrl: &#39;./app.component.html&#39;,//组件模板
  styles: [&#39;h1 { color:red}&#39;]
  // styleUrls: [&#39;./app.component.scss&#39;]//组件的样式文件

})
export class AppComponent {//组件名称

}
Copy after login
How to build components in Angular? 3 methods introduced

in app.component.ts 3. Introduce the component into app.module.ts and declare the component

How to build components in Angular? 3 methods introduced

4. The second way to create components:

Use cli to create components

Input command:

ng generate component hello-world
Copy after login

How to build components in Angular? 3 methods introduced##5. The third way to create components:

1. Download in vscode Angular Files

##2. Right click under components, and the following picture will appearHow to build components in Angular? 3 methods introduced

3. Click Generater component and enter the component name and press EnterHow to build components in Angular? 3 methods introduced

##4. Component generation

How to build components in Angular? 3 methods introduced

For more programming-related knowledge, please visit:

Introduction to Programming

! ! How to build components in Angular? 3 methods introduced

The above is the detailed content of How to build components in Angular? 3 methods introduced. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template