最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular環(huán)境搭建及簡單體驗小結(jié)

 更新時間:2021年05月28日 14:28:40   作者:無風(fēng)聽海  
Angular基于TypeScript和react、vue相比 Angular更適合中大型企業(yè)級項目,本文通過實例代碼給大家分享Angular環(huán)境搭建及簡單體驗,感興趣的朋友跟隨小編一起學(xué)習(xí)吧

Angular介紹

Angular是谷歌開發(fā)的一款開源的web前端框架,誕生于2009年,由Misko Hevery 等人創(chuàng)建,后為Google所收購。是一款優(yōu)秀的前端JS框架,已經(jīng)被用于Google的多款產(chǎn)品當(dāng)中。
根據(jù)項目數(shù)統(tǒng)計angular(1.x 、2.x 、4.x、5.x、6.x、7.x 、8.x、9.x)是現(xiàn)在網(wǎng)上使用量最大的框架。

Angular基于TypeScript和react、vue相比 Angular更適合中大型企業(yè)級項目。

關(guān)于Angular版本,Angular官方已經(jīng)統(tǒng)一命名Angular 1.x統(tǒng)稱為Angular JS;Angular 2.x及以上統(tǒng)稱Angular;

目前2019年12月25日angular最新版本angular9.x。根據(jù)官方介紹,Angular每過幾個月就會更新一個版本。Angular2.x以后所有的Angular版本用法都是一樣的,此教程同樣適用于Angular7.x 、Angular8.x、Angular9.x 以及未來的其它版本…。

學(xué)習(xí)Angular必備基礎(chǔ)
必備基礎(chǔ):html 、css 、js、es6、ts

一、安裝開發(fā)環(huán)境

npm install -g typescript
npm install -g @angular/cli

二、創(chuàng)建hello-world項目

創(chuàng)建項目

ng new angular2-hello-world

查看新建項目的目錄結(jié)構(gòu)

cd angular2-hello-world
sudo apt install tree
tree -F -L 1
.
├── angular.json
├── karma.conf.js
├── node_modules/
├── package.json
├── package-lock.json
├── README.md
├── src/
├── tsconfig.app.json
├── tsconfig.json
└── tsconfig.spec.json

2 directories, 8 files

查看src目錄里的結(jié)構(gòu)

cd src
tree -F

啟動應(yīng)用,可以在http://localhost:4200查看運行結(jié)果

ng serve

創(chuàng)建hello-world組件

ng-generate component hello-world

在hello-world.component.ts中定義新的組件

//導(dǎo)入依賴
import { Component, OnInit } from '@angular/core';

//通過注解聲明控件的選擇器和相關(guān)的文件url
@Component({
  selector: 'app-hello-world',
  templateUrl: './hello-world.component.html',
  styleUrls: ['./hello-world.component.css']
})

//組件的數(shù)據(jù)模型
export class HelloWorldComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}

在hello-world.component.html中定義模板

<p>mango, hello-world works!</p>

為了使用新增加的組件,我們把標(biāo)簽添加到app.component.html中。

<h1>
  <app-hello-world></app-hello-world>
</h1>

執(zhí)行 ng serve查看執(zhí)行效果

三、創(chuàng)建展示用戶的user-item指令

生成指令組件

mango@mango:~/angular2-hello-world$ ng generate component user-item
CREATE src/app/user-item/user-item.component.css (0 bytes)
CREATE src/app/user-item/user-item.component.html (24 bytes)
CREATE src/app/user-item/user-item.component.spec.ts (641 bytes)
CREATE src/app/user-item/user-item.component.ts (286 bytes)
UPDATE src/app/app.module.ts (585 bytes)

為組件聲明并初始化一個name字段

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

@Component({
  selector: 'app-user-item',
  templateUrl: './user-item.component.html',
  styleUrls: ['./user-item.component.css']
})
export class UserItemComponent implements OnInit {
  name: string,

  constructor() { 
    this.name = 'mango';
  }

  ngOnInit(): void {
  }

}

在模板中顯示變量name的值

<p>
    {{name}} welcome  into Angular world.
</p>

將app-user-item添加到app.component.html中,查看瀏覽器執(zhí)行結(jié)果。

四、創(chuàng)建用戶列表user-list指令

創(chuàng)建新組件

mango@mango:~/angular2-hello-world$ ng generate component user-list
CREATE src/app/user-list/user-list.component.css (0 bytes)
CREATE src/app/user-list/user-list.component.html (24 bytes)
CREATE src/app/user-list/user-list.component.spec.ts (641 bytes)
CREATE src/app/user-list/user-list.component.ts (286 bytes)
UPDATE src/app/app.module.ts (677 bytes)

在組件中聲明并初始化names數(shù)組

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

@Component({
  selector: 'app-user-list',
  templateUrl: './user-list.component.html',
  styleUrls: ['./user-list.component.css']
})
export class UserListComponent implements OnInit {
  names: string[];
  constructor() { 
    this.names = ['mango', 'pear', 'grap', 'apple'];
  }

