[英]process.server is undefined in Nuxt.js modules
我正在使用 Nuxt.js 模塊,如果 process.server 為真,該模塊會添加一個插件,但它不起作用。 我嘗試使用 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/ │
│ │
╰──────────────────────────────────────────╯
我該如何解決?
從文檔:
模塊只是在啟動 Nuxt 時按順序調用的函數。
換句話說,模塊總是從服務器調用,並在設置 Nuxt 實例時被調用。 由於這個原因, process.server
是未定義的,因為 Nuxt 還沒有定義它。
您可以改為依賴名稱常規插件,它使用文件名中的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'))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.