亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁(yè) web前端 js教程 探索角度形式:訊號(hào)的新替代方案

探索角度形式:訊號(hào)的新替代方案

Nov 08, 2024 am 07:03 AM

Exploring Angular Forms: A New Alternative with Signals

探索角度形式:訊號(hào)的新替代方案

在 Angular 的世界中,無(wú)論您是在製作簡(jiǎn)單的登入頁(yè)面還是更複雜的使用者設(shè)定檔介面,表單對(duì)於使用者互動(dòng)都是至關(guān)重要的。 Angular 傳統(tǒng)上提供兩種主要方法:範(fàn)本驅(qū)動(dòng)表單反應(yīng)式表單。在我之前的 Angular 反應(yīng)式表單系列中,我探索如何利用反應(yīng)式表單的強(qiáng)大功能來(lái)管理複雜邏輯、建立動(dòng)態(tài)表單以及建立自訂表單控制項(xiàng)。

用於管理反應(yīng)性的新工具 - 訊號(hào) - 已在 Angular 版本 16 中引入,此後一直是 Angular 維護(hù)人員關(guān)注的焦點(diǎn),並在版本 17 中變得穩(wěn)定。訊號(hào)允許您處理狀態(tài)變更聲明性地,提供了一個(gè)令人興奮的替代方案,將範(fàn)本驅(qū)動(dòng)表單的簡(jiǎn)單性與反應(yīng)表單的強(qiáng)大反應(yīng)性結(jié)合起來(lái)。本文將研究訊號(hào)如何為 Angular 中的簡(jiǎn)單和複雜形式添加反應(yīng)性。

回顧:角度形式方法

