透過超過 100 個訣竅學習 Nuxt!

setResponseStatus

setResponseStatus 設定回應的 statusCode(以及可選的 statusMessage)。

Nuxt 提供 composables 和實用工具,以支援一流的伺服器端渲染。

setResponseStatus 設定回應的 statusCode(以及可選的 statusMessage)。

setResponseStatus 只能在 Nuxt 上下文中呼叫。
const event = useRequestEvent()

// event will be undefined in the browser
if (event) {
  // Set the status code to 404 for a custom 404 page
  setResponseStatus(event, 404)

  // Set the status message as well
  setResponseStatus(event, 404, 'Page Not Found')
}
在瀏覽器中,setResponseStatus 將不會有任何作用。
請在 文件 > 入門 > 錯誤處理 中閱讀更多資訊。