Angular獨立組件怎么使用

這篇文章主要介紹“Angular獨立組件怎么使用”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Angular獨立組件怎么使用”文章能幫助大家解決問題。

為平塘等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務,及平塘網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、平塘網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

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

基本使用

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 來實現(xiàn)

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { CustomPreloadingStrategy } from '@views/basic-syntax/router/customPreloadingStrategy';

const routes: Routes = [
  {
    path: 'home',
    // 之前想要實現(xiàn)懶加載 這里必須是一個NgModule 現(xiàn)在使用獨立組件也可以 并且更加簡潔
    loadComponent: () => import('@views/home/home.component').then((mod) => mod.HomeComponent),
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { preloadingStrategy: CustomPreloadingStrategy })],
  exports: [RouterModule],
})
export class AppRoutingModule {}

關(guān)于“Angular獨立組件怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

標題名稱:Angular獨立組件怎么使用
路徑分享:http://muchs.cn/article38/pphjsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、手機網(wǎng)站建設(shè)、電子商務、網(wǎng)站改版微信公眾號、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)