简体   繁体   English

nuxt.js 文件未定义,pugin 有问题

[英]nuxt.js document is not defined, problem with pugin

I added plugin: vue-burger-menu to my nuxt.js project.我在我的nuxt.js项目中添加了插件: vue-burger-menu And i have an error: "document is not defined" .我有一个错误: "document is not defined" I know, that this plugin is available only for client-side.我知道,这个插件仅适用于客户端。 So I found in vue documentation enter link description here what I have to do to fix it.所以我在 vue 文档中发现在这里输入链接描述我必须做些什么来修复它。 It works only for first refresh.它仅适用于第一次刷新。 Then I have again document is not defined.然后我又一次没有定义文件。

nuxt.config.js : nuxt.config.js :

  build: {
vendor: ['vue-burger-menu'],
 }

 plugins: [
{ src: '~/plugins/vue-burger-menu.js', ssr: false }
],

Add a file to my plugins folder called "vue-burger-menu.js" :在我的插件文件夹中添加一个名为"vue-burger-menu.js"

 import Vue from 'vue';
 import VueBurgerMenu from 'vue-burger-menu';

   if (process.browser) {

      Vue.use(VueBurgerMenu);

   }

nav template

 <template lang="pug">
   Slide(right)
        nav.menu_vertical
 </template>

 <script>
 import { Slide } from 'vue-burger-menu'
 export default {
 name: 'Nav',
 components: {
   Slide
 },
}

在此处输入图片说明

Replace deprecated process.browser with process.clientprocess.client替换已弃用的process.browser

if (process.client) {
   Vue.use(VueBurgerMenu);
}

Nuxt is doing SSR and document is only available in browser so you need to wrap your plugin code like Nuxt 正在执行 SSR 并且document仅在browser可用,因此您需要像这样包装插件代码

change "vue-burger-menu.js" as following更改"vue-burger-menu.js"如下

import Vue from 'vue';
import VueBurgerMenu from 'vue-burger-menu';

if (process.browser) {

 Vue.use(VueBurgerMenu);

}

you can find detail documentation Here您可以在此处找到详细文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM