PLATFORM_ID
令牌和
isPlatformBrowser
isPlatformServer
在模板中使用
#ngIf
声明:
<section class="sec-space-b" id="banner" *ngIf="isBrowser">
在组件代码中初始化
isBrowser
import { isPlatformBrowser } from '@angular/common';
import { Component, OnInit, Inject, PLATFORM_ID } from '@angular/core';
@Component({
selector: 'app-home-banner',
templateUrl: './banner.component.html',
styleUrls: ['./banner.component.scss']
})
export class BannerComponent implements OnInit {
public isBrowser = isPlatformBrowser(this.platformId);
constructor(@Inject(PLATFORM_ID) private platformId: any) { }
}
你可以读更多关于
isPlatformServer服务器
和
在本文中(此处使用):
https://www.twilio.com/blog/create-search-engine-friendly-internationalized-web-apps-angular-universal-ngx-translate
你也可以看看我关于Angular Universal的演讲(13:26-关于在浏览器和服务器上运行不同的代码):
https://www.youtube.com/watch?v=J42mqpVsg0k