【4】启动Angular过程介绍

启动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里面的内容

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值