问题
I am used to Angular but I am just starting to use universal (for SEO).
I want to use a map from amcharts 4, I have no problems using without Angular Universal. I know that it is a known issue but I do not understand why in my case the server tries to load the chart and create this error:
ERROR Error: Uncaught (in promise): ReferenceError: addEventListener is not defined
ReferenceError: addEventListener is not defined
...
My code :
constructor(
@Inject(PLATFORM_ID) private platformId: Object
) {
}
ngOnInit(): void {
this.setUpChart();
}
setUpChart() {
if (isPlatformBrowser(PLATFORM_ID)) {
let chart = am4core.create("world-map", am4maps.MapChart);
// ...
}
}
回答1:
Thanks to David, I found the problems. There where two :
isPlatformBrowser(PLATFORM_ID) =>
isPlatformBrowser(platformId): Object
The error was indeed in the library so it was occured only outside my condition. This my code now to avoid this problem :
declare var require: any;
@Component({...})
export class MapComponent implements OnInit, OnDestroy {
private chart;
isBrowser: boolean
constructor(
@Inject(PLATFORM_ID) private platformId: Object
) {
this.isBrowser = isPlatformBrowser(platformId)
}
ngOnInit(): void {
if (this.isBrowser) {
this.setUpChart();
}
}
setUpChart() {
if (this.isBrowser) {
const am4core = require("@amcharts/amcharts4/core");
const am4maps = require("@amcharts/amcharts4/maps");
let chart = am4core.create("world-map", am4maps.MapChart);
// ...
}
}
来源:https://stackoverflow.com/questions/62646218/angular-10-universal-amcharts-4-why-the-server-tries-to-load-the-chart