Angular learning talks about standalone components (Standalone Component)

青灯夜游
Release: 2022-12-19 19:24:47
forward
2928 people have browsed it

This article will take you to continue learning angular and briefly understand the standalone component (Standalone Component) inAngular. I hope it will be helpful to everyone!

Angular learning talks about standalone components (Standalone Component)

An exciting feature of Angular 14 is that Angular’s Standalone Component is finally here. [Related tutorial recommendations: "angularTutorial"]

In Angular 14, developers can try to use independent components to develop various components, but it is worth noting that the API of Angular independent components is still Without stabilization, there may be some destructive updates in the future, so it is not recommended for use in production environments.

Basic usage

##angular.io/guide/stand…

##standalone is launched in Angular14 new features.

It can make your root module AppModule not so bloated

All components/pipe/directives should be introduced in the corresponding components when they are used

For example, this is the previous way of writing. We declare a

Footer

componentand then import this component in the used

Module

import { Component } from &#39;@angular/core&#39;; @Component({ selector: &#39;app-footer&#39;, template: ` <footer class="dark:bg-gray-800 dark:text-gray-50">Footer</footer> `, }) export class FooterComponent {}
Copy after login
import { NgModule } from &#39;@angular/core&#39;; import { CommonModule } from &#39;@angular/common&#39;; import { FooterComponent } from &#39;./footer.component&#39;; @NgModule({ declarations: [HomeComponent, FooterComponent], exports: [], imports: [CommonModule], }) export class AppModuleModule {}
Copy after login
This kind of The writing method prevents us from getting rid of

NgModule

, but in fact our intention is to replace it with

FooterComponent

inAppComponentThe writing method in

React

will actually be easier to manage and understandUse our new features

standalone

The Footer component will be transformed like this

import { Component } from '@angular/core'; @Component({ selector: 'app-footer', // 将该组件声明成独立组件 standalone: true, template: ` 
Footer
`, }) export class FooterComponent {}
Copy after login

Then for example on the Home page we can use it like this

import { Component } from '@angular/core'; import { FooterComponent } from '@components/footer/footer.component'; @Component({ selector: 'app-home', standalone: true, // 声明需要使用的 component / pipe / directive 但是它们也必须都是独立组件 imports: [FooterComponent], template: ``, }) export class WelcomeComponent {}
Copy after login

Independent components can be directly used for lazy loading. Originally we had to use NgModule to achieve

import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { CustomPreloadingStrategy } from '@views/basic-syntax/router/customPreloadingStrategy'; const routes: Routes = [ { path: 'home', // 之前想要实现懒加载 这里必须是一个NgModule 现在使用独立组件也可以 并且更加简洁 loadComponent: () => import('@views/home/home.component').then((mod) => mod.HomeComponent), }, ]; @NgModule({ imports: [RouterModule.forRoot(routes, { preloadingStrategy: CustomPreloadingStrategy })], exports: [RouterModule], }) export class AppRoutingModule {}
Copy after login

More programming related knowledge, Please visit:

Programming Teaching

! !

The above is the detailed content of Angular learning talks about standalone components (Standalone Component). For more information, please follow other related articles on the PHP Chinese website!

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