最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

process.server在Nuxt.js模块中未定义

运维笔记admin11浏览0评论

process.server在Nuxt.js模块中未定义

process.server在Nuxt.js模块中未定义

我正在使用Nuxt.js模块,如果process.server为true,则会添加一个插件,但是它不起作用。我尝试使用typescript模块记录process.server的日志

export default function (moduleOptions?: any) {
  console.log(process.server);
};

显示:

yarn run v1.17.3
$ nuxt-ts
undefined                                                             22:00:16

   ╭──────────────────────────────────────────╮
   │                                          │
   │   Nuxt.js v2.9.2                         │
   │   Running in development mode (spa)      │
   │                                          │
   │   Listening on: http://localhost:3000/   │
   │                                          │
   ╰──────────────────────────────────────────╯

我该如何解决?

回答如下:

从docs:

模块只是在启动Nuxt时被顺序调用的简单函数。

换句话说,总是从服务器调用模块,并在设置Nuxt实例时调用它们。因此,process.server未定义,因为Nuxt尚未定义它。

您可以改为依靠name conventional plugins,后者在文件名中使用clientserver后缀来确定应在何处运行它们。以下示例显示了如何使用此命令从模块添加插件:

import path from 'path'

export default function (moduleOptions) {
  // Register your plugin here with .server in the name
  // to only run on the server.  This expects the plugin file to 
  // be located next to this module file
  this.addPlugin(path.resolve(__dirname, 'plugin.server.ts'))
}
发布评论

评论列表(0)

  1. 暂无评论