窗口未定义Angular Universal 10

问题描述

我正在开发Angular 10应用程序,实现SSR后出现以下错误

/Users/a1410978/Desktop/ssr-workspace/asfc-shell/dist/server/main.js:260991
const _window = window ? window : {};
                ^

ReferenceError: window is not defined
    at Module.../asfc-shared/src/lib/tagging/mi.values.ts (/Users/a1410978/Desktop/ssr-workspace/asfc-shell/dist/server/main.js:260991:17)

mi.values.ts

const _window = window as any ? window as any : {};
const cid_global = _window['cid_global'] ? _window['cid_global'] : {brand: 'unknown',locale: {country: '',language: ''},ENV: '' };
const z_application = cid_global.accessPath && cid_global.accessPath.slice(1,cid_global.accessPath.length-1);
const z_host = cid_global['brand'].toUpperCase();
const z_country = cid_global.locale.country;
const z_language = cid_global.locale.language;
const contrastPreference = cid_global.contrastPreference;
const z_converged = 'BW';
const z_env = cid_global.ENV;
let lastPageTitle = 'unknown';

/* istanbul ignore next */
export const analyticsDefinitions = {
  /**
   *  Event tagging general
   ********************************************************/
  'onclick': {
    'dl': 'userevent','z_event': 'clicked','z_application': z_application,'z_host': z_host,'z_country': z_country,'z_language': z_language,'z_converged': z_converged,'z_env': z_env,'z_client_id': maskClientId(getCidConfigParams('clientId')),'z_referer': getCidConfigParams('source'),'z_redir': getCidConfigParams('redirectUri'),'z_login': 0
  },

我知道使用isPlatformBrowser可以解决我的问题,但是我确定如何在恒定文件中使用它。

请指导我

解决方法

或者您也可以以正确的方式使用该支票:

// Consider as client side
const isClient = typeof window !== 'undefined';

let cid_global;

if (isClient) {
  // You will write your client initialization here,so you don't have check window anymore
  cid_global = window['cid_global'] : {brand: 'unknown',locale: {country: '',language: ''},ENV: '' };

  // More to come
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...