欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡(jiǎn)單談?wù)凙ngular中的獨(dú)立組件的使用

 更新時(shí)間:2022年08月05日 08:49:14   作者:大明二代  
這篇文章主要介紹了簡(jiǎn)單談?wù)凙ngular中的獨(dú)立組件的使用的相關(guān)資料,通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),需要的朋友可以參考下

前言

Angular 14一項(xiàng)令人興奮的特性就是Angular的獨(dú)立組件終于來了。

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

如何創(chuàng)建一個(gè)獨(dú)立組件

對(duì)于已有的組件,我們可以在@Component()中添加standalone: true的標(biāo)識(shí),然后我們可以在沒有@NgModule()的情況下直接使用imports導(dǎo)入其他模塊了。
如果是新建組件,可以使用ng generate component <name> --standalone的命令,直接創(chuàng)建一個(gè)獨(dú)立組件, 例如:

ng generate component button-list --standalone
@Component({
  selector: 'app-button-list',
  standalone: true,
  imports: [
    CommonModule,
  ],
  templateUrl: './button-list.component.html',
  styleUrls: ['./button-list.component.scss']
})
export class ButtonListComponent implements OnInit

在獨(dú)立組件中導(dǎo)入已有的模塊

我們可以在imports中添加已有的模塊,以MatButtonModule為例:

imports: [
    CommonModule,
    MatButtonModule,
],

這樣子我們就可以在ButtonListComponent中使用MatButtonModulemat-button組件了:

<button mat-button>Basic</button>
<button mat-button color="primary">Primary</button>
<button mat-button color="accent">Accent</button>
<button mat-button color="warn">Warn</button>
<button mat-button disabled>Disabled</button>
<a mat-button  rel="external nofollow"  target="_blank">Link</a>

效果圖:

使用獨(dú)立組件啟動(dòng)Angular應(yīng)用

第一步, 將AppComponent設(shè)置為獨(dú)立組件:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  standalone: true,
})
export class AppComponent {
    ...
}

第二步,將AppModule的imports中的導(dǎo)入的模塊加入到AppComponent的imports中,但是有兩個(gè)模塊例外: BrowserModuleBrowserAnimationsModule。

如果導(dǎo)入的話,可能會(huì)導(dǎo)致** BrowserModule have already been loaded. If you need access to common directives such as NgIf and NgFor, import the CommonModule instead.**的問題:

第三步,刪除app.module.ts文件

最后一步, 將main.ts中的:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

改為:

bootstrapApplication(AppComponent).catch(err => console.error(err));

這樣子我們就實(shí)現(xiàn)了使用獨(dú)立組件啟動(dòng)Angular組件了。

為獨(dú)立組件配置路由

我這里分別有三個(gè)獨(dú)立組件: HomeComponent, ButtonListComponentChipListComponent,

然后在main.ts中創(chuàng)建ROUTES對(duì)象

const ROUTES: Route[] = [
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'home'
  },
  {
    path: 'home',
    component: HomeComponent
  },
  {
    path: 'button',
    loadComponent: () =>
      import('./app/button-list/button-list.component').then(
        (mod) => mod.ButtonListComponent
      ),
  },
  {
    path: 'chip',
    loadComponent: () =>
      import('./app/chip-list/chip-list.component').then(
        (mod) => mod.ChipListComponent
      ),
  },
];

其中ButtonListComponentChipListComponent使用loadComponent去實(shí)現(xiàn)路由懶加載。

最后在bootstrapApplication的第二個(gè)參數(shù)中使用providers注冊(cè)RouterModule好了。

bootstrapApplication(AppComponent, {
  providers: [
    importProvidersFrom(RouterModule.forRoot([...ROUTES])),
  ],
}).catch(err => console.error(err));

效果圖:

配置依賴注入

當(dāng)我們想要啟動(dòng)Angular應(yīng)用的時(shí)候,可能需要注入一些值或者服務(wù)。 在bootstrapApplication, 我們可以通過providers來注冊(cè)值或者服務(wù)。

比如,我有一個(gè)獲取圖片的url,需要注入到PhotoService中:

