日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本篇文章帶大家繼續angular的學習,簡單了解一下Angular中的獨立組件(Standalone Component),希望對大家有所幫助!


Angular學習之聊聊獨立組件(Standalone Component)


Angular 14一項令人興奮的特性就是Angular的獨立組件(Standalone Component)終于來了。

在Angular 14中, 開發者可以嘗試使用獨立組件開發各種組件,但是值得注意的是Angular獨立組件的API仍然沒有穩定下,將來可能存在一些破壞性更新,所以不推薦在生產環境中使用。


基本使用

angular.io/guide/stand…

standalone 是 Angular14 推出的新特性。

它可以讓你的 根模塊 AppModule 不至于那么臃腫

所有的 component / pipe / directive 都在被使用的時候 在對應的組件引入就好了

舉個例子 這是之前的寫法 我們聲明一個 Footer 組件

然后在使用的 Module 中導入這個組件

import { Component } from '@angular/core';
 
@Component({
  selector: 'app-footer',
  template: ` <footer class="dark:bg-gray-800 dark:text-gray-50">Footer</footer> `,
})
export class FooterComponent {}
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FooterComponent } from './footer.component';
 
@NgModule({
  declarations: [HomeComponent, FooterComponent],
  exports: [],
  imports: [CommonModule],
})
export class AppModuleModule {}

這種寫法導致我們始終無法擺脫 NgModule

但其實我們的意圖就是在 AppComponent 中使用 FooterComponent

換成 React 中的寫法 其實會更便于管理和理解

用上我們的新特性 standalone

Footer 組件就改造成這樣

import { Component } from '@angular/core';
 
@Component({
  selector: 'app-footer',
  // 將該組件聲明成獨立組件
  standalone: true,
  template: ` <footer class="dark:bg-gray-800 dark:text-gray-50">Footer</footer> `,
})
export class FooterComponent {}

然后比如在 Home 頁面 我們就可以這樣使用

import { Component } from '@angular/core';
 
import { FooterComponent } from '@components/footer/footer.component';
 
@Component({
  selector: 'app-home',
  standalone: true,
  // 聲明需要使用的 component / pipe / directive 但是它們也必須都是獨立組件
  imports: [FooterComponent],
  template: `<app-footer></app-footer>`,
})
export class WelcomeComponent {}

獨立組件可以直接用于懶加載 本來我們必須借助 NgModule 來實現

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 {}


分享到:
標簽:Angular獨立組件 StandaloneComponent
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定