window 未定义 Angular 通用 10

window is not defined 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
}