plugins

Nuxt 拥有一个插件系统,用于在创建 Vue 应用程序时使用 Vue 插件及其他功能。

Nuxt 会自动读取 app/plugins/ 目录中的文件,并在创建 Vue 应用程序时加载它们。

所有插件都会自动注册,您无需将它们单独添加到 nuxt.config 中。
您可以在文件名称中使用 .server.client 后缀,以便仅在服务器端或客户端加载插件。

已注册的插件

只有目录顶层的文件(或任何子目录中的索引文件)才会被自动注册为插件。

目录结构
-| plugins/
---| foo.ts      // scanned
---| bar/
-----| baz.ts    // not scanned
-----| foz.vue   // not scanned
-----| index.ts  // currently scanned but deprecated

只有 foo.tsbar/index.ts 会被注册。

要在子目录中添加插件,您可以使用 nuxt.config.ts 中的 app/plugins 选项

nuxt.config.ts
export default defineNuxtConfig({
  plugins: [
    '~/plugins/bar/baz',
    '~/plugins/bar/foz',
  ],
})

创建插件

传递给插件的唯一参数是 nuxtApp

plugins/hello.ts
export default defineNuxtPlugin((nuxtApp) => {
  // Doing something with nuxtApp
})

对象语法插件

也可以使用对象语法定义插件,以实现更高级的用例。例如

plugins/hello.ts
export default defineNuxtPlugin({
  name: 'my-plugin',
  enforce: 'pre', // or 'post'
  async setup (nuxtApp) {
    // this is the equivalent of a normal functional plugin
  },
  hooks: {
    // You can directly register Nuxt app runtime hooks here
    'app:created' () {
      const nuxtApp = useNuxtApp()
      // do something in the hook
    },
  },
  env: {
    // Set this value to `false` if you don't want the plugin to run when rendering server-only or island components.
    islands: true,
  },
})
如果您使用对象语法,属性将进行静态分析以生成更优化的构建。因此,您不应在运行时定义它们。
例如,设置 enforce: import.meta.server ? 'pre' : 'post' 会破坏 Nuxt 将来为您的插件所做的任何优化。Nuxt 在使用对象语法时会静态预加载任何钩子监听器,让您无需担心插件注册的顺序即可定义钩子。

注册顺序

您可以通过在文件名上添加“字母顺序”编号来控制插件的注册顺序。

目录结构
plugins/
 | - 01.myPlugin.ts
 | - 02.myOtherPlugin.ts

在此示例中,02.myOtherPlugin.ts 将能够访问 01.myPlugin.ts 注入的任何内容。

这在您有一个插件依赖于另一个插件的情况下很有用。

如果您不熟悉“字母顺序”编号,请记住文件名是按字符串而不是数字值排序的。例如,10.myPlugin.ts 会排在 2.myOtherPlugin.ts 之前。这就是为什么示例中个位数数字前面加上 0 的原因。

加载策略

并行插件

默认情况下,Nuxt 按顺序加载插件。您可以将插件定义为 parallel,这样 Nuxt 就不会等到插件执行结束才加载下一个插件。

plugins/my-plugin.ts
export default defineNuxtPlugin({
  name: 'my-plugin',
  parallel: true,
  async setup (nuxtApp) {
    // the next plugin will be executed immediately
  },
})

带依赖的插件

如果一个插件需要在另一个插件运行之前等待,您可以将该插件的名称添加到 dependsOn 数组中。

plugins/depending-on-my-plugin.ts
export default defineNuxtPlugin({
  name: 'depends-on-my-plugin',
  dependsOn: ['my-plugin'],
  async setup (nuxtApp) {
    // this plugin will wait for the end of `my-plugin`'s execution before it runs
  },
})

使用组合式函数

您可以在 Nuxt 插件中使用 组合式函数 以及 工具函数

app/plugins/hello.ts
export default defineNuxtPlugin((nuxtApp) => {
  const foo = useFoo()
})

但是,请记住有一些限制和差异

如果组合式函数依赖于稍后注册的另一个插件,它可能无法工作。
插件按顺序且在所有其他内容之前调用。您可能正在使用一个依赖于尚未调用的另一个插件的组合式函数。
如果组合式函数依赖于 Vue.js 生命周期,它将无法工作。
通常,Vue.js 组合式函数绑定到当前组件实例,而插件仅绑定到 nuxtApp 实例。

提供辅助函数

如果您想在 NuxtApp 实例上提供辅助函数,请在 provide 键下从插件返回它。

export default defineNuxtPlugin(() => {
  return {
    provide: {
      hello: (msg: string) => `Hello ${msg}!`,
    },
  }
})

然后您可以在组件中使用该辅助函数

app/components/Hello.vue
<script setup lang="ts">
// alternatively, you can also use it here
const { $hello } = useNuxtApp()
</script>

<template>
  <div>
    {{ $hello('world') }}
  </div>
</template>
请注意,我们强烈建议使用 组合式函数 而不是提供辅助函数,以避免污染全局命名空间并保持主捆绑入口文件小巧。
如果您的插件提供了 refcomputed,它不会在组件 <template> 中解包。
这是由于 Vue 处理非模板顶层引用(refs)的方式所致。您可以在Vue 文档中.

了解更多信息。

类型化插件

如果您从插件中返回辅助函数,它们将自动进行类型化;您会发现它们在 useNuxtApp() 的返回值中以及您的模板中都有类型定义。如果您需要在另一个插件中使用提供的辅助函数,您可以调用 useNuxtApp() 来获取类型化版本。但通常情况下,除非您确定插件的顺序,否则应避免这样做。

对于高级用例,您可以像这样声明注入属性的类型

index.d.ts
declare module '#app' {
  interface NuxtApp {
    $hello (msg: string): string
  }
}

declare module 'vue' {
  interface ComponentCustomProperties {
    $hello (msg: string): string
  }
}

export {}
如果您正在使用 WebStorm,您可能需要增强 @vue/runtime-core 直到此问题得到解决。

Vue 插件

如果您想使用 Vue 插件,例如vue-gtag来添加 Google Analytics 标签,您可以使用 Nuxt 插件来实现。

首先,安装 Vue 插件依赖项

npm install --save-dev vue-gtag-next

然后创建一个插件文件

app/plugins/vue-gtag.client.ts
import VueGtag, { trackRouter } from 'vue-gtag-next'

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(VueGtag, {
    property: {
      id: 'GA_MEASUREMENT_ID',
    },
  })
  trackRouter(useRouter())
})

Vue 指令

同样,您可以在插件中注册自定义 Vue 指令。

plugins/my-directive.ts
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.directive('focus', {
    mounted (el) {
      el.focus()
    },
    getSSRProps (binding, vnode) {
      // you can provide SSR-specific props here
      return {}
    },
  })
})
如果您注册 Vue 指令,则必须在客户端和服务器端都注册它,除非您只在渲染其中一方时使用它。如果指令仅在客户端有意义,您始终可以将其移动到 ~/plugins/my-directive.client.ts,并在 ~/plugins/my-directive.server.ts 中为服务器提供一个“存根”指令。
Vue 文档中的自定义指令 中阅读更多内容。