判断是否是现代化浏览器,解决Nuxt.js的问题

790 阅读1分钟

背景

还是Nuxt.js 报错的问题, 因为我们的SSR项目要引用一些组件或者方法,这些方法里有的地方使用了window 或者 document, 所以,在使用之前,我们需要加一个判断

解决方案

export function isStandardBrowserEnv () {
    if (typeof navigator !== 'undefined' && (navigator.product === 'ReactNative' ||
        navigator.product === 'NativeScript' ||
        navigator.product === 'NS')) {
        return false;
    }
    return (
        typeof window !== 'undefined' &&
        typeof document !== 'undefined'
    );
}