透過 100 多個技巧的集合來學習 Nuxt!

自動匯入

Nuxt Kit 提供了一組工具,可協助您使用自動匯入。這些函式可讓您註冊自己的工具、組合式函式和 Vue API。

自動匯入

Nuxt 會自動匯入輔助函式、組合式函式和 Vue API,以便在您的應用程式中使用,而無需明確匯入它們。根據目錄結構,每個 Nuxt 應用程式也可以對自己的組合式函式和外掛程式使用自動匯入。使用 Nuxt Kit,您也可以新增自己的自動匯入。addImportsaddImportsDir 可讓您將匯入新增至 Nuxt 應用程式。addImportsSources 可讓您將第三方套件中列出的匯入新增至 Nuxt 應用程式。

這些函式旨在註冊您自己的工具、組合式函式和 Vue API。對於頁面、元件和外掛程式,請參閱特定章節:頁面元件外掛程式

Nuxt 會自動匯入輔助函式、組合式函式和 Vue API,以便在您的應用程式中使用,而無需明確匯入它們。根據目錄結構,每個 Nuxt 應用程式也可以對自己的組合式函式和外掛程式使用自動匯入。組合式函式或外掛程式可以使用這些函式。

觀看 Vue School 關於自動匯入 Nuxt Kit 工具的影片。

addImports

將匯入新增至 Nuxt 應用程式。它使您的匯入在 Nuxt 應用程式中可用,而無需手動匯入它們。

類型

function addImports (imports: Import | Import[]): void

interface Import {
  from: string
  priority?: number
  disabled?: boolean
  meta?: {
    description?: string
    docsUrl?: string
    [key: string]: any
  }
  type?: boolean
  typeFrom?: string
  name: string
  as?: string
}

參數

imports

類型Import | Import[]

必要true

具有以下屬性的物件或物件陣列

  • from (必要)
    類型string
    要從中匯入的模組規範。
  • priority (選用)
    類型number
    預設值1
    匯入的優先順序,如果多個匯入具有相同的名稱,將使用優先順序最高的匯入。
  • disabled (選用)
    類型boolean
    如果停用此匯入。
  • meta (選用)
    類型object
    匯入的中繼資料。
  • meta.description (選用)
    類型string
    匯入的簡短描述。
  • meta.docsUrl (選用)
    類型string
    文件網址。
  • meta[key] (選用)
    類型any
    其他中繼資料。
  • type (選用)
    類型boolean
    如果此匯入是純類型匯入。
  • typeFrom (選用)
    類型string
    在產生類型宣告時,使用此作為來源。
  • name (必要)
    類型string
    要偵測的匯入名稱。
  • as (選用)
    類型string
    以此名稱匯入。

範例

// https://github.com/pi0/storyblok-nuxt
import { defineNuxtModule, addImports, createResolver } from '@nuxt/kit'

export default defineNuxtModule({
  setup(options, nuxt) {
    const names = [
      "useStoryblok",
      "useStoryblokApi",
      "useStoryblokBridge",
      "renderRichText",
      "RichTextSchema"
    ];

    names.forEach((name) =>
      addImports({ name, as: name, from: "@storyblok/vue" })
    );
  }
})

addImportsDir

從目錄將匯入新增至 Nuxt 應用程式。它會自動匯入目錄中的所有檔案,並使其在 Nuxt 應用程式中可用,而無需手動匯入它們。

類型

function addImportsDir (dirs: string | string[], options?: { prepend?: boolean }): void

參數

dirs

類型string | string[]

必要true

具有要從中匯入的目錄路徑的字串或字串陣列。

options

類型{ prepend?: boolean }

預設值{}

要傳遞給匯入的選項。如果 prepend 設定為 true,則會將匯入附加到匯入清單的開頭。

範例

// https://github.com/vueuse/motion/tree/main/src/nuxt
import { defineNuxtModule, addImportsDir, createResolver } from '@nuxt/kit'

export default defineNuxtModule({
  meta: {
    name: '@vueuse/motion',
    configKey: 'motion',
  },
  setup(options, nuxt) {
    const resolver = createResolver(import.meta.url)
    addImportsDir(resolver.resolve('./runtime/composables'))
  },
})

addImportsSources

將列出的匯入新增至 Nuxt 應用程式。

類型

function addImportsSources (importSources: ImportSource | ImportSource[]): void

interface Import {
  from: string
  priority?: number
  disabled?: boolean
  meta?: {
    description?: string
    docsUrl?: string
    [key: string]: any
  }
  type?: boolean
  typeFrom?: string
  name: string
  as?: string
}

interface ImportSource extends Import {
  imports: (PresetImport | ImportSource)[]
}

type PresetImport = Omit<Import, 'from'> | string | [name: string, as?: string, from?: string]

參數

importSources

類型ImportSource | ImportSource[]

必要true

具有以下屬性的物件或物件陣列

  • imports (必要)
    類型PresetImport | ImportSource[]
    必要true
    物件或物件陣列,可以是匯入名稱、匯入物件或匯入來源。
  • from (必要)
    類型string
    要從中匯入的模組規範。
  • priority (選用)
    類型number
    預設值1
    匯入的優先順序,如果多個匯入具有相同的名稱,將使用優先順序最高的匯入。
  • disabled (選用)
    類型boolean
    如果停用此匯入。
  • meta (選用)
    類型object
    匯入的中繼資料。
  • meta.description (選用)
    類型string
    匯入的簡短描述。
  • meta.docsUrl (選用)
    類型string
    文件網址。
  • meta[key] (選用)
    類型any
    其他中繼資料。
  • type (選用)
    類型boolean
    如果此匯入是純類型匯入。
  • typeFrom (選用)
    類型string
    在產生類型宣告時,使用此作為來源。
  • name (必要)
    類型string
    要偵測的匯入名稱。
  • as (選用)
    類型string
    以此名稱匯入。

範例

// https://github.com/elk-zone/elk
import { defineNuxtModule, addImportsSources } from '@nuxt/kit'

export default defineNuxtModule({
  setup() {
    // add imports from h3 to make them autoimported
    addImportsSources({
      from: 'h3',
      imports: ['defineEventHandler', 'getQuery', 'getRouterParams', 'readBody', 'sendRedirect'] as Array<keyof typeof import('h3')>,
    })
  }
})