听蝉 發表於 2020-1-26 13:19:00

Angular的启动过程

<p>&nbsp; &nbsp; &nbsp; 我们知道由命令 ng new project-name,cli将会创建一个基础的angular应用,我们是可以直接运行起来一个应用。这归功与cli已经给我们创建好了一个根模块AppModule,而根模块就是用来启动此应用的模块。</p>
<p>&nbsp; &nbsp; &nbsp;main.ts 是启动的起点,platformBrowserDynamic这个函数是浏览器平台的工厂函数,执行会返回浏览器平台的实例,然后对根模块进行初始化,链式的将所有的依赖的Module都给加载进来。<span><span>每个应用程序都是通过模块的using&nbsp;</span></span><code class="ho is it iu iv b">bootstrapModule</code><span><span>方法</span><span>创建的。</span></span></p>
<div class="cnblogs_code">
<pre>import { enableProdMode } <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">@angular/core</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
import { platformBrowserDynamic } </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">@angular/platform-browser-dynamic</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;

import { AppModule } </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./app/app.module</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
import { environment } </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./environments/environment</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (environment.production) {
enableProdMode();
}
<br>// 首先创建平台,然后创建应用程序实例。
platformBrowserDynamic().bootstrapModule(AppModule)
.</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(err =&gt; console.error(err));</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp;&nbsp;<span><span>创建应用程序时,Angular会检查根模块AppModule,AppModule的属性</span></span><code class="ho is it iu iv b">bootstrap</code><span><span>用于引导应用程序。<span>此属性通常来来引导应用程序的组件。</span><span>然后Angular在DOM中找到作为自举组件的选择器的元素,并初始化该组件。大概就这样吧。</span></span></span></p>
<div class="cnblogs_code">
<pre>import { BrowserModule } <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">@angular/platform-browser</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
import { NgModule } </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">@angular/core</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;

import { AppRoutingModule } </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./app-routing.module</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
import { AppComponent } </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./app.component</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;

@NgModule({
declarations: [
    AppComponent
],
imports: [
    BrowserModule,
    AppRoutingModule
],
providers: [],
bootstrap:
})
export </span><span style="color: rgba(0, 0, 255, 1)">class</span> AppModule { }</pre>
</div>
<div class="_2Uzcx_">
<p>此随笔乃本人学习工作记录,如有疑问欢迎在下面评论,转载请标明出处。</p>
<p>如果对您有帮助请动动鼠标右下方给我来个赞,您的支持是我最大的动力。</p>
</div><br><br>
来源:https://www.cnblogs.com/huangenai/p/12216518.html
頁: [1]
查看完整版本: Angular的启动过程