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

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

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

import.meta

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

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

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

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

在這里插入圖片描述

應用場景

import.meta.hot

Pinia 是 vuex 新替代方案。Pinia 中熱更新實現(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 通過特殊的 import.meta.hot 對象暴露手動 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 的一個別名,strict ESM 中可以使用 import.meta.webpackHot 但是不能使用 module.hot。

在 package.json 中設置 "type": "module" 會強制 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'], () => {
    // 獲取更新后的模塊
    // 因為 babel 6 的模塊編譯格式問題,這里需要加上 `.default`
    const newMutations = require('./mutations').default
    const newModules = require('./modules').default
    // 加載新模塊
    store.hotUpdate({
      mutations: newMutations,
      modules: {
        ...newModules
      }
    })
  })
}

URL()

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

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

二者結合使用

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')

無需關心路徑問題,自動根據(jù)當前 URL 進行轉換。

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

相關文章

最新評論