Angular プログラムを手動で起動するにはどうすればよいですか? angularjsプログラムを手動で起動する方法の詳細な説明

寻∝梦
リリース: 2018-09-07 16:52:25
オリジナル
1792 人が閲覧しました
この記事では主にangularjsプログラムを手動で起動する方法について詳しく説明します

Angularの公式ドキュメントには、 と書かれています。 Angular プログラムを開始するには、次のコードを main.ts ファイルに記述する必要があります: main.ts 文件里写上如下代码:

platformBrowserDynamic().bootstrapModule(AppModule);
ログイン後にコピー

这行代码 platformBrowserDynamic() 是为了构造一个 platform,Angular 官方文档对 platform 的定义是(译者注:为清晰理解,platform 定义不翻译):

the entry point for Angular on a web page. Each page has exactly one platform, and services (such as reflection) which are common to every Angular application running on the page are bound in its scope.

同时,Angular 也有 运行的程序实例(running application instance)的概念,你可以使用 ApplicationRef 标记(token)作为参数注入从而获取其实例。上文的 platform 定义也隐含了一个 platform 可以拥有多个 application 对象,而每一个 application 对象是通过 bootstrapModule 构造出来的,构造方法就像上文 main.ts 文件中使用的那样。所以,上文的 main.ts 文件中代码,首先构造了一个 platform 对象和一个 application 对象。

application 对象被正在构造时,Angular 会去检查模块 AppModulebootstrap 属性,该模块是用来启动程序的:

@NgModule({
  imports: [BrowserModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}
ログイン後にコピー

bootstrap 属性通常包含用来启动程序的组件(译者注:即根组件),Angular 会在 DOM 中查询并匹配到该启动组件的选择器,然后实例化该启动组件。(想看更多就到PHP中文网AngularJS开发手册中学习)

Angular 启动过程隐含了你想要哪一个组件去启动程序,但是如果启动程序的组件是在运行时才被定义的该怎么办呢?当你获得该组件时,又该如何启动程序呢?事实上这是个非常简单的过程。

NgDoBootstrap

假设我们有 AB 两个组件,将编码决定运行时使用哪一个组件来启动程序,首先让我们定义这两个组件吧:

import { Component } from '@angular/core';

@Component({
  selector: 'a-comp',
  template: `<span>I am A component</span>`
})
export class AComponent {}

@Component({
  selector: 'b-comp',
  template: `<span>I am B component</span>`
})
export class BComponent {}
ログイン後にコピー

然后在 AppModule 中注册这两个组件:

@NgModule({
  imports: [BrowserModule],
  declarations: [AComponent, BComponent],
  entryComponents: [AComponent, BComponent]
})
export class AppModule {}
ログイン後にコピー

注意,这里因为我们得自定义启动程序,从而没有在 bootstrap 属性而是 entryComponents 属性中注册这两个组件,并且通过在 entryComponents 注册组件,Angular 编译器(译者注:Angular 提供了 @angular/compiler 包用来编译我们写的 angular 代码,同时还提供了 @angular/compiler-cli CLI 工具)会为这两个组件创建工厂类(译者注:Angular Compiler 在编译每一个组件时,会首先把该组件类转换为对应的组件工厂类,即 a.component.ts 被编译为 a.component.ngfactory.ts)。因为 Angular 会自动把在 bootstrap 属性中注册的组件自动加入入口组件列表,所以通常不需要把根组件注册到 entryComponents 属性中。(译者注:即在 bootstrap 属性中注册的组件不需要在 entryComponents 中重复注册)。

由于我们不知道 A 还是 B 组件会被使用,所以没法在 index.html 中指定选择器,所以 index.html 看起来只能这么写(译者注:我们不知道服务端返回的是 A 还是 B 组件信息):

<body>
  <h1 id="status">
     Loading AppComponent content here ...
  </h1>
</body>
ログイン後にコピー

如果此时运行程序会有如下错误:

The module AppModule was bootstrapped, but it does not declare “@NgModule.bootstrap” components nor a “ngDoBootstrap” method. Please define one of these

错误信息告诉我们, Angular 在向抱怨我们没有指定具体使用哪一个组件来启动程序,但是我们的确不能提前知道(译者注:我们不知道服务端何时返回什么)。等会儿我们得手动在 AppModule 类中添加 ngDoBootstrap 方法来启动程序:

export class AppModule {
  ngDoBootstrap(app) {  }
}
ログイン後にコピー

Angular 会把 ApplicationRef 作为参数传给 ngDoBootstrap(译者注:参考 Angular 源码中这一行),等会准备启动程序时,使用 ApplicationRefbootstrap 方法初始化根组件。

让我们写一个自定义方法 bootstrapRootComponent

// app - reference to the running application (ApplicationRef)
// name - name (selector) of the component to bootstrap
function bootstrapRootComponent(app, name) {
  // define the possible bootstrap components 
  // with their selectors (html host elements)
  // (译者注:定义从服务端可能返回的启动组件数组)
  const options = {
    'a-comp': AComponent,
    'b-comp': BComponent
  };
  // obtain reference to the DOM element that shows status
  // and change the status to `Loaded` 
  //(译者注:改变 id 为 #status 的内容)
  const statusElement = document.querySelector('#status');
  statusElement.textContent = 'Loaded';
  // create DOM element for the component being bootstrapped
  // and add it to the DOM
  // (译者注:创建一个 DOM 元素)
  const componentElement = document.createElement(name);
  document.body.appendChild(componentElement);
  // bootstrap the application with the selected component
  const component = options[name];
  app.bootstrap(component); // (译者注:使用 bootstrap() 方法启动组件)
}
ログイン後にコピー
ログイン後にコピー
このコード行 platformBrowserDynamic() は、platform、Angular の公式ドキュメントでは、platform を次のように定義しています (翻訳者注: 明確に理解するために、platform の定義は次のとおりです)未翻訳): 🎜🎜 Web ページ上の Angular のエントリ ポイントは、各ページに 1 つのプラットフォームがあり、ページ上で実行されているすべての Angular アプリケーションに共通するサービス (リフレクションなど) がそのスコープにバインドされています。同時に、Angular には実行アプリケーション インスタンス (実行アプリケーション インスタンス) の概念もあり、パラメータ インジェクションとして ApplicationRef トークンを使用してそのインスタンスを取得できます。上記の platform の定義は、platform が複数の application オブジェクトを持つことができ、各 application オブジェクトが上記の main.ts ファイルで使用されているのと同じ構築方法を使用して、bootstrapModule 経由で構築されます。したがって、上記の main.ts ファイル内のコードは、最初に platform オブジェクトと application オブジェクトを構築します。 🎜🎜 application オブジェクトが構築されているとき、Angular はプログラムの起動に使用されるモジュール AppModulebootstrap 属性をチェックします。 🎜
function fetch(url) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve('b-comp');
    }, 2000);
  });
}
ログイン後にコピー
ログイン後にコピー
🎜bootstrap 属性には通常、プログラムの起動に使用されるコンポーネント (翻訳者注: ルート コンポーネント) が含まれます。Angular は DOM 内の起動コンポーネントのセレクターを照会して照合し、Start コンポーネントをインスタンス化します。成分。 (さらに詳しく知りたい場合は、PHP 中国語 Web サイトAngularJS 開発マニュアル🎜 にアクセスして学習してください)🎜🎜Angular のスタートアッププロセスは暗黙的です どのコンポーネントをプログラムを開始したいのですが、プログラムを開始するコンポーネントが実行時に定義されている場合はどうなるでしょうか?コンポーネントを入手したら、プログラムをどのように起動しますか?実際には非常に簡単なプロセスです。 🎜

