仅客户端NUXT 3 VUE插件

发布于 2025-01-31 20:33:59 字数 852 浏览 0 评论 0原文

我是Nuxt和Vue的新手,所以请轻松对我。我正在尝试使用 vue3-vue3-video-player ,这似乎不基于以下错误支持SSR,当我在视频组件中导入它时会遇到的:

reference eRseRor:Navigator未定义

即使使用<<&lt; clientonly&gt; 。因此,根据我在 nuxt 3文档我想我想创建位于>插件/vue3-video-player.client.js的仅限客户端插件,具有以下内容

import Vue3VideoPlayer from '@cloudgeek/vue3-video-player'

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(Vue3VideoPlayer)
})

: player&gt; ,我会收到以下错误:

[vue warn]:无法解析组件:vue3-video-player

所以我想我的问题是如何创建一个仅客户端VUE使用NUXT 3插件的组件?还是完全不同的方法可以更好?

I am new to Nuxt and Vue, so go easy on me. I am trying to create a video player component in my Nuxt 3 app using vue3-video-player, which doesn't seem to support SSR based on the following error I get when I import it in my video component:

ReferenceError: navigator is not defined

This error persists even if the component is wrapped with <ClientOnly>. So, based on what I saw in the Nuxt 3 Documentation I thought I would create a client-only plugin located at plugins/vue3-video-player.client.js with the following contents:

import Vue3VideoPlayer from '@cloudgeek/vue3-video-player'

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(Vue3VideoPlayer)
})

But when I try to use it in my component as <vue3-video-player>, I get the following error:

[Vue warn]: Failed to resolve component: vue3-video-player

So I guess my question is how do I create a client-only Vue component using Nuxt 3 plugins? Or is there an entirely different approach that would work better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神经大条 2025-02-07 20:33:59

NUXT 3的解决方案:

NUXT将自动读取插件目录中的文件并加载它们。您可以使用.server.client文件名中的后缀以仅在服务器或客户端端加载插件。

例如:

插件/apexcharts.client.ts

一切都很简单!这就是为什么我们喜欢nuxt❤️


nuxt 2的解决方案(显示差异):

nuxt.config.ts

  plugins: [
    {src: '~/plugins/apexcharts', mode: 'client'}
  ],

这仅适用于<代码> NUXT 2 因为NUXT 3 插件/目录均为自动注册单独使用nuxt.config

Solution for nuxt 3:

Nuxt will automatically read the files in your plugins directory and load them. You can use .server or .client suffix in the file name to load a plugin only on the server or client side.

For example:

plugins/apexcharts.client.ts

Everything is so simple! And that is why we love nuxt ❤️


Solution for nuxt 2 (to show the difference):

nuxt.config.ts

  plugins: [
    {src: '~/plugins/apexcharts', mode: 'client'}
  ],

This is only for nuxt 2 because All plugins in your nuxt 3 plugins/ directory are auto-registered, so you should not add them to your nuxt.config separately.

£噩梦荏苒 2025-02-07 20:33:59

标记给定的正确答案在这里

如果您要安装和使用第三方NPM软件包,并且插入“窗口未定义”键入错误,您可以将软件包作为插件加载为如下(例如 wad

npm安装Web-audio-daw

// plugins/wad.client.ts
import Wad from "web-audio-daw"
export default defineNuxtPlugin(nuxtApp => {
  return {
    provide: {
      Wad,
    }
  }
})
// pages/whatever.vue
<script lang="ts" setup>
const { $Wad } = useNuxtApp();
// Can use $Wad normally from here on out
</script>

To tag along with the given correct answer here,

If you're trying to install and use a third party NPM package, and running into "window is not defined" type errors, you can load the package as a plugin as follows (eg WAD)

npm install web-audio-daw

// plugins/wad.client.ts
import Wad from "web-audio-daw"
export default defineNuxtPlugin(nuxtApp => {
  return {
    provide: {
      Wad,
    }
  }
})
// pages/whatever.vue
<script lang="ts" setup>
const { $Wad } = useNuxtApp();
// Can use $Wad normally from here on out
</script>
闻呓 2025-02-07 20:33:59

您可以尝试提供辅助功能。如 docs

import Vue3VideoPlayer from '@cloudgeek/vue3-video-player'

export default defineNuxtPlugin((nuxtApp) => {
  
  return {
    provide: {
      Vue3VideoPlayer
    }
  }

})

you could try to provide a helper function. As mentioned in the docs.

import Vue3VideoPlayer from '@cloudgeek/vue3-video-player'

export default defineNuxtPlugin((nuxtApp) => {
  
  return {
    provide: {
      Vue3VideoPlayer
    }
  }

})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文