我正在使用 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 :
Modules are simply functions that are called sequentially when booting 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'))
}
关于javascript - Nuxt.js 模块中未定义 process.server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58146662/