上一篇 Angular2项目初体验-编写自己的第一个组件

 

  好了,前面简单介绍了Angular2的基本开发,并且写了一个非常简单的组件,这篇文章我们将要学会编写多个组件并且有主从关系

  现在,假设我们要做一个博客,博客的的文章具有主从关系,文章和文章详情;现在,我们新建一个Article的文件夹和其组件的基本架构(顺便说一下,我使用的是vs code 有个Angular2 fiels的插件,可以直接建立)

  效果如下

  

  我们需要显示博客,那就要建立一个blogdata的数据来源,这里为了方便我们直接采用本地变量的模式来做;为了保证功能单一性原则,我们新建一个data的文件夹,在里面建立Blog类

  

  1. export class Blog{
  2. id:number;
  3. title:string;
  4. }
  5. export let BLOGS:Blog[]=[
  6. { id:1,title:"号外号外?奥巴马要下台啦"},
  7. { id:2,title:"什么?奥巴马要下台啦?"},
  8. { id:3,title:"号外号外?川普要上台了"},
  9. { id:4,title:"啥?我大四川人也要当美国总统了?"},
  10. { id:5,title:"mdzz,一群麻瓜,统统查杀"},
  11. { id:6,title:"首推龙文,必须出具"}
  12. ]

  然后在我们的Article组件中导入它

  

  1. import { Component } from \'@angular/core\';
  2. import {BLOGS,Blog} from \'../data/blog\';
  3.  
  4. @Component({
  5. selector: \'ngarticle\',
  6. templateUrl: \'./article.component.html\',
  7. styleUrls:[\'./article.component.css\']
  8. })
  9.  
  10. export class ArticleComponent {
  11. blogList:Blog[];
  12. constructor()
  13. {
  14. this.blogList=BLOGS;
  15. }
  16. }

  由于article是html自带的标签,我们使用ngarticle作为选择器

  然后,我们开始编写我们的组件模板 article.component.html

  

  1. <div class="article">
  2. <ul class="articleList">
  3. <li *ngFor="let blog of blogList">
  4. <a>
  5. {{blog.id}}:{{blog.title}}
  6. </a>
  7. </li>
  8. </ul>
  9. </div>

  

  接下来是article.component.css

  1. .articleList {
  2. list-style: none;
  3. }
  4.  
  5. .articleList li {
  6. position: relative;
  7. width: 100%;
  8. font-size:16px;
  9. border-bottom: 1px dashed #d9d9d9;
  10. box-sizing: border-box;
  11. border-radius:2px;
  12. }
  13. .articleList li > a {
  14. position:relative;
  15. overflow: hidden;
  16. text-overflow: ellipsis;
  17. white-space: nowrap;
  18. padding: 15px 25px;
  19. display: block;
  20. width: 100%;
  21. color:#333;
  22. height:100%;
  23. }
  24. .articleList li > span {
  25. position:relative;
  26. overflow: hidden;
  27. text-overflow: ellipsis;
  28. white-space: nowrap;
  29. padding: 15px 25px;
  30. display: block;
  31. color:#000;
  32. height:100%;
  33. background:69aa6f
  34. }
  35. .articleList li > a:hover {
  36. color: #69aa6f;
  37. transition: color 0.3s, box-shadow 0.3s;
  38. }
  39. .articleList li:hover {
  40. background: #efefef;
  41. }

  完成之后,在我们的app.component.html中使用它

  <ngarticle></ngarticle>

     然后在命令行中进入项目根目录,使用ng serve命令执行,然后我们就得到了一个错误

 

 错误显示我们的ngarticle是一个未知的组件,未知怎么办,注册嘛,所有的Angular组件都要注册(声明)之后才能使用,具体方法如下

  打开我们的App.Module.ts,我们像声明AppComponent一样声明我们的ArticleComponent,方法就是在declaration中添加我们的组件,这个declaration数组就是我们的组件声明的地方,所有的组件都需要在这里声明之后才可以使用

  

  1. import { BrowserModule } from \'@angular/platform-browser\';
  2. import { NgModule } from \'@angular/core\';
  3. import { FormsModule } from \'@angular/forms\';
  4. import { HttpModule } from \'@angular/http\';
  5.  
  6. import { AppComponent } from \'./app.component\';
  7. import { ArticleComponent } from \'./article/article.component\';
  8.  
  9. @NgModule({
  10. declarations: [
  11. AppComponent,
  12. ArticleComponent
  13. ],
  14. imports: [
  15. BrowserModule,
  16. FormsModule,
  17. HttpModule
  18. ],
  19. providers: [],
  20. bootstrap: [AppComponent]
  21. })
  22. export class AppModule { }

  到此,效果如下

  

  然后我们需要一个显示明细的东西,我们来做个显示明细编辑吧

  新建一个article-detail组件,和上边一样,文件夹

  articledetail.component.ts

  

  1. import { Component, OnInit,Input } from \'@angular/core\';
  2. import {BLOGS,Blog} from \'../data/blog\';
  3.  
  4. @Component({
  5. selector: \'article-detail\',
  6. templateUrl: \'./articledetail.component.html\',
  7. styleUrls:[\'./articledetail.component.css\']
  8. })
  9.  
  10. export class ArticledetailComponent implements OnInit {
  11. @Input() blog:Blog;
  12. ngOnInit() { }
  13. }

  这里我们注意到多了一个@Input()装饰器,这个是用来接受组件输入的,待会儿我们修改article.component.html的时候你就知道是干什么的了

  articledetail.component.html

  

  1. <div class="articledetail" *ngIf="blog">
  2. <h2>文章明细</h2>
  3. <div class="content">
  4. <div class="row">
  5. <span >ID</span>
  6. <span>{{blog.id}}</span>
  7. </div>
  8. <div class="row">
  9. <span >Title</span>
  10. <input type="text" class="myInput" [(ngModel)]="blog.title"/>
  11. </div>
  12. </div>
  13. </div>

  这里用了*ngIf 值为blog,表示如果blog有值,为真,那么就显示内容,否则不显示

  我们注意到这里又多了一个[(ngModel)],简单就理解为input一类标签的值绑定,双向的哦

  articledetail.component.css

  

  1. .articledetail {
  2. margin:20px;
  3. width: 100%;
  4. margin: 0 auto;
  5. }
  6. h2{
  7. text-align: center;
  8. }
  9. .row{
  10. width: 100%;
  11. padding: 10px 20px;
  12. }
  13. .row>span{
  14. margin-left: 25px;
  15. }
  16. .myInput{
  17. height: 36px;
  18. line-height: 36px;
  19. border: 0px;
  20. border-bottom: 2px solid #000;
  21. padding: 6px 12px;
  22. box-shadow: 0px 5px 10px #ccc;
  23. width: auto;
  24. }

  因为加入了detail组件,我们先到app.module.ts中注册(声明)下,代码我就不贴了,大家都会了

  然后是修改刚才的article组件

  article.component.ts

  

  1. import { Component } from \'@angular/core\';
  2. import {BLOGS,Blog} from \'../data/blog\';
  3.  
  4. @Component({
  5. selector: \'ngarticle\',
  6. templateUrl: \'./article.component.html\',
  7. styleUrls:[\'./article.component.css\']
  8. })
  9.  
  10. export class ArticleComponent {
  11. blogList:Blog[];
  12. selectedBlog:Blog;
  13. constructor()
  14. {
  15. this.blogList=BLOGS;
  16. }
  17. selectBlog(blog:Blog)
  18. {
  19. this.selectedBlog=blog;
  20. }
  21. }

  然后是article.component.html

  

  1. <div class="article">
  2. <ul class="articleList">
  3. <li *ngFor="let blog of blogList" (click)="selectBlog(blog)">
  4. <a>
  5. {{blog.id}}:{{blog.title}}
  6. </a>
  7. </li>
  8. </ul>
  9. <div>
  10. <article-detail [blog]="selectedBlog"></article-detail>
  11. </div>
  12. </div>

  

  我们看到article.component.ts中新增了一个属性selectedBlog,多了一个事件 selectBlog;这个事件通过(click)=”selectBlog(blog)”绑定到了li标签的点击事件上,没错,Angular的点击事件就是这样么绑定的,然后我们在下方看到了我们的article-detail 组件的标签,里面有个 [blog]=”selectedBlog”,结合到刚才detail组件的@Input() blog:Blog 属性,我们就明白了,Input故名思议就是输入,可以接收父组件的参数;

  这里简单聊一聊[blog]类似的传值,[X]会将值绑定到组件或者单个标签的prototype(非attr)上,比如一个input标签有value 的prototy,我们也可以使用这样的绑定,你可以在article-detail组件中的input试一试,而这种绑定是单向的。由此,我们联想到刚才的[(ngModel)],好吧,其实[()]语法相当于两个语法,一个是[value]值绑定。一个是(input)事件的绑定;可以理解为

[ngModel]=”blog.title”+(ngModelChange)=”blog.title=$event”;这两个事件更原始的模样是[value]=”blog.title”+(input)=”blog.title=$event.target.value”,不过我们不用去关心Angular2帮我们做的这些事情,知道怎么用就行了吧

  行文到此,我们该来看下效果了

  

 

  结束语:到此我们可以完整得编写组件了,也知道了一些组件间的交互模式,嗨,也不知道多说些啥,下一篇文章我们将要了解Angular的服务

  更新ing。。。

  


  

  项目已经放到了gitbub上,地址 https://github.com/SeeSharply/LearnAngular

  本文章的提交 https://github.com/SeeSharply/LearnAngular/tree/a877676846b22b6dbe5430d02b01d25fb5463c61

  

  

 

版权声明:本文为seesharply原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/seesharply/p/6200468.html