从flask视角理解angular(二)Blueprint VS Component

Component相似flask app下面的每一个blueprint。css

 

import 'rxjs/add/operator/switchMap';
import { Component, OnInit }        from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { Location }                 from '@angular/common';

import { Hero }         from './hero';
import { HeroService }  from './hero.service';
@Component({
  selector: 'hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: [ './hero-detail.component.css' ]
})
export class HeroDetailComponent implements OnInit {
  hero: Hero;

  constructor(
    private heroService: HeroService,
    private route: ActivatedRoute,
    private location: Location
  ) {}

  ngOnInit(): void {
    this.route.paramMap
      .switchMap((params: ParamMap) => this.heroService.getHero(+params.get('id')))
      .subscribe(hero => this.hero = hero);
  }

  goBack(): void {
    this.location.back();
  }
}

 

@Component 说明了两个路径,html


1 /templates/每一个blueprint下的用 jinjapython

2 语法的XXX.html,2 /static/下的 cssnginx

也能够直接把模板内容和css直接写在@Component下面,这时Component有点相似Unity3d里的gameobject,@Component里面的“selector,template” 有点相似gameobject里的“transfrom”,"material"。(U3d里是gameobject->component  不要和ng的Component混了)。web


区别在于:flask强调"动静分离"。这样部署的时候,static部分用nginx, web api部分 用 gunicorn。flask

angular的“先后端”通通用ts/js搞了。这样开发者不须要太在意“动静”与“先后"的分野。封装程度相似unity3d里的prefab,感受很不错。后端

 

定义

每次开头都要导入Componentapi

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

相似blueprint:app

from flask import Blueprint

 

导出ide

export class AppComponent {
  title = 'Tour of Heroes'; hero = 'Windstorm'; }

相似

user_blueprint = Blueprint('user', __name__, url_prefix='/user')

 

OnInit

不一样于构造函数constructor。

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

export class AppComponent implements OnInit {
  ngOnInit(): void {
  }
}

感受constructor相似于python的__new__() 而ngOnInit()相似于一般使用的__init__(self)

 看介绍:

只要咱们实现了 Angular 的 ngOnInit 生命周期钩子,Angular 就会主动调用这个钩子。 Angular提供了一些接口,用来介入组件生命周期的几个关键时间点:刚建立时、每次变化时,以及最终被销毁时

 又有点像unity3d的Monobehaviour的 Awake() Start() Update()...由引擎在特定时机调用调用。

 

 

命名

 

注意命名方法 HeroDetailComponent 大概相似于把蓝图对象命名为HeroDetailBlueprint

文件名和组件名遵循风格指南中的标准方式。

  • 组件的类名应该是大驼峰形式,而且以Component结尾。 所以英雄详情组件的类名是HeroDetailComponent

    The component class name should be written in upper camel case and end in the word "Component". The hero detail component class is HeroDetailComponent.

  • 组件的文件名应该是小写中线形式,每一个单词之间用中线分隔,而且以.component.ts结尾。 所以HeroDetailComponent类应该放在hero-detail.component.ts文件中。

 

引用Componet视图

父组件AppComponent会告诉子组件HeroDetailComponent要显示哪一个英雄, 告诉的方法是把它的selectedHero属性绑定到HeroDetailComponenthero属性上。

这种绑定(协调主视图AppComponent与HeroDetailComponent的方式)是这样的:

<hero-detail [hero]="selectedHero"></hero-detail>

 

在等号的左边,是方括号围绕的hero属性,这表示它是属性绑定表达式的目标。 咱们要绑定到的目标属性必须是一个输入属性,不然Angular会拒绝绑定,并抛出一个错误。

 

1 把AppComponentselectedHero属性绑定到HeroDetailComponent的input 属性hero上。表示了传输关系

2 <hero-detail>是HeroDetailComponent 的 @Component里的selector。

 

这样的好处是,不用在AppComponent里显示引用HeroDetailComponent。只要[hero]是declaration过的某个Component里的input属性就OK了。

 

——这个比较别扭但又很巧妙。

相关文章
相关标签/搜索