angular - Angular universal
问题描述
I'm using Mapkitjs to create a map, and it initialized via window object, on the server i see error
TypeError: Cannot read property 'init' of undefined
How i can add script into the server for fix that error? I tried
const win = domino.createWindow(template);
const script = win.document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js';
win.document.body.appendChild(script);
But it doesn't work
解决方案
Just bypass the server side as it does not support DOM-associated stuffs.
Example:
import { isPlatformBrowser } from '@angular/common';
import { Inject, PLATFORM_ID } from '@angular/core';
...
isBrowser: boolean;
constructor(
@Inject(PLATFORM_ID) private platformId
) {
this.isBrowser = isPlatformBrowser(this.platformId);
}
...
if (this.isBrowser) {
// Put your code here
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js';
document.body.appendChild(script);
}
推荐阅读
- excel - 我无法让我的第一个函数公式起作用
- spring - 不同api端点的Spring多种身份验证方法
- visual-studio-code - vscode.d.ts 无法安装
- json - 使用 nginx 的 Angular i18n json 缓存问题
- macos - 使用电子在mac os中创建文件时权限被拒绝
- csv - 将应用程序移动到新服务器后,使用 CsvHelper 库生成的 CSV 文件中缺少逗号/分隔符
- python - 加入 2 个具有不同列数的 pandas 数据帧
- raku - 标签和“顶层不允许控制流命令”(可能的 LTA 错误消息)
- azure - Azure ML studio 导出数据 Azure Storage V2
- java - Java RESTful Web 服务错误:无法访问 WADL