Angular 通用
Angular universal
我正在使用 Mapkitjs 创建地图,它是通过 window 对象初始化的,在服务器上我看到错误
TypeError: Cannot read property 'init' of undefined
我如何将脚本添加到服务器以修复该错误?
我试过了
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);
但是不行
绕过服务器端,因为它不支持 DOM 相关内容。
示例:
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);
}
我正在使用 Mapkitjs 创建地图,它是通过 window 对象初始化的,在服务器上我看到错误
TypeError: Cannot read property 'init' of undefined
我如何将脚本添加到服务器以修复该错误? 我试过了
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);
但是不行
绕过服务器端,因为它不支持 DOM 相关内容。
示例:
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);
}