NgDoBootstrap

🎜 2 つのコンポーネント AB があるとします。まず、実行時にどのコンポーネントを使用するかを決定します。これら 2 つのコンポーネントを定義しましょう: 🎜
export class AppModule {
  ngDoBootstrap(app) {
    fetch('url/to/fetch/component/name')
      .then((name)=>{ this.bootstrapRootComponent(app, name)});
  }
}
ログイン後にコピー
ログイン後にコピー
🎜 次に、これら 2 つのコンポーネントを AppModule に登録します: 🎜
import {AComponentNgFactory, BComponentNgFactory} from './components.ngfactory.ts';
@NgModule({
  imports: [BrowserModule],
  declarations: [AComponent, BComponent]
})
export class AppModule {
  ngDoBootstrap(app) {
    fetch('url/to/fetch/component/name')
      .then((name) => {this.bootstrapRootComponent(app, name);});
  }
  bootstrapRootComponent(app, name) {
    const options = {
      'a-comp': AComponentNgFactory,
      'b-comp': BComponentNgFactory
    };
    ...
ログイン後にコピー
ログイン後にコピー
🎜 スタートアップ プログラムをカスタマイズする必要があるため、bootstrap がないことに注意してください。これら 2 つのコンポーネントを entryComponents 属性に登録する代わりに code> 属性を使用し、コンポーネントを entryComponents に登録することで、Angular コンパイラ (翻訳者注: Angular は @angular/compiler パッケージは、作成した Angular コードをコンパイルするために使用され、@angular/compiler-cli CLI も提供します。ツール ) は、これら 2 つのコンポーネントのファクトリ クラスを作成します (翻訳者注: 各コンポーネントをコンパイルするとき、Angular Compiler はまずコンポーネント クラスを対応するコンポーネント ファクトリ クラスに変換します。つまり、a.component.ts は a.component .ngfactory にコンパイルされます) .ts)。 Angular は bootstrap 属性に登録されたコンポーネントをエントリ コンポーネント リストに自動的に追加するため、通常はルート コンポーネントを entryComponents 属性に登録する必要はありません。 (翻訳者注: bootstrap 属性に登録されたコンポーネントは、entryComponents に繰り返し登録する必要はありません)。 🎜🎜 A コンポーネントと B コンポーネントのどちらが使用されるかわからないため、index.html でセレクターを指定することはできませんしたがって、 index.html がそれを記述する唯一の方法のようです (翻訳者注: サーバーが A を返すか B を返すかはわかりません) > コンポーネント情報): 🎜rrreee 🎜この時点でプログラムを実行すると、次のエラーが表示されます: 🎜🎜モジュール AppModule はブートストラップされましたが、「@NgModule.bootstrap」コンポーネントも「ngDoBootstrap」メソッドも宣言していませんこれらのいずれかを定義してください🎜🎜 エラー メッセージは、Angular がプログラムの開始に使用するコンポーネントを指定していないことを示していますが、事前に知ることはできません (翻訳者注: わかりません)サーバーが何かを返すとき)。後で、プログラムを開始するために ngDoBootstrap メソッドを AppModule クラスに手動で追加する必要があります。 🎜rrreee🎜Angular は ApplicationRef をパラメータとして渡します。 ngDoBootstrap (翻訳者注: Angular ソース コードのこの行を参照してください)、プログラムを開始する準備ができたら、ApplicationRef の <code>bootstrap メソッドを使用します。 code> を使用してルートコンポーネントを初期化します。 🎜🎜ルート コンポーネントを開始するカスタム メソッド bootstrapRootComponent を作成しましょう: 🎜
// app - reference to the running application (ApplicationRef)
// name - name (selector) of the component to bootstrap
function bootstrapRootComponent(app, name) {
  // define the possible bootstrap components 
  // with their selectors (html host elements)
  // (译者注:定义从服务端可能返回的启动组件数组)
  const options = {
    'a-comp': AComponent,
    'b-comp': BComponent
  };
  // obtain reference to the DOM element that shows status
  // and change the status to `Loaded` 
  //(译者注:改变 id 为 #status 的内容)
  const statusElement = document.querySelector('#status');
  statusElement.textContent = 'Loaded';
  // create DOM element for the component being bootstrapped
  // and add it to the DOM
  // (译者注:创建一个 DOM 元素)
  const componentElement = document.createElement(name);
  document.body.appendChild(componentElement);
  // bootstrap the application with the selected component
  const component = options[name];
  app.bootstrap(component); // (译者注:使用 bootstrap() 方法启动组件)
}
ログイン後にコピー
ログイン後にコピー

