填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

PlatformRef

Angular 平臺是 Angular 在網頁上的入口點。每個頁面只有一個平臺。頁面上執行的每個 Angular 應用程式所共有的服務(例如反射)都在其範圍內繫結。當使用 PlatformBrowser 這樣的平臺工廠建立平臺時,將隱式初始化此頁面的平臺;也可以透過呼叫 createPlatform() 函式來顯式初始化此頁面的平臺。

The Angular platform is the entry point for Angular on a web page. Each page has exactly one platform. Services (such as reflection) which are common to every Angular application running on the page are bound in its scope. A page's platform is initialized implicitly when a platform is created using a platform factory such as PlatformBrowser, or explicitly by calling the createPlatform() function.

      
      interface PlatformRef {
  injector: Injector
  destroyed
  bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>, options?: BootstrapOptions): Promise<NgModuleRef<M>>
  bootstrapModule<M>(moduleType: Type<M>, compilerOptions: (CompilerOptions & BootstrapOptions) | (CompilerOptions & BootstrapOptions)[] = []): Promise<NgModuleRef<M>>
  onDestroy(callback: () => void): void
  destroy()
}
    

屬性

屬性說明
injector: Injector唯讀

檢索平臺 Injector,該平臺是頁面上每個 Angular 應用程式的父注入器,並提供單例提供者。

Retrieves the platform Injector, which is the parent injector for every Angular application on the page and provides singleton providers.

destroyed唯讀

方法

為給定的平臺建立 @NgModule 的實例,以進行離線編譯。

Creates an instance of an @NgModule for the given platform for offline compilation.

bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>, options?: BootstrapOptions): Promise<NgModuleRef<M>>
      
      bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>, options?: BootstrapOptions): Promise<NgModuleRef<M>>
    
引數
moduleFactory NgModuleFactory
options BootstrapOptions
可選. 預設值是 `undefined`.
返回值

Promise<NgModuleRef<M>>

使用說明

以下示例為瀏覽器平臺建立 NgModule。

The following example creates the NgModule for a browser platform.

my_module.ts: @NgModule({ imports: [BrowserModule] }) class MyModule {} main.ts: import {MyModuleNgFactory} from './my_module.ngfactory'; import {platformBrowser} from '@angular/platform-browser'; let moduleRef = platformBrowser().bootstrapModuleFactory(MyModuleNgFactory);
      
      my_module.ts:

@NgModule({
  imports: [BrowserModule]
})
class MyModule {}

main.ts:
import {MyModuleNgFactory} from './my_module.ngfactory';
import {platformBrowser} from '@angular/platform-browser';

let moduleRef = platformBrowser().bootstrapModuleFactory(MyModuleNgFactory);
    

使用給定的執行時編譯器為給定的平臺建立 @NgModule 的實例。

Creates an instance of an @NgModule for a given platform using the given runtime compiler.

bootstrapModule<M>(moduleType: Type<M>, compilerOptions: (CompilerOptions & BootstrapOptions) | (CompilerOptions & BootstrapOptions)[] = []): Promise<NgModuleRef<M>>
      
      bootstrapModule<M>(moduleType: Type<M>, compilerOptions: (CompilerOptions & BootstrapOptions) | (CompilerOptions & BootstrapOptions)[] = []): Promise<NgModuleRef<M>>
    
引數
moduleType Type
compilerOptions (CompilerOptions & BootstrapOptions) | (CompilerOptions & BootstrapOptions)[]
可選. 預設值是 `[]`.
返回值

Promise<NgModuleRef<M>>

使用說明

簡單的例子
Simple Example
@NgModule({ imports: [BrowserModule] }) class MyModule {} let moduleRef = platformBrowser().bootstrapModule(MyModule);
      
      @NgModule({
  imports: [BrowserModule]
})
class MyModule {}

let moduleRef = platformBrowser().bootstrapModule(MyModule);
    

註冊銷燬平臺時要呼叫的監聽器。

Registers a listener to be called when the platform is destroyed.

onDestroy(callback: () => void): void
      
      onDestroy(callback: () => void): void
    
引數
callback () => void
返回值

void

銷燬頁面上的當前 Angular 平臺和所有 Angular 應用程式。銷燬在平臺上註冊的所有模組和監聽器。

Destroys the current Angular platform and all Angular applications on the page. Destroys all modules and listeners registered with the platform.

destroy()
      
      destroy()
    
引數

沒有引數。