使用Angular CLI快速創(chuàng)建Angular項(xiàng)目的一些基本概念和寫法小結(jié)
Angular CLI是一個命令行界面工具,它可以創(chuàng)建項(xiàng)目、添加文件以及執(zhí)行一大堆開發(fā)任務(wù),比如測試、打包和發(fā)布,這里的快速開始就是基于這個命令。
開始項(xiàng)目前,你需要先安裝node和npm,然后執(zhí)行npm install -g @angular/cli安裝Angular CLI。
一:用命令行新建一個項(xiàng)目
ng new newApp --skip-install cd newApp cnpm install ng serve --open
執(zhí)行上面的命令就會自動新建一個Angualr項(xiàng)目,并啟動了項(xiàng)目。
其中--skip-install表示node包先不安裝,我們接著使用cnpm install安裝會快多了。
二:目錄結(jié)構(gòu)
現(xiàn)在來看看ng命令幫助我們生成了什么,也就是項(xiàng)目的目錄結(jié)構(gòu),里面都是干什么的,先有個大致了解,你可以不知道全部,不過先記住下面幾個個人感覺重要的:
1.src:應(yīng)用代碼存放的地方;
2.src/app:你的代碼主要存放的地方,這樣說也許不合適,不過你開發(fā)的時候,大部分時間都是在修改這里的代碼;
3.src/assets:圖片等存放的地方,構(gòu)建時會復(fù)制到發(fā)布包里;
4.src/main.js:你基本不會修改它,它是程序的主入口;
5.src/styles.css:特別用的樣式寫在對應(yīng)的地方,后面會說,對于公共的樣式就會寫在這里;
6.karma.conf.js:給Karma的單元測試配置,當(dāng)運(yùn)行ng test時會用到它。
三:自定義組件
import { Component } from '@angular/core'; @Component({ selector: 'my-comp', template: '<ul><li *ngFor='let row of dataList'>ID:{{row.id}} INFO:{{row.info}}</li></ul>', styles: [`ul{background-color: antiquewhite;padding-left: 10px;list-style: none;}`] }) export class MyComponent { dataList = [ { id: 1, info: "Angular" }, { id: 2, info: "React" }, { id: 3, info: "Vue" } ]; }
上面就已經(jīng)定義好了一個非常簡單的組件,不過在使用前,你還需要在模塊中定義,此時就是src/app/app.module.ts中注冊:
import { NgModule } from '@angular/core'; import { MyComponent } from './my.component'; @NgModule({ declarations: [ MyComponent ] }) ......
現(xiàn)在已經(jīng)注冊好了,你就可以使用了,上面的例子的使用方法很簡單,就是自定義了一個標(biāo)簽my-comp,和普通的div的用法一模一樣。
需要注意的是,為了方便查看,在注冊的例子中我去掉了無關(guān)的代碼,實(shí)際情況還好有包括啟動,別的組件,服務(wù)等的注冊,你可以看看命令行自動生成的別的指令,這里主要還是說明更重要的東西,下同。
類似AngularJS,Angular的selector除了上面的自定義標(biāo)簽,還有別的:
1.selector: 'element-name'//自定義標(biāo)簽選擇器;
2.selector: '.class'//樣式選擇器;
3.selector: '[attribute]'//屬性選擇器;
4.selector: '[attribute=value]'//屬性值選擇器;
5.selector: ':not(sub_selector)'//取反選擇器;
6.selector: 'selector1, selector2'//多種選擇器。
四:自定義服務(wù)
和組件一樣,我們先來定義一個服務(wù)。
import { Injectable } from '@angular/core'; export class DataFormat { id: number; info: string; } @Injectable() export class MyServ { getData(): DataFormat[] { return [ { id: 1, info: "Angular" }, { id: 2, info: "React" }, { id: 3, info: "Vue" } ]; } }
接著來注冊它,服務(wù)和組件在注冊上有點(diǎn)不同,我們現(xiàn)在先注冊在主組件上面吧,默認(rèn)就是在arc/app/app.component.ts文件中注冊:
import { Component } from '@angular/core'; import { MyServ } from './my.service'; @Component({ providers: [MyServ] })
服務(wù)的使用也很簡單,我們這里用構(gòu)造函數(shù)來演示一下:
import { MyServ } from './my.service'; ...... export class MyComponent { dataList: any[]; constructor(private demoService: MyServ) { this.dataList = this.demoService.getData(); } }
還記得自定義組件的代碼嗎?我們就在其中演示了服務(wù)的用法,上面只給出了修改的部分代碼。
五:路由的使用
我們這里給出路由的一個簡單用法,具體的細(xì)節(jié)和上面的類似,會單獨(dú)再去討論,這篇文章的目的就是快速入門使用。
為了方便演示,我們默認(rèn)已經(jīng)定義好了二個組件:MyComponent和My2Component。
首先需要確定index.html頁面的head標(biāo)簽中定義好了<base href="/" rel="external nofollow" >或動態(tài)生成該元素的腳本。
我們先在src/app/app.module.ts中注冊路由:
...... import { RouterModule } from '@angular/router'; @NgModule({ declarations: [MyComponent,My2Component], imports: [ RouterModule.forRoot([ {path: 'my',component: MyComponent}, {path: 'my2',component: My2Component} ]) ] ...... }) ......
使用就很簡單了:
<a routerLink="/my">toMycomp</a> <a routerLink="/my2">toMy2comp</a> <router-outlet></router-outlet>
點(diǎn)擊toMycomp或者toMy2comp就會跳轉(zhuǎn)對應(yīng)的路由設(shè)置的組件了。
六:HTTP
由于@angular/http庫中的HttpModule保存著http相關(guān)的服務(wù),需要先引入進(jìn)來(這里是在src/app/app.module.ts中引入):
import { HttpModule } from '@angular/http'; @NgModule({ imports: [HttpModule] }) ......
現(xiàn)在,http就是一個服務(wù),下面簡單演示一種用法:
...... import { Http } from '@angular/http'; ...... constructor(private http: Http) { http.get('assets/XXX.json').forEach(function (data) { console.log(data['_body']); }); } ......
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angular設(shè)計(jì)模式hierarchical?injector實(shí)現(xiàn)代碼復(fù)用模塊化
這篇文章主要為大家介紹了Angular設(shè)計(jì)模式hierarchical?injector實(shí)現(xiàn)代碼復(fù)用模塊化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10AngularJS實(shí)現(xiàn)的自定義過濾器簡單示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的自定義過濾器,結(jié)合實(shí)例形式分析了AngularJS自定義過濾器的簡單定義與使用操作技巧,需要的朋友可以參考下2019-02-02Angular基于Constructor?Parameter的依賴注入方式詳解
這篇文章主要為大家介紹了Angular基于Constructor?Parameter的依賴注入方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11