bootstrapApplication(AppComponent, {
  providers: [
    {
      provide: 'photoUrl',
      useValue: 'https://picsum.photos',
    },
    {provide: PhotosService, useClass: PhotosService },
    importProvidersFrom(RouterModule.forRoot([...ROUTES])),
    importProvidersFrom(HttpClientModule)
  ],
})

PhotoService代碼如下:

@Injectable()
export class PhotosService {

  constructor(
    @Inject('photoUrl') private photoUrl: string,
    private http: HttpClient
  ) { }

  public getPhotoUrl(i: number): string {
    return `${this.photoUrl}/200/300?random=${i}`;
  }
}

源代碼

本文所使用的源代碼

線上demo

相關(guān)文章

  • Angular2利用組件與指令實(shí)現(xiàn)圖片輪播組件

    Angular2利用組件與指令實(shí)現(xiàn)圖片輪播組件

    這篇文章主要給大家介紹了Angular2中組件與指令的一個(gè)小實(shí)踐,利用組件和指令實(shí)現(xiàn)一個(gè)圖片輪播組件的相關(guān)資料,文中給出了詳細(xì)的介紹和示例代碼,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。
    2017-03-03
  • 深入理解AngularJS中的ng-bind-html指令

    深入理解AngularJS中的ng-bind-html指令

    ng-bind-html和ng-bind的區(qū)別就是,ng-bind把值作為字符串,和元素的內(nèi)容進(jìn)行綁定,但是ng-bind-html把值作為html,和元素的html進(jìn)行綁定.相當(dāng)于jq里面的.text()和.html()。這篇文章主要給大家深入的介紹了AngularJS中ng-bind-html指令 的相關(guān)資料,需要的朋友可以參考下。
    2017-03-03
  • 如何以Angular的姿勢(shì)打開Font-Awesome詳解

    如何以Angular的姿勢(shì)打開Font-Awesome詳解

    這篇文章主要給大家介紹了關(guān)于如何以Angular的姿勢(shì)打開Font-Awesome的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Angular具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • Angular6 發(fā)送手機(jī)驗(yàn)證碼按鈕倒計(jì)時(shí)效果實(shí)現(xiàn)方法

    Angular6 發(fā)送手機(jī)驗(yàn)證碼按鈕倒計(jì)時(shí)效果實(shí)現(xiàn)方法

    這篇文章主要介紹了Angular6 發(fā)送手機(jī)驗(yàn)證碼按鈕倒計(jì)時(shí)效果實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • AngularJS基礎(chǔ) ng-non-bindable 指令詳細(xì)介紹

    AngularJS基礎(chǔ) ng-non-bindable 指令詳細(xì)介紹

    本文主要講解AngularJS ng-non-bindable 指令,這里幫大家整理了ng-non-bindable指令的基本知識(shí)資料,有需要的小伙伴可以參考下
    2016-08-08
  • 詳解AngularJS之$window窗口對(duì)象

    詳解AngularJS之$window窗口對(duì)象

    本篇文章主要介紹了AngularJS之$window窗口對(duì)象,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Angular2進(jìn)階之如何避免Dom誤區(qū)

    Angular2進(jìn)階之如何避免Dom誤區(qū)

    這篇文章主要介紹了Angular2進(jìn)階之如何避免Dom誤區(qū),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • 使用AngularJS來實(shí)現(xiàn)HTML頁面嵌套的方法

    使用AngularJS來實(shí)現(xiàn)HTML頁面嵌套的方法

    這篇文章主要介紹了使用AngularJS來實(shí)現(xiàn)HTML頁面嵌套的方法,主要用到了AngularJS中的ng-include指令,需要的朋友可以參考下
    2015-06-06
  • AngularJS實(shí)現(xiàn)圖片上傳和預(yù)覽功能的方法分析

    AngularJS實(shí)現(xiàn)圖片上傳和預(yù)覽功能的方法分析

    這篇文章主要介紹了AngularJS實(shí)現(xiàn)圖片上傳和預(yù)覽功能的方法,結(jié)合HTML5實(shí)例形式對(duì)比分析了AngularJS圖片上傳的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-11-11
  • Angular中$broadcast和$emit的使用方法詳解

    Angular中$broadcast和$emit的使用方法詳解

    本篇文章主要介紹了Angular中$broadcast和$emit的使用方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05

最新評(píng)論