传入该方法的参数是 ApplicationRef 和启动组件的名称,同时定义变量 options 来映射所有可能的启动组件,并以组件选择器作为 key,当我们从服务器中获取所需要信息后,再根据该信息查询是哪一个组件类。

先构建一个 fetch 方法来模拟 HTTP 请求,该请求会在 2 秒后返回 B 组件选择器即 b-comp 字符串:

function fetch(url) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve('b-comp');
    }, 2000);
  });
}
ログイン後にコピー
ログイン後にコピー

现在我们拥有 bootstrap 方法来启动组件,在 AppModule 模块的 ngDoBootstrap 方法中使用该启动方法吧:

export class AppModule {
  ngDoBootstrap(app) {
    fetch('url/to/fetch/component/name')
      .then((name)=>{ this.bootstrapRootComponent(app, name)});
  }
}
ログイン後にコピー
ログイン後にコピー

这里我做了个 stackblitz demo 来验证该解决方法。(译者注:译者把该作者 demo 中 angular 版本升级到最新版本 5.2.9,可以查看 angular-bootstrap-process,2 秒后会根据服务端返回信息自定义启动 application

在 AOT 中能工作么?

当然可以,你仅仅需要预编译所有组件,并使用组件的工厂类来启动程序:

import {AComponentNgFactory, BComponentNgFactory} from './components.ngfactory.ts';
@NgModule({
  imports: [BrowserModule],
  declarations: [AComponent, BComponent]
})
export class AppModule {
  ngDoBootstrap(app) {
    fetch('url/to/fetch/component/name')
      .then((name) => {this.bootstrapRootComponent(app, name);});
  }
  bootstrapRootComponent(app, name) {
    const options = {
      'a-comp': AComponentNgFactory,
      'b-comp': BComponentNgFactory
    };
    ...
ログイン後にコピー
ログイン後にコピー

记住我们不需要在 entryComponents 属性中注册组件,因为我们已经有了组件的工厂类了,没必要再通过 Angular Compiler 去编译组件获得组件工厂类了。(译者注:components.ngfactory.ts 是由 Angular AOT Compiler 生成的,最新 Angular 版本 在 CLI 里隐藏了该信息,在内存里临时生成 xxx.factory.ts 文件,不像之前版本可以通过指令物理生成这中间临时文件,保存在硬盘里。)

好了,本篇文章到这就结束了(想看更多就到PHP中文网AngularJS使用手册中学习),有问题的可以在下方留言提问。

以上がAngular プログラムを手動で起動するにはどうすればよいですか? angularjsプログラムを手動で起動する方法の詳細な説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!