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,后者在文件名中使用client
或server
后缀来确定应在何处运行它们。以下示例显示了如何使用此命令从模块添加插件:
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'))
}