问题描述
我正在开发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
}