Angular學習之聊聊獨立組件(Standalone Component)-天天亮點

          來源:php中文網 | 2022-12-19 19:49:21 |

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

          Angular 14一項令人興奮的特性就是Angular的獨立組件(Standalone Component)終于來了。【相關教程推薦:《angular教程》】

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


          (資料圖片僅供參考)

          基本使用

          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學習之聊聊獨立組件(Standalone Component)的詳細內容,更多請關注php中文網其它相關文章!

          關鍵詞: 獨立組件 Angular.js

          亚洲AⅤ男人的天堂在线观看| 亚洲人成色在线观看| 亚洲国产成人久久精品大牛影视 | 亚洲大香伊人蕉在人依线| 久久亚洲私人国产精品vA | 亚洲av无码av在线播放| 亚洲精品无码久久久久YW| 在线观看亚洲AV日韩AV| 亚洲熟妇成人精品一区| 亚洲乱亚洲乱妇24p| 亚洲成aⅴ人片久青草影院按摩| 亚洲七久久之综合七久久| 亚洲色欲色欲www在线播放| 亚洲乱妇熟女爽到高潮的片| 亚洲av无码专区在线观看亚| 综合一区自拍亚洲综合图区| 亚洲 国产 图片| 国产福利电影一区二区三区,亚洲国模精品一区| 亚洲AV无码专区日韩| 亚洲综合色成在线播放| 中文亚洲AV片不卡在线观看| 国产V亚洲V天堂无码久久久| 亚洲福利视频一区| 亚洲精品第五页中文字幕| 亚洲成年人电影在线观看| 亚洲日本久久久午夜精品| 婷婷亚洲综合五月天小说| 久久久久亚洲AV无码观看| 亚洲一区二区三区在线 | 亚洲女子高潮不断爆白浆| 国产精品亚洲专区无码WEB| 亚洲精品成人区在线观看| 亚洲综合AV在线在线播放| 亚洲国产精品一区第二页| 亚洲视频免费观看| 亚洲综合av一区二区三区不卡| 国产精品亚洲av色欲三区| 国产乱辈通伦影片在线播放亚洲 | 亚洲欧美日韩国产精品一区| 国产亚洲漂亮白嫩美女在线| 亚洲无码日韩精品第一页|