在本文中,我们将了解 Angular 10 中的isPlatformBrowser是什么以及如何使用它。
isPlatformBrowser用于获取代表浏览器平台的平台 id
句法:
isPlatformBrowser(platformId);
NgModule:isPlatformBrowser使用的模块是:
返回值:返回一个布尔值,说明平台 id 是否代表浏览器平台。
方法:
示例 1:
import { Component, Inject }
from '@angular/core';
import { PLATFORM_ID }
from '@angular/core';
import { isPlatformBrowser }
from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
isBrowser: boolean;
constructor( @Inject(PLATFORM_ID) platformId: Object) {
this.isBrowser = isPlatformBrowser(platformId);
console.log(this.isBrowser)
}
}
import { Component, Inject }
from '@angular/core';
import { PLATFORM_ID }
from '@angular/core';
import { isPlatformBrowser }
from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
isB: boolean;
constructor( @Inject(PLATFORM_ID) platformId: Object) {
this.isB = isPlatformBrowser(platformId);
}
}
GeeksforGeeks.
isBrowserPlatform | angularJs.
输出:
示例 2:
import { Component, Inject }
from '@angular/core';
import { PLATFORM_ID }
from '@angular/core';
import { isPlatformBrowser }
from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
isB: boolean;
constructor( @Inject(PLATFORM_ID) platformId: Object) {
this.isB = isPlatformBrowser(platformId);
}
}
GeeksforGeeks.
isBrowserPlatform | angularJs.
输出:
参考: https://www.sychzs.cn/api/common/isPlatformBrowser