Home > Article > Web Front-end > Why Are Navigator, Window, and Document Undefined in My Nuxt App?
How to Address undefined Error on Navigator/Window/Document in Nuxt
When attempting to retrieve user agent or retina information within a Nuxt application, errors may arise indicating that navigator, window, or document are undefined. This occurs due to JavaScript code being executed during server-side rendering (SSR), which lacks access to browser-specific objects like window or navigator.
Solution
To resolve this issue, wrap your logic JS code within the following construct:
<script> import { jsPlumb } from 'jsplumb' export default { mounted() { if (process.client) { // Your JS code here, like: jsPlumb.ready(function () {}) } }, } </script>
This ensures that your code only executes on the client-side, where these objects are available. Additionally, consider using the
<template> <div> <p>Rendered on both: server + client</p> <client-only> <p>Rendered only on client</p> </client-only> </div> </template>
Additional Tips
For example:
export default { components: { [process.client && 'VueEditor']: () => import('vue2-editor'), } }
By employing these techniques, you can access navigator, window, and document objects within Nuxt applications and resolve the undefined errors encountered.
The above is the detailed content of Why Are Navigator, Window, and Document Undefined in My Nuxt App?. For more information, please follow other related articles on the PHP Chinese website!