启动angular应用
我们要搞清楚三件事
1 启动时加载了那个页面?
2启动时加载了那些脚本?
3 这些脚本做了什么事?
打开 文件angular.json
"projects": {
"auction": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/auction",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css",
"./node_modules/bootstrap/dist/css/bootstrap.css"
],
"scripts": [
"./node_modules/jquery/dist/jquery.js",
"./node_modules/bootstrap/dist/js/bootstrap.js"
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
} 在 "index": "src/index.html", 是整个的启动页面
"main": "src/main.ts", 启动时加载的脚本 负责引导脚本启动 我们开看下他的内容
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.log(err));
enableProdMode 关闭angular开发者模式 platformBrowserDynamic方法 告诉angular使用那个模块启动整个应用 AppModule 导入整个应用的主模块 environment 导入配置
if (environment.production) {
enableProdMode();
}如果是生产环境 那么就关闭开发者模式
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.log(err));
调用bootstrapModule方法 传入AppModule模块 这就是整个应用的起点
接着我们接着往下分析app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { FooterComponent } from './footer/footer.component';
import { NavbarComponent } from './navbar/navbar.component';
import { SearchComponent } from './search/search.component';
import { CarouselComponent } from './carousel/carousel.component';
import { ProductComponent } from './product/product.component';
import { StartsComponent } from './starts/starts.component';
@NgModule({
declarations: [
AppComponent,
FooterComponent,
NavbarComponent,
SearchComponent,
CarouselComponent,
ProductComponent,
StartsComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }我们可以看到
imports: [
BrowserModule
],
他会在加载这些模块(模块a)以后接着分析这些模块(模块a)加载了什么模块 (模块b)然后接着加载 (模块b)所加载的(模块c)
以此类推,直到加载完毕所有的所需的依赖
当加载完毕以后
他会在index.html当中寻找启动模块(appModule)指定的主组件( bootstrap: [AppComponent])对应的css选择器
接着我们打开 app.component.ts
import {Component} from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'auction';
}上面所写的css选择器也就是代码中的 app-root
找到这个组件以后 这个组件位置在app.component.html 中的默认当中<app-root>
他会用上述代码中的templateUrl 里的内容 来替换掉 app-root标签
整个过程完成之前
他会先展示<app-root>Loading</app-root> 标签里面的内容 也就是Loading
接着 我们来启动这个程序 启动方法
大家可以看下效果 在点击刷新的时候 先出现Loading 再出现pp.component.html里面的内容










