角度通用仅用/index.html打开

问题描述 投票:0回答:2

我已经实现了ssr,它没有错误,事情是添加角度通用后,应用程序不会加载,除非我添加/index.html到URL。那么每件事都有效,所有路线都运转良好!含义:

http://localhost:4000/

继续加载并没有成功,但是

http://localhost:4000/index.html

确实。然后重定向到http://localhost:4000/!但如果我在浏览器中手动输入URL或尝试打开没有/index.html的主页,它将永远加载

angular angular-universal angular7
2个回答
0
投票

这个问题很经典

移动到角度普遍永远记得你不能使用,如here所说

窗口,文档......和其他浏览器对象及其方法(如setTimeout)

所有DOM API

所有其他特定于浏览器的API,如localStorage,IndexedDB ......

问题是,在大多数情况下,你不会面临任何错误或警告!

最好的解决方案是检查平台,然后在必要的地方使用,如下所示

import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';

@Injectable()
export class LocalStorage {

  constructor(@Inject(PLATFORM_ID) protected platformId: Object) {}

  setItem(key: string, value: any) {

    if (isPlatformBrowser(this.platformId)) {

      localStorage.setItem(key, JSON.stringify(value));

    }

  }

}

0
投票

我在我的一个子组件中有一个SetInterval,它是Landing页面的主要组件,删除了setInterval解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.