亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

教你巧用?import.meta?實(shí)現(xiàn)熱更新的問(wèn)題

 更新時(shí)間:2022年04月16日 09:33:03   作者:奮飛  
import.meta?是一個(gè)給?JavaScript?模塊暴露特定上下文的元數(shù)據(jù)屬性的對(duì)象,它包含了這個(gè)模塊的信息,這篇文章主要介紹了巧用?import.meta?實(shí)現(xiàn)熱更新的問(wèn)題,需要的朋友可以參考下

import.meta

import.meta 是一個(gè)給 JavaScript 模塊暴露特定上下文的元數(shù)據(jù)屬性的對(duì)象,它包含了這個(gè)模塊的信息。

import.meta 對(duì)象是由 ECMAScript 實(shí)現(xiàn)的,它帶有一個(gè) null 的原型對(duì)象。這個(gè)對(duì)象可以擴(kuò)展,并且它的屬性都是可寫(xiě),可配置和可枚舉的。

<script type="module">
	console.log(import.meta)  // {url: 'http://127.0.0.1:5500/dist/index.html?a=1'}
</script>

它返回一個(gè)帶有 url 屬性的對(duì)象,指明模塊的基本 URL。也可以是外部腳本的 URL,還可以是內(nèi)聯(lián)腳本所屬文檔的 URL。注意,url 也可能包含參數(shù)或者哈希(比如后綴?#

在這里插入圖片描述

應(yīng)用場(chǎng)景

import.meta.hot

Pinia 是 vuex 新替代方案。Pinia 中熱更新實(shí)現(xiàn),借助 import.meta。

import { defineStore, acceptHMRUpdate } from 'pinia'

const useAuth = defineStore('auth', {
  // options...
})

// make sure to pass the right store definition, `useAuth` in this case.
if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useAuth, import.meta.hot))
}

Vite 通過(guò)特殊的 import.meta.hot 對(duì)象暴露手動(dòng) HMR API。

https://github.com/vitejs/vite/blob/main/packages/vite/src/client/client.ts#L412

interface ImportMeta {
  readonly hot?: {
    readonly data: any

    accept(): void
    accept(cb: (mod: any) => void): void
    accept(dep: string, cb: (mod: any) => void): void
    accept(deps: string[], cb: (mods: any[]) => void): void

    prune(cb: () => void): void
    dispose(cb: (data: any) => void): void
    decline(): void
    invalidate(): void

    on(event: string, cb: (...args: any[]) => void): void
  }
}

vite HMR API:https://cn.vitejs.dev/guide/api-hmr.html

import.meta.webpackHot

module.hot 的一個(gè)別名,strict ESM 中可以使用 import.meta.webpackHot 但是不能使用 module.hot

在 package.json 中設(shè)置 "type": "module" 會(huì)強(qiáng)制 package.json 下的所有文件使用 ECMAScript 模塊

vuex 借助 webpack 模塊熱替換:https://webpack.docschina.org/guides/hot-module-replacement/
vuex 提供 hotUpdate 方法:https://github.com/vuejs/vuex/blob/HEAD/src/store.js#L242-L243

if (import.meta.webpackHot) {
  // 使 action 和 mutation 成為可熱重載模塊
  import.meta.webpackHot.accept(['./mutations', './modules'], () => {
    // 獲取更新后的模塊
    // 因?yàn)?babel 6 的模塊編譯格式問(wèn)題,這里需要加上 `.default`
    const newMutations = require('./mutations').default
    const newModules = require('./modules').default
    // 加載新模塊
    store.hotUpdate({
      mutations: newMutations,
      modules: {
        ...newModules
      }
    })
  })
}

URL()

URL() 構(gòu)造函數(shù)返回一個(gè)新創(chuàng)建的 URL 對(duì)象,表示由一組參數(shù)定義的 URL。

const url = new URL(url [, base])
  • url 是一個(gè)表示絕對(duì)或相對(duì) URL 的 DOMString。如果url 是相對(duì) URL,則會(huì)將 base 用作基準(zhǔn) URL。如果 url 是絕對(duì)URL,則無(wú)論參數(shù)base是否存在,都將被忽略。
  • base 可選。是一個(gè)表示基準(zhǔn) URL 的 DOMString,在 url 是相對(duì) URL 時(shí),它才會(huì)起效。如果未指定,則默認(rèn)為 ''。

二者結(jié)合使用

new URL('./relative-path', import.meta.url)

示例:獲取圖片

function loadImg (relativePath) {
  const url = new URL(relativePath, import.meta.url)
  const image = new Image()
  image.src = url
  return image
}
loadImg('../../images/1.jpg')

無(wú)需關(guān)心路徑問(wèn)題,自動(dòng)根據(jù)當(dāng)前 URL 進(jìn)行轉(zhuǎn)換。

到此這篇關(guān)于巧用 import.meta 實(shí)現(xiàn)熱更新的文章就介紹到這了,更多相關(guān)import.meta熱更新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論