在深入探討使用訊號(hào)增強(qiáng)範(fàn)本驅(qū)動(dòng)表單的主題之前,讓我們先快速回顧一下 Angular 的傳統(tǒng)表單方法:

  1. 模板驅(qū)動(dòng)表單:使用 ngModel 等指令直接在 HTML 範(fàn)本中定義,這些表單易於設(shè)置,非常適合簡(jiǎn)單表單。但是,它們可能無(wú)法提供更複雜場(chǎng)景所需的細(xì)微控制。

    這是範(fàn)本驅(qū)動(dòng)表單的最小範(fàn)例:

    <form (ngSubmit)="onSubmit()">
      <label for="name">Name:</label>
      <input>
    
    </li>
    </ol>
    
    
    
    <pre class="brush:php;toolbar:false">```typescript
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html'
    })
    export class AppComponent {
      name = '';
    
      onSubmit() {
        console.log(this.name);
      }
    }
    ```
    
    1. 反應(yīng)式表單:使用 Angular 的 FormGroup、FormControl 和 FormArray 類(lèi)別在元件類(lèi)別中以程式設(shè)計(jì)方式進(jìn)行管理;反應(yīng)式表單提供對(duì)表單狀態(tài)和驗(yàn)證的精細(xì)控制。正如我之前關(guān)於 Angular Reactive Forms 的文章所討論的那樣,這種方法非常適合複雜的表單。

      這是一個(gè)反應(yīng)式形式的最小範(fàn)例:

      import { Component } from '@angular/core';
      import { FormGroup, FormControl } from '@angular/forms';
      
      @Component({
        selector: 'app-root',
        templateUrl: './app.component.html'
      })
      export class AppComponent {
        form = new FormGroup({
          name: new FormControl('')
        });
      
        onSubmit() {
          console.log(this.form.value);
        }
      }
      
    ```html
    <form [formGroup]="form" (ngSubmit)="onSubmit()">
      <label for="name">Name:</label>
      <input>
    
    
    
    <h2>
      
      
      Introducing Signals as a New Way to Handle Form Reactivity
    </h2>
    
    <p>With the release of Angular 16, signals have emerged as a new way to manage reactivity. Signals provide a declarative approach to state management, making your code more predictable and easier to understand. When applied to forms, signals can enhance the simplicity of template-driven forms while offering the reactivity and control typically associated with reactive forms.</p>
    
    <p>Let’s explore how signals can be used in both simple and complex form scenarios.</p>
    
    <h3>
      
      
      Example 1: A Simple Template-Driven Form with Signals
    </h3>
    
    <p>Consider a basic login form. Typically, this would be implemented using template-driven forms like this:<br>
    </p>
    
    <pre class="brush:php;toolbar:false"><!-- login.component.html -->
    <form name="form" (ngSubmit)="onSubmit()">
      <label for="email">E-mail</label>
      <input type="email">
    
    
    
    
    
    <pre class="brush:php;toolbar:false">// login.component.ts
    import { Component } from "@angular/core";
    
    @Component({
      selector: "app-login",
      templateUrl: "./login.component.html",
    })
    export class LoginComponent {
      public email: string = "";
      public password: string = "";
    
      onSubmit() {
        console.log("Form submitted", { email: this.email, password: this.password });
      }
    }
    

    這種方法適用於簡(jiǎn)單的表單,但是透過(guò)引入訊號(hào),我們可以在添加反應(yīng)功能的同時(shí)保持簡(jiǎn)單性:

    // login.component.ts
    import { Component, computed, signal } from "@angular/core";
    import { FormsModule } from "@angular/forms";
    
    @Component({
      selector: "app-login",
      standalone: true,
      templateUrl: "./login.component.html",
      imports: [FormsModule],
    })
    export class LoginComponent {
      // Define signals for form fields
      public email = signal("");
      public password = signal(""); // Define a computed signal for the form value
    
      public formValue = computed(() => {
        return {
          email: this.email(),
          password: this.password(),
        };
      });
    
      public isFormValid = computed(() => {
        return this.email().length > 0 && this.password().length > 0;
      });
    
      onSubmit() {
        console.log("Form submitted", this.formValue());
      }
    }
    
    <!-- login.component.html -->
    <form name="form" (ngSubmit)="onSubmit()">
      <label for="email">E-mail</label>
      <input type="email">
    
    
    
    <p>In this example, the form fields are defined as signals, allowing for reactive updates whenever the form state changes. The formValue signal provides a computed value that reflects the current state of the form. This approach offers a more declarative way to manage form state and reactivity, combining the simplicity of template-driven forms with the power of signals.</p>
    
    <p>You may be tempted to define the form directly as an object inside a signal. While such an approach may seem more concise, typing into the individual fields does not dispatch reactivity updates, which is usually a deal breaker. Here’s an example StackBlitz with a component suffering from such an issue:</p>
    
    <p>Therefore, if you'd like to react to changes in the form fields, it's better to define each field as a separate signal. By defining each form field as a separate signal, you ensure that changes to individual fields trigger reactivity updates correctly. </p>
    
    <h3>
      
      
      Example 2: A Complex Form with Signals
    </h3>
    
    <p>You may see little benefit in using signals for simple forms like the login form above, but they truly shine when handling more complex forms. Let's explore a more intricate scenario - a user profile form that includes fields like firstName, lastName, email, phoneNumbers, and address. The phoneNumbers field is dynamic, allowing users to add or remove phone numbers as needed.</p>
    
    <p>Here's how this form might be defined using signals:<br>
    </p>
    
    <pre class="brush:php;toolbar:false">// user-profile.component.ts
    import { JsonPipe } from "@angular/common";
    import { Component, computed, signal } from "@angular/core";
    import { FormsModule, Validators } from "@angular/forms";
    
    @Component({
      standalone: true,
      selector: "app-user-profile",
      templateUrl: "./user-profile.component.html",
      styleUrls: ["./user-profile.component.scss"],
      imports: [FormsModule, JsonPipe],
    })
    export class UserProfileComponent {
      public firstName = signal("");
      public lastName = signal("");
      public email = signal(""); 
      // We need to use a signal for the phone numbers, so we get reactivity when typing in the input fields
      public phoneNumbers = signal([signal("")]);
      public street = signal("");
      public city = signal("");
      public state = signal("");
      public zip = signal("");
    
      public formValue = computed(() => {
        return {
          firstName: this.firstName(),
          lastName: this.lastName(),
          email: this.email(), // We need to do a little mapping here, so we get the actual value for the phone numbers
          phoneNumbers: this.phoneNumbers().map((phoneNumber) => phoneNumber()),
          address: {
            street: this.street(),
            city: this.city(),
            state: this.state(),
            zip: this.zip(),
          },
        };
      });
    
      public formValid = computed(() => {
        const { firstName, lastName, email, phoneNumbers, address } = this.formValue(); // Regex taken from the Angular email validator
    
        const EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
        const isEmailFormatValid = EMAIL_REGEXP.test(email);
    
        return (
          firstName.length > 0 &&
          lastName.length > 0 &&
          email.length > 0 &&
          isEmailFormatValid &&
          phoneNumbers.length > 0 && // Check if all phone numbers are valid
          phoneNumbers.every((phoneNumber) => phoneNumber.length > 0) &&
          address.street.length > 0 &&
          address.city.length > 0 &&
          address.state.length > 0 &&
          address.zip.length > 0
        );
      });
    
      addPhoneNumber() {
        this.phoneNumbers.update((phoneNumbers) => {
          phoneNumbers.push(signal(""));
          return [...phoneNumbers];
        });
      }
    
      removePhoneNumber(index: number) {
        this.phoneNumbers.update((phoneNumbers) => {
          phoneNumbers.splice(index, 1);
          return [...phoneNumbers];
        });
      }
    }
    

    請(qǐng)注意,phoneNumbers 欄位被定義為訊號(hào)數(shù)組中的一個(gè)訊號(hào)。這種結(jié)構(gòu)使我們能夠追蹤各個(gè)電話號(hào)碼的變更並反應(yīng)性地更新表單狀態(tài)。 addPhoneNumber 和removePhoneNumber 方法更新phoneNumbers 訊號(hào)數(shù)組,觸發(fā)表單中的反應(yīng)性更新。

    <!-- user-profile.component.html -->
    
    
    
    
    <blockquote>
    <p>在範(fàn)本中,我們使用phoneNumbers訊號(hào)陣列來(lái)動(dòng)態(tài)渲染電話號(hào)碼輸入欄位。 addPhoneNumber 和removePhoneNumber 方法允許使用者反應(yīng)性地新增或刪除電話號(hào)碼,從而更新表單狀態(tài)。請(qǐng)注意 track 函數(shù)的用法,這是確保 ngFor 指令正確追蹤phoneNumbers 陣列變更所必需的。 </p>
    </blockquote>
    
    <p>這是複雜表單範(fàn)例的 StackBlitz 演示,供您試用:</p>
    
    <h3>
      
      
      使用訊號(hào)驗(yàn)證表單
    </h3>
    
    <p>驗(yàn)證對(duì)於任何表單都至關(guān)重要,確保使用者輸入在提交之前符合所需的標(biāo)準(zhǔn)。使用訊號(hào),可以以反應(yīng)性和聲明性的方式處理驗(yàn)證。在上面的複雜表單範(fàn)例中,我們實(shí)作了一個(gè)名為 formValid 的計(jì)算訊號(hào),它檢查所有欄位是否符合特定的驗(yàn)證標(biāo)準(zhǔn)。 </p>
    
    <p>可以輕鬆自訂驗(yàn)證邏輯以適應(yīng)不同的規(guī)則,例如檢查有效的電子郵件格式或確保填寫(xiě)所有必填欄位。使用訊號(hào)進(jìn)行驗(yàn)證可以讓您建立更多可維護(hù)和可測(cè)試的程式碼,因?yàn)轵?yàn)證規(guī)則被明確定義並自動(dòng)對(duì)表單欄位中的變更做出反應(yīng)。它甚至可以被抽象化為單獨(dú)的實(shí)用程序,以使其可以在不同形式中重複使用。 </p>
    
    <p>在複雜表單範(fàn)例中,formValid 訊號(hào)可確保填寫(xiě)所有必填欄位並驗(yàn)證電子郵件和電話號(hào)碼格式。 </p>
    
    <p>這種驗(yàn)證方法有點(diǎn)簡(jiǎn)單,需要更好地連接到實(shí)際的表單欄位。雖然它適用於許多用例,但在某些情況下,您可能需要等到 Angular 中添加明確「訊號(hào)形式」支援。 Tim Deschryver 開(kāi)始實(shí)現(xiàn)一些圍繞著訊號(hào)形式的抽象,包括驗(yàn)證,並寫(xiě)了一篇關(guān)於它的文章。讓我們看看將來(lái) Angular 中是否會(huì)添加這樣的東西。 </p>
    
    <h3>
      
      
      為什麼要使用角度形式的訊號(hào)?
    </h3>
    
    <p>Angular 中訊號(hào)的採(cǎi)用提供了一種強(qiáng)大的新方法來(lái)管理表單狀態(tài)和反應(yīng)性。訊號(hào)提供了一種靈活的聲明性方法,可以透過(guò)結(jié)合模板驅(qū)動(dòng)表單和反應(yīng)式表單的優(yōu)勢(shì)來(lái)簡(jiǎn)化複雜的表單處理。以下是使用 Angular 形式的訊號(hào)的一些主要好處:</p>
    
    <ol>
    <li><p><strong>聲明式狀態(tài)管理</strong>:訊號(hào)可讓您以聲明方式定義表單欄位和計(jì)算值,讓您的程式碼更可預(yù)測(cè)且更易於理解。 </p></li>
    <li><p><strong>反應(yīng)性</strong>:訊號(hào)為表單欄位提供反應(yīng)性更新,確保表單狀態(tài)的變更自動(dòng)觸發(fā)反應(yīng)性更新。 </p></li>
    <li><p><strong>粒度控制</strong>:訊號(hào)可讓您在粒度層級(jí)定義表單字段,從而實(shí)現(xiàn)對(duì)表單狀態(tài)和驗(yàn)證的細(xì)粒度控制。 </p></li>
    <li><p><strong>動(dòng)態(tài)表單</strong>:訊號(hào)可用於建立具有可動(dòng)態(tài)新增或刪除欄位的動(dòng)態(tài)表單,提供靈活的方式來(lái)處理複雜的表單場(chǎng)景。 </p></li>
    <li><p><strong>簡(jiǎn)單性</strong>:與傳統(tǒng)的反應(yīng)式表單相比,訊號(hào)可以提供更簡(jiǎn)單、更簡(jiǎn)潔的方式來(lái)管理表單狀態(tài),使建置和維護(hù)複雜表單變得更加容易。 </p></li>
    </ol>
    
    <h3>
      
      
      結(jié)論
    </h3>
    
    <p>在我之前的文章中,我們探索了 Angular 反應(yīng)式表單的強(qiáng)大功能,從動(dòng)態(tài)表單建置到自訂表單控制項(xiàng)。隨著訊號(hào)的引入,Angular 開(kāi)發(fā)人員擁有了一種新工具,它將模板驅(qū)動(dòng)表單的簡(jiǎn)單性與反應(yīng)式表單的反應(yīng)性融為一體。 </p>
    
    <p>雖然許多用例都需要反應(yīng)式表單,但訊號(hào)為需要更直接、聲明性方法的 Angular 應(yīng)用程式中的表單狀態(tài)管理提供了一種全新、強(qiáng)大的替代方案。隨著 Angular 的不斷發(fā)展,嘗試這些新功能將幫助您建立更易於維護(hù)、性能更高的應(yīng)用程式。 </p>
    
    <p>編碼愉快! </p>
    
    
              
    
                
            

以上是探索角度形式:訊號(hào)的新替代方案的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

熱門(mén)話題

Laravel 教程
1597
29
PHP教程
1488
72
如何在node.js中提出HTTP請(qǐng)求? 如何在node.js中提出HTTP請(qǐng)求? Jul 13, 2025 am 02:18 AM

在Node.js中發(fā)起HTTP請(qǐng)求有三種常用方式:使用內(nèi)置模塊、axios和node-fetch。 1.使用內(nèi)置的http/https模塊無(wú)需依賴,適合基礎(chǔ)場(chǎng)景,但需手動(dòng)處理數(shù)據(jù)拼接和錯(cuò)誤監(jiān)聽(tīng),例如用https.get()獲取數(shù)據(jù)或通過(guò).write()發(fā)送POST請(qǐng)求;2.axios是基於Promise的第三方庫(kù),語(yǔ)法簡(jiǎn)潔且功能強(qiáng)大,支持async/await、自動(dòng)JSON轉(zhuǎn)換、攔截器等,推薦用於簡(jiǎn)化異步請(qǐng)求操作;3.node-fetch提供類(lèi)似瀏覽器fetch的風(fēng)格,基於Promise且語(yǔ)法簡(jiǎn)單

JavaScript數(shù)據(jù)類(lèi)型:原始與參考 JavaScript數(shù)據(jù)類(lèi)型:原始與參考 Jul 13, 2025 am 02:43 AM

JavaScript的數(shù)據(jù)類(lèi)型分為原始類(lèi)型和引用類(lèi)型。原始類(lèi)型包括string、number、boolean、null、undefined和symbol,其值不可變且賦值時(shí)復(fù)制副本,因此互不影響;引用類(lèi)型如對(duì)象、數(shù)組和函數(shù)存儲(chǔ)的是內(nèi)存地址,指向同一對(duì)象的變量會(huì)相互影響。判斷類(lèi)型可用typeof和instanceof,但需注意typeofnull的歷史問(wèn)題。理解這兩類(lèi)差異有助於編寫(xiě)更穩(wěn)定可靠的代碼。

React與Angular vs Vue:哪個(gè)JS框架最好? React與Angular vs Vue:哪個(gè)JS框架最好? Jul 05, 2025 am 02:24 AM

選哪個(gè)JavaScript框架最好?答案是根據(jù)需求選擇最適合的。 1.React靈活自由,適合需要高度定制、團(tuán)隊(duì)有架構(gòu)能力的中大型項(xiàng)目;2.Angular提供完整解決方案,適合企業(yè)級(jí)應(yīng)用和長(zhǎng)期維護(hù)的大項(xiàng)目;3.Vue上手簡(jiǎn)單,適合中小型項(xiàng)目或快速開(kāi)發(fā)。此外,是否已有技術(shù)棧、團(tuán)隊(duì)規(guī)模、項(xiàng)目生命週期及是否需要SSR也都是選擇框架的重要因素。總之,沒(méi)有絕對(duì)最好的框架,適合自己需求的就是最佳選擇。

JavaScript時(shí)間對(duì)象,某人構(gòu)建了一個(gè)eactexe,在Google Chrome上更快的網(wǎng)站等等 JavaScript時(shí)間對(duì)象,某人構(gòu)建了一個(gè)eactexe,在Google Chrome上更快的網(wǎng)站等等 Jul 08, 2025 pm 02:27 PM

JavaScript開(kāi)發(fā)者們,大家好!歡迎閱讀本週的JavaScript新聞!本週我們將重點(diǎn)關(guān)注:Oracle與Deno的商標(biāo)糾紛、新的JavaScript時(shí)間對(duì)象獲得瀏覽器支持、GoogleChrome的更新以及一些強(qiáng)大的開(kāi)發(fā)者工具。讓我們開(kāi)始吧! Oracle與Deno的商標(biāo)之爭(zhēng)Oracle試圖註冊(cè)“JavaScript”商標(biāo)的舉動(dòng)引發(fā)爭(zhēng)議。 Node.js和Deno的創(chuàng)建者RyanDahl已提交請(qǐng)願(yuàn)書(shū),要求取消該商標(biāo),他認(rèn)為JavaScript是一個(gè)開(kāi)放標(biāo)準(zhǔn),不應(yīng)由Oracle

什麼是緩存API?如何與服務(wù)人員使用? 什麼是緩存API?如何與服務(wù)人員使用? Jul 08, 2025 am 02:43 AM

CacheAPI是瀏覽器提供的一種緩存網(wǎng)絡(luò)請(qǐng)求的工具,常與ServiceWorker配合使用,以提升網(wǎng)站性能和離線體驗(yàn)。 1.它允許開(kāi)發(fā)者手動(dòng)存儲(chǔ)如腳本、樣式表、圖片等資源;2.可根據(jù)請(qǐng)求匹配緩存響應(yīng);3.支持刪除特定緩存或清空整個(gè)緩存;4.通過(guò)ServiceWorker監(jiān)聽(tīng)fetch事件實(shí)現(xiàn)緩存優(yōu)先或網(wǎng)絡(luò)優(yōu)先等策略;5.常用於離線支持、加快重複訪問(wèn)速度、預(yù)加載關(guān)鍵資源及後臺(tái)更新內(nèi)容;6.使用時(shí)需注意緩存版本控制、存儲(chǔ)限制及與HTTP緩存機(jī)制的區(qū)別。

處理諾言:鏈接,錯(cuò)誤處理和承諾在JavaScript中 處理諾言:鏈接,錯(cuò)誤處理和承諾在JavaScript中 Jul 08, 2025 am 02:40 AM

Promise是JavaScript中處理異步操作的核心機(jī)制,理解鍊式調(diào)用、錯(cuò)誤處理和組合器是掌握其應(yīng)用的關(guān)鍵。 1.鍊式調(diào)用通過(guò).then()返回新Promise實(shí)現(xiàn)異步流程串聯(lián),每個(gè).then()接收上一步結(jié)果並可返回值或Promise;2.錯(cuò)誤處理應(yīng)統(tǒng)一使用.catch()捕獲異常,避免靜默失敗,並可在catch中返回默認(rèn)值繼續(xù)流程;3.組合器如Promise.all()(全成功才成功)、Promise.race()(首個(gè)完成即返回)和Promise.allSettled()(等待所有完成)

利用Array.Prototype方法用於JavaScript中的數(shù)據(jù)操作 利用Array.Prototype方法用於JavaScript中的數(shù)據(jù)操作 Jul 06, 2025 am 02:36 AM

JavaScript數(shù)組內(nèi)置方法如.map()、.filter()和.reduce()可簡(jiǎn)化數(shù)據(jù)處理;1).map()用於一對(duì)一轉(zhuǎn)換元素生成新數(shù)組;2).filter()按條件篩選元素;3).reduce()用於聚合數(shù)據(jù)為單一值;使用時(shí)應(yīng)避免誤用導(dǎo)致副作用或性能問(wèn)題。

JS綜述:深入研究JavaScript事件循環(huán) JS綜述:深入研究JavaScript事件循環(huán) Jul 08, 2025 am 02:24 AM

JavaScript的事件循環(huán)通過(guò)協(xié)調(diào)調(diào)用棧、WebAPI和任務(wù)隊(duì)列來(lái)管理異步操作。 1.調(diào)用棧執(zhí)行同步代碼,遇到異步任務(wù)時(shí)交由WebAPI處理;2.WebAPI在後臺(tái)完成任務(wù)後將回調(diào)放入相應(yīng)的隊(duì)列(宏任務(wù)或微任務(wù));3.事件循環(huán)檢查調(diào)用棧是否為空,若為空則從隊(duì)列中取出回調(diào)推入調(diào)用棧執(zhí)行;4.微任務(wù)(如Promise.then)優(yōu)先於宏任務(wù)(如setTimeout)執(zhí)行;5.理解事件循環(huán)有助於避免阻塞主線程並優(yōu)化代碼執(zhí)行順序。

See all articles