  ngOnInit(): void {
  }

}

在組件的模板中遞歸遍歷names數(shù)組

<ul>
    <li *ngFor="let name of names">Hello {{name}}</li>
</ul>

將組件添加app.component.html中,查看瀏覽器執(zhí)行結(jié)果。

五、組合使用user-item和user-list

修改user-item的name參數(shù)使用外部輸入

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-user-item',
  templateUrl: './user-item.component.html',
  styleUrls: ['./user-item.component.css']
})
export class UserItemComponent implements OnInit {
  @Input()
  name!: string;

  constructor() { 
    
  }

  ngOnInit(): void {
  }

}

修改user-list的模板

<ul>
    <app-user-item *ngFor="let name of names" [name]="name"></app-user-item>
</ul>

保存查看瀏覽器執(zhí)行情況。

六、啟動過程分析

ng會首先從angular.json中查找程序的main入口為src/main.ts

{
            "outputPath": "dist/angular2-hello-world",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }

查看main.ts文件,發(fā)現(xiàn)啟動的Module是AppModule,位于app/app.module.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.error(err));

在app.module.ts中可以看到,通過NgModule標(biāo)注聲明了本模塊中的組件以及依賴的外部Module及作為頂層組件啟動的AppComponent;

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HelloWorldComponent } from './hello-world/hello-world.component';
import { UserItemComponent } from './user-item/user-item.component';
import { UserListComponent } from './user-list/user-list.component';

@NgModule({
  declarations: [
    AppComponent,
    HelloWorldComponent,
    UserItemComponent,
    UserListComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

以上就是Angular環(huán)境搭建及簡單體驗的詳細(xì)內(nèi)容,更多關(guān)于Angular環(huán)境搭建的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Angular?Ngrx?Store應(yīng)用程序狀態(tài)典型示例詳解

    Angular?Ngrx?Store應(yīng)用程序狀態(tài)典型示例詳解

    這篇文章主要為大家介紹了Angular?Ngrx?Store應(yīng)用程序狀態(tài)典型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • AngularJS實踐之使用ng-repeat中$index的注意點

    AngularJS實踐之使用ng-repeat中$index的注意點

    最近通過客戶的投訴主要到在ng-repeat中使用了$index引發(fā)的一個bug,下面一起來看看這個錯誤是如何引發(fā)的, 以及如何避免這種bug產(chǎn)生,然后說說我們從中得到的經(jīng)驗和教訓(xùn)。有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-12-12
  • angular $watch 一個變量的變化(實例講解)

    angular $watch 一個變量的變化(實例講解)

    下面小編就為大家?guī)硪黄猘ngular $watch 一個變量的變化(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • angular簡介和其特點介紹

    angular簡介和其特點介紹

    這篇文章主要介紹了angular簡介和其特點介紹,本文講解了關(guān)于和jquery的比較、關(guān)于適用場合、關(guān)于UI的結(jié)合、關(guān)于angularjs的特點等內(nèi)容,需要的朋友可以參考下
    2015-01-01
  • 詳解如何為你的angular app構(gòu)建一個第三方庫

    詳解如何為你的angular app構(gòu)建一個第三方庫

    這篇文章主要介紹了詳解如何為你的angular app構(gòu)建一個第三方庫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 淺談Angular4實現(xiàn)熱加載開發(fā)旅程

    淺談Angular4實現(xiàn)熱加載開發(fā)旅程

    本篇文章主要介紹了淺談Angular4實現(xiàn)熱加載開發(fā)旅程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解 (上)

    Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解 (上)

    這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來看看吧。
    2017-07-07
  • angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動效果

    angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動效果

    這篇文章主要為大家詳細(xì)介紹了angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動效果的相關(guān)資料,angularjs modal模態(tài)框創(chuàng)建可拖動的指令,感興趣的小伙伴們可以參考一下
    2016-01-01
  • AngularJS 表達(dá)式詳解及實例代碼

    AngularJS 表達(dá)式詳解及實例代碼

    這篇文章主要介紹了AngularJS 表達(dá)式,這里整理了詳細(xì)的資料,有需要的小伙伴可以參考下
    2016-09-09
  • 淺談AngularJs指令之scope屬性詳解

    淺談AngularJs指令之scope屬性詳解

    下面小編就為大家?guī)硪黄獪\談AngularJs指令之scope屬性詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10

最新評論

孟村| 东宁县| 永胜县| 金湖县| 高雄市| 东方市| 黑龙江省| 喀什市| 南雄市| 阿拉善左旗| 华池县| 莱西市| 合阳县| 定州市| 辽中县| 依安县| 安仁县| 内乡县| 百色市| 随州市| 宜阳县| 泗水县| 鄂伦春自治旗| 交城县| 平顺县| 马尔康县| 双柏县| 延津县| 望都县| 天镇县| 清丰县| 石嘴山市| 邯郸市| 乐业县| 历史| 翁源县| 阜新| 含山县| 台江县| 新民市| 云霄县|