插件
插件是独立的代码,通常为 Vue 添加应用级功能。在 Nuxt 中,插件会自动从 plugins 目录导入。但是,如果你需要随模块一起发布插件,Nuxt Kit 提供了 addPlugin 和 addPluginTemplate 方法。这些实用程序允许你自定义插件配置,以更好地满足你的需求。
¥Plugins are self-contained code that usually add app-level functionality to Vue. In Nuxt, plugins are automatically imported from the plugins directory. However, if you need to ship a plugin with your module, Nuxt Kit provides the addPlugin and addPluginTemplate methods. These utils allow you to customize the plugin configuration to better suit your needs.
addPlugin
注册一个 Nuxt 插件并添加到插件数组中。
¥Registers a Nuxt plugin and to the plugins array.
类型
¥Type
function addPlugin (plugin: NuxtPlugin | string, options: AddPluginOptions): NuxtPlugin
interface NuxtPlugin {
src: string
mode?: 'all' | 'server' | 'client'
order?: number
}
interface AddPluginOptions { append?: boolean }
参数
¥Parameters
plugin
类型:NuxtPlugin | string
¥Type: NuxtPlugin | string
必需:true
¥Required: true
插件对象或包含插件路径的字符串。如果提供了字符串,它将被转换为插件对象,并将 src 设置为该字符串值。如果提供了插件对象,它必须具有以下属性:
¥A plugin object or a string with the path to the plugin. If a string is provided, it will be converted to a plugin object with src set to the string value. If a plugin object is provided, it must have the following properties:
src(必需)
类型:string
插件的路径。mode(可选)
类型:'all' | 'server' | 'client'
默认:'all'
如果设置为'all',该插件将同时包含在客户端和服务器 bundles 中。如果设置为'server',该插件将仅包含在服务器 bundles 中。如果设置为'client',该插件将仅包含在客户端 bundles 中。在指定src选项时,你还可以使用.client和.server修饰符,以便仅在客户端或服务器端使用插件。order(可选)
类型:number
默认:0
插件的顺序。这允许对插件顺序进行更精细的控制,仅供高级用户使用。数字较小的优先运行,用户插件默认使用0。建议将order设置为介于-20(对于pre插件,在 Nuxt 插件之前运行)和20(对于post插件,在 Nuxt 插件之后运行)之间的数字。
order。目前,你需要在每个选择 Nuxt 4 行为的层中定义兼容版本。要将插件附加到插件数组的末尾,请改用 append 选项。¥Don't use order unless you know what you're doing. For most plugins, the default order of 0 is sufficient. To append a plugin to the end of the plugins array, use the append option instead.options
类型:AddPluginOptions
¥Type: AddPluginOptions
默认:{}
¥Default: {}
传递给插件的选项。如果将 append 设置为 true,则插件将被附加到插件数组中,而不是添加到前面。
¥Options to pass to the plugin. If append is set to true, the plugin will be appended to the plugins array instead of prepended.
示例
¥Examples
import { createResolver, defineNuxtModule, addPlugin } from '@nuxt/kit'
export default defineNuxtModule({
setup() {
const resolver = createResolver(import.meta.url)
addPlugin({
src: resolver.resolve('runtime/plugin.js'),
mode: 'client'
})
}
})
// https://github.com/nuxt/nuxters
export default defineNuxtPlugin((nuxtApp) => {
const colorMode = useColorMode()
nuxtApp.hook('app:mounted', () => {
if (colorMode.preference !== 'dark') {
colorMode.preference = 'dark'
}
})
})
addPluginTemplate
添加模板并注册为 Nuxt 插件。这对于需要在构建时生成代码的插件很有用。
¥Adds a template and registers as a nuxt plugin. This is useful for plugins that need to generate code at build time.
类型
¥Type
function addPluginTemplate (pluginOptions: NuxtPluginTemplate, options: AddPluginOptions): NuxtPlugin
interface NuxtPluginTemplate<Options = Record<string, any>> {
src?: string,
filename?: string,
dst?: string,
mode?: 'all' | 'server' | 'client',
options?: Options,
getContents?: (data: Options) => string | Promise<string>,
write?: boolean,
order?: number
}
interface AddPluginOptions { append?: boolean }
interface NuxtPlugin {
src: string
mode?: 'all' | 'server' | 'client'
order?: number
}
参数
¥Parameters
pluginOptions
类型:NuxtPluginTemplate
¥Type: NuxtPluginTemplate
必需:true
¥Required: true
具有以下属性的插件模板对象:
¥A plugin template object with the following properties:
src(可选)
类型:string
模板的路径。如果未提供src,则必须提供getContents。filename(可选)
类型:string
模板文件名。如果未提供filename,则它将从src路径生成。在这种情况下,src选项是必需的。dst(可选)
类型:string
目标文件的路径。如果未提供dst,则它将从filename路径和 nuxtbuildDir选项生成。mode(可选)
类型:'all' | 'server' | 'client'
默认:'all'
如果设置为'all',该插件将同时包含在客户端和服务器 bundles 中。如果设置为'server',该插件将仅包含在服务器 bundles 中。如果设置为'client',该插件将仅包含在客户端 bundles 中。在指定src选项时,你还可以使用.client和.server修饰符,以便仅在客户端或服务器端使用插件。options(可选)
类型:Options
传递给模板的选项。getContents(可选)
类型:(data: Options) => string | Promise<string>
一个函数,用于使用options对象调用。它应该返回一个字符串或一个解析为字符串的 Promise。如果提供了src,则此函数将被忽略。write(可选)
类型:boolean
如果设置为true,模板将被写入目标文件。否则,该模板将仅在虚拟文件系统中使用。order(可选)
类型:number
默认:0
插件的顺序。这允许对插件顺序进行更精细的控制,仅供高级用户使用。数字较小的优先运行,用户插件默认使用0。建议将order设置为介于-20(对于pre插件,在 Nuxt 插件之前运行)和20(对于post插件,在 Nuxt 插件之后运行)之间的数字。
order。目前,你需要在每个选择 Nuxt 4 行为的层中定义兼容版本。要将插件附加到插件数组的末尾,请改用 append 选项。¥Don't use order unless you know what you're doing. For most plugins, the default order of 0 is sufficient. To append a plugin to the end of the plugins array, use the append option instead.options
类型:AddPluginOptions
¥Type: AddPluginOptions
默认:{}
¥Default: {}
传递给插件的选项。如果将 append 设置为 true,则插件将被附加到插件数组中,而不是添加到前面。
¥Options to pass to the plugin. If append is set to true, the plugin will be appended to the plugins array instead of prepended.
示例
¥Examples
// https://github.com/vuejs/vuefire
import { createResolver, defineNuxtModule, addPluginTemplate } from '@nuxt/kit'
export default defineNuxtModule({
setup() {
const resolver = createResolver(import.meta.url)
addPluginTemplate({
src: resolve(templatesDir, 'plugin.ejs'),
filename: 'plugin.mjs',
options: {
...options,
ssr: nuxt.options.ssr,
},
})
}
})
import { VueFire, useSSRInitialState } from 'vuefire'
import { defineNuxtPlugin } from '#imports'
export default defineNuxtPlugin((nuxtApp) => {
const firebaseApp = nuxtApp.$firebaseApp
nuxtApp.vueApp.use(VueFire, { firebaseApp })
<% if(options.ssr) { %>
if (import.meta.server) {
nuxtApp.payload.vuefire = useSSRInitialState(undefined, firebaseApp)
} else if (nuxtApp.payload?.vuefire) {
useSSRInitialState(nuxtApp.payload.vuefire, firebaseApp)
}
<% } %>
})