从flask视角理解angular(二)Blueprint VS Component
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从flask视角理解angular(二)Blueprint VS Component,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3155字,纯文字阅读大概需要5分钟。
内容图文
Component类似flask app下面的每个blueprint。
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 说明了两个路径,
1 /templates/每个blueprint下的用 jinja2 语法的XXX.html,2 /static/下的 css
也可以直接把模板内容和css直接写在@Component下面,这时Component有点类似Unity3d里的gameobject,@Component里面的“selector,template” 有点类似gameobject里的“transfrom”,"material"。(U3d里是gameobject->component 不要和ng的Component混了)。
区别在于:flask强调"动静分离"。这样部署的时候,static部分用nginx, web api部分 用 gunicorn。
angular的“前后端”统统用ts/js搞了。这样开发者不需要太在乎“动静”与“前后"的分野。封装程度类似unity3d里的prefab,感觉很不错。
定义
每次开头都要导入Component
import { Component } from ‘@angular/core‘;
类似blueprint:
from flask import Blueprint
导出
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()...由引擎在特定时机调用调用。
——不要被生命周期钩子(lifecycle hook)之类的名词吓倒。
在实干派野生程序员这里,不要过于在意这些唬人的新名词。
要敢于看它是怎么回事,然后类比到自己熟悉的概念和机制上,软件工程的世界里,没有那么多概念,范畴级的发明创造。
在python里,可以不需要知道__init__(
self
)是不是构造函数,反正当构造函数来用;
在unity3d里不需要纠结Awake() Start()该叫什么名,反正大概知道该塞什么进去就OK了。
命名
注意命名方法 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
属性绑定到HeroDetailComponent
的hero
属性上。
这种绑定(协调主视图AppComponent与HeroDetailComponent的方式)是这样的:
<hero-detail [hero]="selectedHero"></hero-detail>
在等号的左边,是方括号围绕的hero
属性,这表示它是属性绑定表达式的目标。 我们要绑定到的目标属性必须是一个输入属性,否则Angular会拒绝绑定,并抛出一个错误。
1 把
AppComponent
的selectedHero
属性绑定到HeroDetailComponent
的input 属性hero
上。表示了传输关系2 <hero-detail>是HeroDetailComponent 的 @Component里的selector。
这样的好处是,不用在AppComponent里显示引用HeroDetailComponent。只要[hero]是declaration过的某个Component里的input属性就OK了。
——这个比较别扭但又很巧妙。
原文:http://www.cnblogs.com/xuanmanstein/p/7616653.html
内容总结
以上是互联网集市为您收集整理的从flask视角理解angular(二)Blueprint VS Component全部内容,希望文章能够帮你解决从flask视角理解angular(二)Blueprint VS Component所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。