日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用PromiseAPI加載JS、CSS或圖像文件

使用JavaScript或TypeScript加載資源(JavaScript,CSS,圖像)的最簡單方法。

Web應(yīng)用程序通常使用第三方API在應(yīng)用程序的特定路由上提供功能,其中許多API都很繁重,并且NPM上沒有軟件包。在Web應(yīng)用程序上添加這些API的通常方法是將其添加到主HTML文件中,使用這種方法會大大影響頁面加載時間。例如,如果是JavaScript文件,它將下載,編譯并執(zhí)行腳本。

如果我們可以避免在頁面首次加載時加載這些API會怎樣呢?這將有助于更快地加載頁面內(nèi)容,減少總體網(wǎng)絡(luò)數(shù)據(jù)的使用,并降低低端設(shè)備的內(nèi)存使用。

Loadx API

Loadx API將通過以下功能幫助我們異步加載資源:

  • 加載JS,CSS或圖像
  • 緩存結(jié)果
  • ??出色地實現(xiàn)了Promise和async/await
  • ?壓縮后的ES3僅400字節(jié)

讓我們使用此API創(chuàng)建一個示例:

 
 
 
 
  1. import loadx from 'loadx'; 
  2. async function getUser() {  // 加載Axios API 
  3.   await loadx.js('https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js'); 
  4.   // 從Rest API獲取用戶 
  5.   return axios.get('/user/12345'); 
  6. }async function loadImage(url) {  // 圖像容器 
  7.   const containerEl = document.querySelector('.container'); 
  8.   // loading spinner 
  9.   const spinnerEl = document.querySelector('.spinner'); 
  10.     if (containerEl === null || spinnerEl === null) { 
  11.     throw new Error('Image container not found') 
  12.   }    // show the spinner 
  13.   spinnerEl.style.diplay = 'block'; 
  14.   // 加載圖像 
  15.   await loadx.img(url, containerEl); 
  16.   // hide the spinner 
  17.   spinnerEl.style.diplay = 'none'; 
  18. }function loadCSSFramework() {  // 簡單加載Tailwind CSS 
  19.   loadx.css('https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css'); 

結(jié)束

現(xiàn)在,只有在需要時才可以加載資源。試一試吧!


網(wǎng)頁標(biāo)題:使用PromiseAPI加載JS、CSS或圖像文件
文章位置:http://m.5511xx.com/article/copgipg.html