angular創建組件的步驟


首先運行創建組件的命令
ng g component components/news 這樣就會在app下生成一個components文件夾,在components下面生成了news組件
新建好以后它會自動在app.module.ts中,引入並聲明這個組件
import {NewsComponent} from './components/news/news.component';
引入(又稱為注冊這個組件):
@NgModule({
declarations:[
NewsComponent
],
})
在其他地方使用這個組件的話,需要先找到它的名字
例: <app-news></app-news>

 

講解組件:
組件由html,scss,ts組成
如何將業務邏輯里ts的數據綁定到html模板上

怎么聲明數據呢? 在ts文件中聲明 public title='我是新聞組件'(這個是再constructor上面寫的)
然后放到HTML中顯示 花括號綁定數據 {{title}}
推薦使用方式: public student:any = '我是一個學生的屬性'


聲明屬性的幾種方式:
public 公有(默認) 可以在這個類里面使用,也可以在類外面使用
protected 保護類型 它只有在當前類和它的子類里面可以訪問
private 私有 只有在當前類才可以訪問這個屬性 x


聲明一個對象:
public userinfo:object={
username:"張三",
age:'20'
}
接下來前台渲染對象 {{userinfo.username}}


雙重循環:
<ul>
<li *ngFor="let item of cars">
<h2>{{item.cate}}</h2>
<ol>
<li *ngFor="let car of item.list">
{{cra.title}}--{{car.price}}
</li>
</ol>
</li>
</ul>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM