1. 概述
总的来说一句话,如果你是老大,你可以选择用什么;如果你不是,那么,老大让用什么,你就用什么。
前端框架Angular,Vue,React三选一,没有第四种。 (当然,如果只是一两个简单的页面,纯JS、jquery足够了)。
国内的话,React和vue更流行一点,国外的话React和Angular更流行。
Vue 中文资料更多一些
Angular有些资源访问受限或者较慢(防火墙问题)
2. 框架比较
网上比较的文章太多了,如果想了解,baidu、google一下吧,这个就不再多说了。
3. 为什么国内Angular不是很流行
只说些个人看法,更多原因请自行搜索。
Angular有些资源访问受限或者较慢(防火墙问题)
上手难度稍大,或者说需要更多的时间培训才能开发。
Angular对anuglarJS(1.0版本)不支持(虽然后期推出了兼容方案), 伤了很多人的心。不过这个难免,一个组件、框架的升级,如果一直背负的早期债务,很难有更好的提升。
TypeScript。Angular是第一个吃螃蟹的,完全抛弃了js,转入Typscript,得罪的很多js的铁杆粉丝,以及惧怕ts的人。不过现在看来,typescript才是前端的趋势。vue3也声称用typescript重写了,并打算更完美的支持typescript。
大而全的框架。选择Angular的话,它自带了很多东西,路由、表单、动画、http请求等等,需要更多的时间去学习和理解。而其他框架,很多组件都是有备选方案,或者第三方提供的(如vue使用axios处理http请求),让人觉得了解框架更简单。
版本升级较快。每半年一个主版本,优缺点共存吧。
恶性循环:用的人少 》 聊的人少 》 资源少 》 用的人更少。
vue更符合国人的习惯,考虑了一些小的常用细节。比如自带支持避免一个按钮点击多次(修饰符once,@click.prevent.once)。
来自 https://www.cnblogs.com/skywind/p/14251778.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: VSCode
1. Angular (Angular2)v.s. AngularJS
Angular最早期的版本,也叫AnugularJS,使用javascript开发;新的版本,才叫Angular,也称为Angular2,使用typescript开发,Angular和AngularJS是不兼容的(当然,官方也有2个版本的集成方案)。
2. 检查本地环境的版本
检查npm, node 版本:
3. TypeScript
Angular要求开发语言必须为TypeScript。TypeScript有很多好处,比如引入了类、接口的概念,比如强类型,可以在写代码时(编译时)就能检查到一些错误,避免只有代码在执行时才发现异常。
如果没用过TypeScript,不要害怕TypeScript这个新的概念,只要你有javascript经验,直接入手,没有任何难度。只需要用到class、interface等东西时,参考一下别人如何使用,或者查一下typescript文档即可。
4. Angular CLI
angular-cli又称 Angular脚手架,是angular开发团队自行维护的一个开发工具,用于快速生成项目或者组件的框架以提高效率。可以方便的生成angular app、component、service 等等, 并且可以通过参数,按照自己的需求去创建。可以说是angular开发必不可少的利器。
参考:https://cli.angular.io/
ng generate: 新建component、service、pipe, class 等
ng new: 新建angular app
ng update: 升级angular自身,以及依赖
ng version: 显示anuglar cli全局版本、以及本地的angular cli、angular code等的版本
ng add: 新增第三方库。会做2件事,1)基于npm安装node_modules, 2)自动更改配置文件,保证新的依赖正常工作
总结一下,Angular的开放,离不开AngularCLI。
5. Angular 的版本
不同于Angular2+Angular的不兼容, Angular 2.0 之后会保证向下兼容。官方的版本发布计划是:
每 6 个月发布一个主版本(第一位版本号,主版本)
每个主版本发布 1 ~ 3 个小版本(第二位版本号,Feature 版本号)
每周发布一个补丁版本(第三位版本号,Hotfix 版本号)
6. Angular 版本如何升级
Angular CLI提供了升级命令(ng update),同时,官网(https://update.angular.io/)也有升级指南。选择从哪个版本升级到哪个版本后,会给出一步一步的升级命令,直接执行就好。
7. 依赖注入(dependency injection)
依赖注入是Angular实现的一种应用程序设计模式, 是Angular的核心概念之一。
依赖就是具有一系列功能的服务(service), 应用程序中的各种组件和指令(derictives)可能需要服务的功能。 Angular提供了一种平滑的机制,通过它我们可以将这些依赖项注入我们的组件和指令中。因此,我们只是在构建依赖关系,这些依赖关系可以在应用程序的所有组件之间注入。
使用依赖注入还有以下好处,
不需要实例化,(new 实例)。不需要关心class的构造函数里需要什么参数
一次注入(app module通过Providers注入),所有组件都可以使用。而且是用同一个service实例(Singleton),也就是说一个service里的数据是共分享的,可以用于组件间数据传递。
8. 编译-AOT和JIT的区别
每个Angular应用程序都包含浏览器无法理解的组件和模板。 因此,在浏览器内部运行之前,需要先编译所有Angular应用程序。
Angular提供两种编译类型:
JIT(Just-in-Time) compilation
AOT(Ahead-of-Time) compilation
区别在于,在JIT编译中,应用程序在运行时在浏览器内部进行编译;而在AOT编译中,应用程序在构建期间进行编译。
显而易见,AOT编译好处多多,因而是Angular的默认编译方式。主要优点
由于应用程序是在浏览器内部运行之前进行编译的,因此浏览器会加载可执行代码并立即呈现应用程序,从而加快了呈现速度。
在AOT编译中,编译器将与应用程序一起发送外部HTML和CSS文件,从而消除了对那些源文件的单独AJAX请求,从而减少了ajax请求。
开发人员可以在构建阶段检测并处理错误,这有助于最大程度地减少错误。
AOT编译器将HTML和模板添加到JS文件中,然后再在浏览器中运行。 因此,没有多余的HTML文件可读取,从而为应用程序提供了更好的安全性。
本文由博客群发一文多发等运营工具平台 OpenWrite 发布
来自 https://www.cnblogs.com/skywind/p/14251463.html
(Based on Angular 10)
0. Angular-CLI
Angular CLI is a command-line interface tool that you use to initialize, develop, scaffold, and maintain Angular applications directly from a command shell.
It is the MUST have tool to write Angular code, so make sure you are familiar with its commands.
1. IDE
1.1 Visual Studio Code
Visual Studio Code
: Best and free IDE for web developing.
Plugins:
Angular Language Service
- This extension provides a rich editing experience for Angular templates, both inline and external templates.
This extension is brought to you by members of the Angular team. It is fantastic at helping write solid code in the html templates.
Prettier
- VS Code plugin for prettier/prettier, which formats code consistently. Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
Code Spell Checker
- A basic spell checker that works well with camelCase code.
The goal of this spell checker is to help catch common spelling errors while keeping the number of false positives low.
GitLens
- If you use git, this is quite helpful. GitLens supercharges the Git capabilities built into Visual Studio Code. It helps you to visualize code authorship at a glance via Git blame annotations and code lens, seamlessly navigate and explore Git repositories, gain valuable insights via powerful comparison commands, and so much more.
1.2 Alternatives
Atom by GitHub
Sublime Text 3
PyCharm
IntelliJ IDEA
Webstorm
There are may alternatives, you can choose which you are familiar with.
*
2. UI Framework
2.1 Angular Material
Angular Material is Material Design components for Angular.
There are many alternatives. But Angular Material is the official one from Angular, and Angular also give detail upgrade solution on Angular plus Angular Material in https://update.angular.io/.
For future upgrade reason, this is your best choice.
3. State Management - NgRx
@ngrx/store is RxJS powered global state management for Angular applications, inspired by Redux. Store is a controlled state container designed to help write performant, consistent applications on top of Angular.
NgRx Store is mainly for managing global state across an entire application.
4. Code Reuse
4.1 Angular libraries
Angular libraries should what you need.
Many applications need to solve the same general problems, such as presenting a unified user interface, presenting data, and allowing data entry. Developers can create general solutions for particular domains that can be adapted for re-use in different apps. Such a solution can be built as Angular libraries and these libraries can be published and shared as npm packages.
An Angular library is an Angular project that differs from an app in that it cannot run on its own. A library must be imported and used in an app.
You can easily reuse your Angular library or also submit your library into npm server.
4.2 Alternatives - Angular custom elements
Note that libraries are intended to be used by Angular apps. To add Angular functionality to non-Angular web apps, you can use Angular custom elements.
Angular elements are Angular components packaged as custom elements (also called Web Components), a web standard for defining new HTML elements in a framework-agnostic way.
来自 https://www.cnblogs.com/skywind/p/14256698.html
了解了一些Angular的基本概念后,如果想进一步学习Angular,接下来让我们来搭建本地开发环境,并从一个入门项目了解Angular的基本用法。
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: VSCode
1. 本地开发环境搭建
本地开发环境搭建只需要node.js, 和Angular CLI。
1.1. node.js
官网(https://nodejs.org/)下载最新的LTS(Long Time Support)版本的node.js,安装。
说明:
LTS(Long Time Support)版本, 官方会支持更长时间,比如打补丁,改bug等。相对更稳定、靠谱。
node.js 安装后,同时会安装npm
检查本地node.js, npm环境
node -v
npm -v
1.2. Angular CLI
angular-cli又称 Angular脚手架,是angular开发团队自行维护的一个开发工具,用于快速生成项目或者组件的框架以提高效率。可以方便的生成angular app、component、service 等等, 并且可以通过参数,按照自己的需求去创建。可以说是angular开发必不可少的利器。(参考:https://cli.angular.io/)
npm安装最新版本@angular/cli
npm install -g @angular/cli
检查本地angular环境
ng v
说明:
该命令如果在非angular项目下执行,返回全局的Angular CLI环境版本
在angular项目下执行, 返回当前angular项目使用的angular,angular CLI,以及核心angular组件的版本。
全局Angular CLI版本有何能与项目的Angular CLI版本不一致,不冲突。项目中,使用项目制定的CLI版本。
2. 开发工具 - Visual Studio Code
推荐使用,Visual Studio Code (VSCode),微软开发的,可以说是现今为止最好的免费的Angular开发工具。并且有很多非常好用的插件。
推荐插件:
Angular Language Service: Angular语法自动提示, Angular开发必备。 This extension provides a rich editing experience for Angular templates, both inline and external templates.
This extension is brought to you by members of the Angular team. It is fantastic at helping write solid code in the html templates.
Prettier - 代码自动格式化插件。VS Code plugin for prettier/prettier, which formats code consistently. Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
Code Spell Checker - 语法检查插件. 注释可以写中文,变量名不行吧,如果拼写不对不好看吧。所以推荐把这个语法检查插件装上。
GitLens - GIT 辅助插件。If you use git, this is quite helpful. GitLens supercharges the Git capabilities built into Visual Studio Code. It helps you to visualize code authorship at a glance via Git blame annotations and code lens, seamlessly navigate and explore Git repositories, gain valuable insights via powerful comparison commands, and so much more.
Markdown All in One: 如果用Markdown写东西,这个东西一定要有,增加了对MD文件的很多支持,比如生成目录(TOC), 目录编号等。
第一个Anuglar项目
创建第一个anuglar项目
使用Anuglar CLI可以很轻松的创建angular项目。使用的Angular版本与当前环境的Anuglar CLI一致。
ng new my-ngular-app
cd my-ngular-app
ng serve
说明
ng
是angular CLI的简称
ng serve
: 启动angular项目。
Angular CLI常用命令
ng serve
: 启动angular项目。默认情况下,angular CLI检测代码改动,如果文件改动,自动编译更改部分代码,然后重新加载(reload)页面。
ng build
: 编译代码,默认输出到根目录下的dist
目录。
ng test
: 执行单元测试(Unit Test)
在线实战项目
Angular官方提供了2个新手入门项目,并且都是基于StackBlitz(针对 Web 开发者的在线 IDE),可以不使用本地环境,直接基于Web学习和练习Angular。
新手项目:Basic Angular app
入门项目:Tour of Heroes
来自 https://www.cnblogs.com/skywind/p/14260839.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: VSCode
1. 第三方UI库的选择
Angular开发,除非你只有简单一两个页面,否则引入第三方UI库就是必不可少的。而具体用哪个库,就需要考虑多方面的东西了。比如:
流行程度
版本更新情况 (是否能跟进anuglar更新,bug是否及时修复)
UI风格,是否适用于项目
入手难易程度
文档完善程度
市面上有很多Angular可用的类库,可用根据项目、企业情况去选择。简单列一下可选的UI库。
Angular Material
: angular 官方UI库。https://material.angular.io/
clarity
: 国外的一套比较流行的框架, https://clarity.design/。
NG-ZORRO
: 阿里Ant Design的Angular版本。 https://ng.ant.design/docs/introduce/en
Element Angular
:国内非常流行的基于Vue的Element,同样有Angular版本。 https://element-angular.faas.ele.me/
Kendo UI
。很多很强大,只是要收费。https://www.telerik.com/kendo-angular-ui
2. Angular Material
2.1. 优缺点
2.1.1. 优点
官方UI组件
最标准的Material实现
紧跟Angular核心组件的更新进度
官方支持与Angular的同步升级
2.1.2. 缺点
组件不像其他框架那么多,但是基本够用
风格不是特别像国内的框架
2.2. 引入到项目
根目录下执行下面命令:
ng add @angular/material
import 到页面所属的module,或者是app.module.ts中。
import { MatSliderModule } from '@angular/material/slider';
…
@NgModule ({....
imports: [...,
MatSliderModule,
…]
})
3. 总结
第三方组件各有优缺点
根据公司、项目的需求去选择
Angular Material升级更容易,官方支持。
来自 https://www.cnblogs.com/skywind/p/14262449.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 摘要
近些年Web技术飞速发展,新的类库、框架如雨后春笋般不断涌现,而每个类库也不断的更新、升级,甚至是不再兼容的升级。笔者之前维护的一个6年项目,是用jQueryMobile开发的,估计入门web开发比较短的同学都没有听说过吧。另外,就拿Angular来说,从2.0开始,使用ts开发,不再兼容1.0 版本。从2.0开始,命名为Angular,1.0的叫angularJS, 2个版本彻底切割。而这也是angular流失很多用户的其中一个原因。
针对这些问题,Angular专门提供了版本升级的指导方案,非常详细、准确(Angular每半年发布一个主版本,这个也是必须的)。
https://update.angular.io/ 是官方的angular升级指导页面,你可以选择你的项目的版本,以及要升级到的版本;同时可以选择项目使用的技术,比如是否使用了Angular Material (这也是之前推荐这个UI框架的原因--升级方便),是否和AngualrJS同时使用等等。之后,自动提示升级步骤。
升级主要通过 ng update xxx
来实现, ng update
命令不同于npm命令,npm update
相当于 npm+更改配置(代码),也就是说,再更新完node-modules之后,ng update
会自动更新配置文件,甚至是代码中的基本的import的代码,实现自动升级、更新。
举例,10.2升级到11.1,如果没有涉及到复杂的anuglar技术,大致的升级步骤是:
Run ng update @angular/core @angular/cli
which should bring you to version 11 of Angular.
Run ng update @angular/material
.
Angular now requires TypeScript 4.0. ng update will migrate you automatically.
Support for IE9, IE10, and IE mobile has been removed. This was announced * in the v10 update.
3. 总结
Angular虽然更新、升级比较频繁(从某种意义上也是优点),但是升级部分确实做得最完善的
UI框架,@angular/material可以实现和Angular的同步升级,其他第三方框架,都会有演示,时间不等
个人不推荐使用最新的版本,不做小白鼠。可以使用之前发布的一个版本,比如现在是v11,那么我们就使用v10。更稳定,同时网上资料也多。
及时更新angular版本,免得技术债欠的越来越多而导致无法升级。
这个升级指导也是相对的,如果你对Angular有些特殊的用法,或者没有安装官方推荐方式使用,升级也是比较麻烦的。
升级时,如果跨多版本,建议一个版本一个版本的升级,每升级一次,执行UT或者把站点跑起来试试,以防出错。
---------------- END ----------------
======================
« 上一篇:
Angular入门到精通系列教程(5)- 第三方UI库(Angular Material)
» 下一篇: Angular入门到精通系列教程(7)- 组件(@Component)基本知识
来自 https://www.cnblogs.com/skywind/p/14262974.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 概述
组件是 Angular 应用的主要构造块。每个组件包括如下部分:
Component可以是一个页面,也可以是一个组件(控件)。总是,是一个页面元素。
任何一个Component都是NgModule的一部分,这样它就可以被其他应用和其他Component所调用。为了定义Component是NgModule的一个成员之一,开发者应该在NgModule的declarations属性中,将自己开发的Component列出。
另外,通过Component修饰符(也就是@Component)开发者可以配置元数据,这样通过各式各样的Life-Cycle hooks,Components就可以控制他们的运行环境。
2. 创建Component
基于AngularCLI,可以很方便的创建Component。在要创建Component的目录下,执行如下命令
ng generate component <component-name>
e.g. ng generate component MyComponent
AngularCLI会自动生成一个文件夹和4个文件:
一个以该组件命名的文件夹(e.g my-component)
一个组件文件 < component-name >.component.ts(e.g my-component.component.ts)
一个模板文件 < component-name >.component.html(e.g my-component.component.html)
一个 CSS 文件, < component-name >.component.css(e.g my-component.component.css)
测试文件 < component-name >.component.spec.ts(e.g my-component.component.spec.ts)
对于Component,所有文件名都会自动增加Component后缀,所以不建议< component-name > 中带有‘component’这个单词。
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
以上是核心的ts文件,指定了selector(CSS 选择器),template(html)文件,css文件。html/css文件如果需要可以多个component公用。尤其是css,可以看到参数是Array,所以可以制定多个css。
2.1. 组件模板
组件模板,即HTML部分,可以是一个html文件,也可以是一段html描述,是等价的。Angular 组件需要一个用 template 或 templateUrl 定义的模板。但你不能在组件中同时拥有这两个语句。
html 文件方式
@Component({
selector: 'app-component-overview',
templateUrl: './component-overview.component.html',
})
html代码方式
@Component({
selector: 'app-component-overview',
template: '<h1>Hello World!</h1>',
})
3. 视图封装模式
在 Angular 中,组件的 CSS 样式被封装进了自己的视图中,而不希望影响到应用程序的其它部分。这部分也是可以通过配置去进行控制的。
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
encapsulation: ViewEncapsulation.None,
styleUrls: ['./my-component.component.css']
})
可以看到,增加了一个encapsulation
属性 (视图封装模式)。通过在组件的元数据上设置视图封装模式,你可以分别控制每个组件的封装模式。 可选的封装模式一共有如下几种:
Emulated 模式(默认值)通过预处理(并改名)CSS 代码来模拟 Shadow DOM 的行为,以达到把 CSS 样式局限在组件视图中的目的。 更多信息,见附录 1。(说明:只进不出,全局样式能进来,组件样式出不去)
ShadowDom 模式使用浏览器原生的 Shadow DOM 实现来为组件的宿主元素附加一个 Shadow DOM。组件的视图被附加到这个 Shadow DOM 中,组件的样式也被包含在这个 Shadow DOM 中。(说明:不进不出,没有样式能进来,组件样式出不去。)
None 意味着 Angular 不使用视图封装。 Angular 会把 CSS 添加到全局样式中。而不会应用上前面讨论过的那些作用域规则、隔离和保护等。 从本质上来说,这跟把组件的样式直接放进 HTML 是一样的。
3.1. 特殊的选择器 :host
使用 :host 伪类选择器,用来选择组件宿主元素中的元素(相对于组件模板内部的元素)。 :host 选择是是把宿主元素作为目标的唯一方式。除此之外,你将没办法指定它, 因为宿主不是组件自身模板的一部分,而是父组件模板的一部分。
e.g.
:host {
}
3.2. inline-styles
默认AngularCLI生成的component,css在一个单独文件中。当然,同html模板类似,如果需要,你也可以制定css样式写在ts中, 不单独放到一个文件中。命令:ng generate component MyComponent --inline-style
。
生成component如下
@Component({
selector: 'app-my-component',
template: '<h1>Hello World!</h1>',
styles: ['h1 { font-weight: normal; }']
})
4. 总结
Angular CLI辅助创建一个component所需的多个文件
建议html/css/ts分开
在期望目录下执行Angular CLI命令,可以生成到制定目录
ng generate component XXX
可以简写为 ng g c
---------------- END ----------------
======================
来自 https://www.cnblogs.com/skywind/p/14266772.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 摘要
组件之间传递数据,最主要的就是父子组件之间传递数据, 例如:
<parent-component>
<child-component></child-component>
</parent-component>
父组件传入数据给子组件,同时,子组件数据发生变化是,希望能够通知父组件。
Angular 中,@Input() 和 @Output() 为子组件提供了一种与其父组件通信的方法。 @Input() 允许父组件更新子组件中的数据。相反,@Output() 允许子组件向父组件发送数据。
子组件中的 @Input() 装饰器表示该属性可以从其父组件中获取值。
例如:
export class ChildComponent {
@Input() message: string;
}
增加@Input() 装饰器的变量,除了数据可以从父组件传入后,其他逻辑和普通变量一致;
子组件的html代码中,既可使用message这个变量, 例如:
<p>
Parent says: {{message}}
</p>
2.2. 父组件传递变量给子组件
当父组件调用子组件时,可以把父组件的变量(如messageToChild
) 传递给子组件
<child-component [message]="messageToChild"></child-component>
子组件中,可以更改message
这个传入的变量,但是其作用域只在子组件中,父组件拿不到更改后的结果。(如何传给父组件,请接着看)
3. 子传父 @Output()
Angular通过事件(Event)来实现子组件通知父组件数据的改变,父组件需要订阅该事件。
3.1. 子组件定义@Output
子组件定义@Output
export class ChildComponent {
@Output() newItemEvent = new EventEmitter<string>();
addNewItem(value: string) {
this.newItemEvent.emit(value);
}
}
子组件当数据发生变化时,调用这个addNewItem
方法既可。例如,html中
<label>Add an item: <input #newItem></label>
<button (click)="addNewItem(newItem.value)">Add to parent's list</button>
3.2. 父组件订阅事件
父组件的ts代码中,增加一个处理上面事件的方法,例如
addItem(newItem: string) {
}
父组件的html中,订阅该事件。
<child-component (newItemEvent)="addItem($event)"></child-component>
事件绑定 (newItemEvent)='addItem($event)'
会把子组件中的 newItemEvent 事件连接到父组件的 addItem() 方法。
4. 总结
使用@Input() 和 @Output() 可以很方便的实现父子组件之间的数据传递、共享。
可以同时使用 @Input() 和 @Output()
---------------- END ----------------
======================
来自 https://www.cnblogs.com/skywind/p/14267182.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 摘要
当 Angular 实例化组件类并渲染组件视图及其子视图时,组件实例的生命周期就开始了。生命周期一直伴随着变更检测,Angular 会检查数据绑定属性何时发生变化,并按需更新视图和组件实例。当 Angular 销毁组件实例并从 DOM 中移除它渲染的模板时,生命周期就结束了。当 Angular 在执行过程中创建、更新和销毁实例时,指令就有了类似的生命周期。
你的应用可以使用生命周期钩子方法来触发组件或指令生命周期中的关键事件,以初始化新实例,需要时启动变更检测,在变更检测过程中响应更新,并在删除实例之前进行清理。
2. 生命周期及顺序
ngOnChanges(): 当 Angular 设置或重新设置数据绑定的输入属性时响应。
ngOnInit(): 在 Angular 第一次显示数据绑定和设置指令/组件的输入属性之后,初始化指令/组件。
ngDoCheck(): 每次执行变更检测时的 ngOnChanges() 和 首次执行变更检测时的 ngOnInit() 后调用。
ngAfterContentInit(): 当 Angular 把外部内容投影进组件视图或指令所在的视图之后调用。
ngAfterContentChecked(): ngAfterContentInit() 和每次 ngDoCheck() 之后调用
ngAfterViewInit(): 当 Angular 初始化完组件视图及其子视图或包含该指令的视图之后调用。
ngAfterViewChecked(): ngAfterViewInit() 和每次 ngAfterContentChecked() 之后调用。
ngOnDestroy(): 每当 Angular 每次销毁指令/组件之前调用,清理释放资源。
3. 响应生命周期事件
我们以通过实现一个或多个 Angular中定义的生命周期钩子接口来响应组件或指令生命周期中的事件。每个接口都有唯一的一个钩子方法,它们的名字是由接口名再加上 ng 前缀构成的。例如:
@Component()
export class DemoComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
说明:
1) 通过生命周期钩子接口来响应生命周期中的事件,需要在类名之后,声明实现(implements) 具体的钩子接口。然后代码中定义个钩子函数才能被执行。如 ngOnInit()
对应 接口OnInit
。
2) 可以实现多个钩子接口,例如 export class DemoComponent implements OnInit, OnDestroy {
4. 主要生命周期事件
4.1. 初始化事件 ngOnInit()
使用 ngOnInit() 方法执行以下初始化任务:
逻辑稍复杂,不适合放到构造函数中的逻辑
初始化中的数据访问逻辑
处理需要根据父组件传入参数(@Input)进行初始化的逻辑
4.2. 实例销毁 ngOnDestroy()
把清理逻辑放进 ngOnDestroy() 中,这个逻辑就必然会在 Angular 销毁该指令之前运行。下列逻辑可言放到ngOnDestroy():
取消订阅可观察对象和 DOM 事件。
停止 interval 计时器。
反注册该指令在全局或应用服务中注册过的所有回调。
释放其他占有的资源。
5. 总结
使用生命周期事件钩子函数,别忘了类名后面implements
相应的接口,否则不生效;
初始化代码,区分哪些放构造函数,哪些放 ngOnInit();
可以精简的钩子事件方法来避免性能问题;
ngOnChanges()发生的非常频繁,加入复杂逻辑会影响性能;
---------------- END ----------------
======================
来自 https://www.cnblogs.com/skywind/p/14267694.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 摘要
指令(Directive)在Angular 1.0时代(当时叫AngularJS)是很流行的,现在用到的偏少。可以简单理解为,指令(Directive)用于扩展已有Element(DOM)。
2. 组件与指令之间的关系
如果去看Angular源码,可以看到下面定义
export declare interface Component extends Directive {
是的,Component派生于Directive,也就是说,Component属于Directive。
2.1. 指令的种类
Component 是 Directive 的子接口,是一种特殊的指令,Component 可以带有 HTML 模板,Directive 不能有模板。
属性型指令:用来修改 DOM 元素的外观和行为,但是不会改变DOM 结构,Angular 内置指令里面典型的属性型指令有 ngClass、ngStyle,如果打算封装自己的组件库,属性型指令是必备的内容。
结构型指令:可以修改 DOM 结构,内置的常用结构型指令有 *ngFor
、*ngIf
和 *ngSwitch
。由于结构型指令会修改 DOM 结构,因而同一个 HTML 标签上面不能同时使用多个结构型指令。如果要在同一个 HTML 元素上面使用多个结构性指令,可以考虑加一层空的元素来嵌套,比如在外面套一层空的(div
) 。
3. Angular 中指令的用途
Angualr中用指令来增强DOM的功能,包括 HTML 原生DOM和我们自己自定义的组件(Component)。举例来说,可以扩展一个Button,实现避免点击后,服务器端未响应前的二次点击;高亮某些收入内容等等。
4. 指令举例
4.1. 指令功能
实现一个指令,鼠标移动到上面时, 背景显示为黄色,鼠标移开,恢复正常。
4.2. Anuglar CLI生成基本文件
ng generate directive MyHighlight
Anuglar CLI自动生成html、css、ut等文件。
4.3. Directive指令核心代码
import { Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor(el: ElementRef) {
el.nativeElement.style.backgroundColor = 'yellow';
}
@HostListener('mouseenter') onMouseEnter() {
this.highlight('yellow');
}
@HostListener('mouseleave') onMouseLeave() {
this.highlight(null);
}
private highlight(color: string) {
this.el.nativeElement.style.backgroundColor = color;
}
}
4.4. 使用该指令
<p my-highlight>Highlight me!</p>
my-highlight
即我们的元素扩展属性(指令、directive)。
5. 总结
---------------- END ----------------
======================
来自 https://www.cnblogs.com/skywind/p/14272393.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 摘要
模块(NgModule)是Angular的核心概念之一。模块(NgModule)用于组织业务代码,按照自己的业务场景,把组件、服务、路由打包到模块里面。
模块(NgModule)的主要作用:
NgModule 组织业务代码,开发者可以利用 NgModule 把关系比较紧密的组件组织到一起。
NgModule 用来控制组件、指令、管道等的可见性,处于同一个NgModule 里面的组件默认互相可见,而对于外部的组件来说,只能看到NgModule 导出(exports)的内容。这样就实现了封装,只暴露希望暴露的组件、服务给调用者。
NgModule 是 @angular/cli 打包的最小单位。打包的时候,@angular/cli 会检查所有 @NgModule 和路由配置,如果你配置了异步模块,cli 会自动把模块切分成独立的 chunk(块)。在 Angular 里面,打包和切分的动作是 @angular/cli 自动处理的,不需要你干预。当然,如果需要,你也可以修改angular的编译配置,基于Webpack 配一个环境出来,自定义打包规则。
NgModule 是 Router 进行异步加载的最小单位,Router 能加载的最小单位是模块,而不是组件。当然,也可以一个模块里面只放一个组件是。
2. NgModule举例、说明
前文说过,Angular中任何的Component、service,都必须属于一个NgModule。所以,使用AngularCLI生成的框架程序,自动生成的组件,也是属于一个Component的,并且标记为启动模块。
这样,angular站点启动后,会以这个模块为入口,根据配置加载各个模块。
下面以默认生成的启动模块为例,进行解释:
@NgModule({
declarations: [
AppComponent,
MyComponentComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
declarations,用来放组件、指令、管道的声明;
imports,用来导入外部模块。比如当前模块需要调用其他模块的组件,需要加入到这里。比如上面例子,导入了Browser和Routing 2个模块。
providers,需要使用的第三方或者其他模块的Service 都放在这里;
bootstrap,定义启动组件。 一个可以启动的Angular项目(如果只是一个Library除外),需要定义一个启动组件。
exports, 声明的组件、指令和管道可以在导入了本模块的模块下任何组件的模板中使用。 导出的这些可声明对象就是该模块的公共 API。换句话说,其他模块想用本模块中定义的内容,需要在这里声明。
entryComponents, 如果其他模块想要动态加载本模块中的组件到视图中, 那么,这些组件需要写入entryComponents。
3. Angular CLI生成模块
AngularCLI是一个很好很强大的工具集,可以帮助我们生成很多基础代码、文件, 包括创建一个模块,并且可以制定参数。
ng generate module <name> [options]
详情参考https://angular.io/cli/generate#module。
几个简单的例子:
创建feature1模块: ng generate module feature1
创建feature2模块, 并且带路由:ng generate module feature2 --routing
创建一个延迟加载的feature3模块(延迟加载模块,参考下面章节): ng generate module feature3 --route feature3 --module app.module
说明: ng generate module xxx
可以简写为 ng g m xxx
4. 延迟加载模块
延迟加载使得应用程序在启动时不被载入,而是结合路由配置,在需要时才动态加载相应模块。这样 Angular 就不需要在第一个界面从服务器下载所有的文件,直到请求它,才下载相应的模块。这对提供性能和减少首屏的初始下载文件尺寸有巨大的帮助,而且它可以很容易设置。
举例来说,上文创建了3个模块,主程序模块以及feature1、feature2会被打成一个包(js),feature3会被单独打一个包(js),当用户访问/feature3/* 的地址后,才会加载feature3这个包(js),否则永远不会请求、加载feature3的模块,从而提高性能(首页加载时间)。当一个项目大到一定程度时,最好考虑把某些模块设置为延迟加载模块。
延迟加载的路由定义(angular CLI会自动生成):
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{
path: 'feature3',
loadChildren: () =>
import('./feature3/feature3.module').then((m) => m.Feature3Module),
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
最后复习一下生成延迟加载模块的命令ng generate module feature3 --route feature3 --module app.module
,或者简写为 ng g m feature3 --route feature3 --module app.module
。
5. 总结
Angular项目,就是模块(NgModule)的一个集合,任组件、服务等必须包含在一个模块中。
延迟加载模块用于提高首页加载性能。
Angular CLI命令,生成模块。
---------------- END ----------------
======================
https://www.cnblogs.com/skywind/p/14262449.html
来自 https://www.cnblogs.com/skywind/p/14323831.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 摘要
简单来说地址栏中,不同的地址(URL)对应不同的页面,这就是路由。同时,点击浏览器的前进和后退按钮,浏览器就会在你的浏览历史中向前或向后导航,这也是基于路由。
在 Angular 里面,Router 是一个独立的模块,定义在 @angular/router 模块中,
Router 可以配合 NgModule 进行模块的延迟加载(懒加载)、预加载操作(参考《Angular入门到精通系列教程(11)- 模块(NgModule),延迟加载模块》);
Router 会管理组件的生命周期,它会负责创建、销毁组件。
对于一个新的基于AngularCLI的项目,初始化时可以通过选项,将AppRoutingModule默认加入到app.component.ts中。
2. 路由(Router)基本用法
2.1. 准备
我们首先创建2个页面,用于说明路由的使用:
ng g c page1
ng g c page2
使用上面AnuglarCLI命令,创建Page1Component, Page2Component 2个组件。
2.2. 注册路由
const routes: Routes = [
{
path: 'page1',
component: Page1Component
},
{
path: 'page2',
component: Page2Component
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
可以看到,简单的路由注册,只需要path和component2个属性,分别定义路由的相对路径,以及这个路由的响应组件。
2.3. html中的用法
<a routerLink="page1">Page1</a> |
<a routerLink="page2">Page2</a>
在html模板中,直接使用routerLink属性,标识为angular的路由。执行代码,可以看到 Page1和Page2 两个超链接,点击可以看到地址栏地址改为http://localhost:4200/page2或http://localhost:4200/page1, 页面内容在page1和page2中切换
2.4. ts 代码中的用法
有时候,需要根据ts中的业务逻辑,进行跳转。ts中,需要注入Router实例,如
constructor(private router: Router) {}
跳转代码:
this.router.navigate(['/page1']);
this.router.navigate(['/page1', 123]);
3. 接收参数
3.1. 路径中的参数
一般来说,我们把参数作为url中的一段,如/users/1, 代表查询id是1的用户,路由定义为"/users/id" 这种风格。
针对我们的简单页面,比如我们的page1页面可以传id参数,那么我们需要修改我们的routing为:
const routes: Routes = [
{
path: 'page1/:id',
component: Page1Component,
},
{
path: 'page1',
redirectTo: 'page1/',
},
{
path: 'page2',
component: Page2Component,
},
];
ts代码读取参数时, 首先需要注入ActivatedRoute,代码如下:
constructor(private activatedRoute: ActivatedRoute) {}
ngOnInit(): void {
this.activatedRoute.paramMap.subscribe((params) => {
console.log('Parameter id: ', params.get('id'));
});
}
3.2. 参数(QueryParameter)中的参数
参数还有另外一种写法,如http://localhost:4200/?name=cat, 即URL地址后,加一个问号'?', 之后再加参数名和参数值('name=cat')。这种称为查询参数(QueryParameter)。
取这查询参数时,和之前的路由参数类似,只是paramMap改为queryParamMap,代码如下:
this.activatedRoute.queryParamMap.subscribe((params) => {
console.log('Query Parameter name: ', params.get('name'));
});
4. URL路径显示格式
不同于传统的纯静态(html)站点,angular中的url不是对应一个真实的文件(页面),因为anuglar接管的路由(Routing)处理,来决定显示那个Component给终端用户。为了针对不同的场景,angular的URL路径显示格式有2中:
http://localhost:4200/page1/123
http://localhost:4200/#/page1/123
默认是第一种,不加#的。如果需要,可以在app-routing.ts中,加入useHash: true
, 如:
@NgModule({
imports: [RouterModule.forRoot(routes, { useHash: true })],
exports: [RouterModule],
})
5. 部署中遇到的问题
同样,因为anuglar接管的路由(Routing)处理,所以部署时,部署到iis, nginx等等的服务器,都会有不同的技巧(要求),详细参考:
https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode
6. 总结
angular默认不支持可选路由(e.g. /user/:id?),但是我们可以定义2个路由,指向同一个Component来实现这个,达到代码复用;(或者使用redirectTo)
可以使用useHash参数,实现augular路径前加一个#;
读取参数时,都需要subscribe订阅一下,不能直接读取。
打包后部署问题,查看官方wifi (https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode)
---------------- END ----------------
======================
来自 https://www.cnblogs.com/skywind/p/14325163.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 摘要
在我们的实际的业务开发过程中,我们经常会遇到如下需求:
需要限制某些 URL 的可访问性,例如,对于系统管理界面,只有那些拥有管理员权限的用户才能打开。
当用户处于编辑界面时,在没有保存就离开时,需要提示用户是否放弃修改。
针对以上场景,Angualr使用路由守卫
(Route Guards)来实现。
2. 路由守卫(Route Guards)
2.1. 创建路由守卫
Angular CLI提供了命令行工具,可以快速创建路由守卫框架文件:ng generate guard auth
。 执行后,Angular CLI会问我们需要实现哪些接口,我们直接勾选即可:
? Which interfaces would you like to implement? (Press <space> to select, <a> to toggle all, <i> to invert selection)
>(*) CanActivate
( ) CanActivateChild
( ) CanDeactivate
( ) CanLoad
说明:
CanActivate: 控制路由是否可以激活
CanActivateChild: 控制子路由是否可以激活
CanDeactivate: 控制路由是否可以退出
CanLoad: 控制模块(module)是否可以被加载
比较经常使用的是1、3,分别控制进入和退出。 按照上面配置,AngularCLI自动生成如下代码,return true;
替换为我们实际的代码即可。return false;
表示不允许跳转,或者取消离开当前页面。
import { Injectable } from '@angular/core';
import { CanActivate, CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate, CanDeactivate<unknown> {
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return true;
}
}
在canActivate方法中,我们还可以使用跳转。如页面判断是否已经登录,如果没有登录,跳转到Login页面:
this.router.navigate(['/login']);
return false;
2.2. 控制路由是否可以激活
控制路由是否可以激活,需要定义在定义路由的地方,增加canActivate属性。如果需要,还可以增加data属性, 比如告诉我们的AuthGuard进入当前路由需要验证哪些权限。data属性是可选的。
const routes: Routes = [
{
path: "page1",
component: Page1Component,
data: { permissions: ['YourPage1Permission'] },
canActivate: [AuthGuard]
},
{
path: "page2",
component: Page2omponent,
data: { permissions: ['YourPage2Permission'] },
canActivate: [AuthGuard]
}
]
2.3. 控制路由是否退出(离开)
和控制路由是否可以激活类似,在路由定义出增加 canDeactivate
,并制定相应的Guard守卫即可。这里不再举例
3. 总结
通过路由守卫(Route Guards)实现控制URL的进入和离开;
Angular CLI可以辅助我们创建guard文件;
---------------- END ----------------
======================
https://www.cnblogs.com/skywind/p/14272393.html
来自 https://www.cnblogs.com/skywind/p/14326072.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 概要
当我们完成angular的开发后,如何部署到服务器呢?
2. 编译打包
2.1. 基本打包命令
基于Angular CLI生成的Angular项目,默认会有2个环境配置文件
└──myProject/src/environments/
└──environment.ts
└──environment.prod.ts
environment.ts: 针对开发环境使用的环境文件
environment.prod.ts: 生产环境编译时,将替换原有的environment.ts,然后再打包。 (根目录下的angular.json定义了这个默认行为,有需要,可以进行修改)
AngularCLI刚刚生成2个文件后,如果打开比较2个文件的区别,可以看到开发环境使用的environment.ts文件中,有这么一句production: false
。因为,针对生产环境,angular在编译时需要核心考虑效率等问题,而开发环境,要考虑方便开发者进行调试,侧重点不同。
那么针对生产环境如何编译呢?Angular CLI同样提供了命令,
ng build --prod
其中,参数--prod
即告诉编译环境,编译为生产环境包。同样,angular.json中定义了默认的编译参数,如果需要,可以进行修改。主要配置参数如下
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": false,
"budgets": [
{
"type": "initial",
"maximumWarning": "5mb",
"maximumError": "10mb"
}
]
}
}
Angular默认打包到根目录下的dist
目录下,生成的文件为纯静态文件(html, css, js),以及图片文件。
2.2. 打包部署到二级目录
有不少情况,我们的angular web站点不能直接部署到网站的根目录下,需要部署到二级目录下。 比如,不能部署到 http://abc.com下,要求部署到 http://abc.com/demo 这个二级目录下。针对这种情况,就需要修改一下我们的编译参数,修改为:
ng build --prod --deploy-url /demo/ --base-href /demo/
增加 --deploy-url
和 --base-href
。
使用场景:比如我们有多个站点,希望使用同一个反向代理, http://site1
, http://site2
, 分别映射到 http://abc.com/site1
, http://abc.com/site2/
。 那么为了方便配置,需要把site1, site2都部署到二级目录,如http://site1/site1
, http://site2/site2
。 然后 http://site1/site1
代理到http://abc.com/site1
, http://site2/site2
代理到http://abc.com/site2/
, 免得css、js因为目录级别问题找不到。
3. Angular站点的发布
Angular站点编译打包后,可以方便的发布到已有web服务器,或者打成docker image, 然后发布。
3.1. web服务器发布
因为我们打包后,生成的文件为纯静态文件(html, css, js, 图片等), 所以打包后的问题,可以直接copy到iis, nginx , apache tomcat等web服务器,或者node.js, java等可以显示静态文件的程序的目录下即可。
3.2. 使用docker发布
如果部署到docker,我们可以基于一个基础的nginx docker, 然后把编译好的angular项目,copy到docker 内的nginx目录下即可。
基本步骤:
准备Dockerfile 文件, docker可以基于nginx:alpine
, 将编译好的angular 站点文件复制到 docker 的nginx默认目录 /usr/share/nginx/html
FROM nginx:alpine
COPY . /usr/share/nginx/html
说明: 1) 假设angular打包后的文件,与Dockerfile文件在同一个目录
2) COPY . /usr/share/nginx/html, 两个参数 .
代表当前路径, /usr/share/nginx/html
是docker中的目标目录
编译docker。 在Dockerfile目录下,执行
docker build -t your-docker-name .
docker save your-docker-name > your-docker-name.tar
gzip your-docker-name.tar
三条命令分别为:
可以看到,因为angular编译后为纯静态文件,所以使用docker发布非常简单。部署时,只需要复制docker文件到目标机器,解压缩,然后执行 docker load < your-docker-name.tar
即可加载docker image到目标机器。
4. 总结
为生产环境编译,一定要加参数--prod
如果要部署到二级目录,编译时加参数。如部署到/demo二级目录下,加参数: --deploy-url /demo/ --base-href /demo/
使用docker发布,可以选择基本的nginx docker, 然后将编译好的angular文件copy到nginx目录下即可。
---------------- END ----------------
======================
来自 https://www.cnblogs.com/skywind/p/14343721.html
环境:
Angular CLI: 11.0.6
Angular: 11.0.7
Node: 12.18.3
npm : 6.14.6
IDE: Visual Studio Code
1. 概要
实际的Angular项目,肯定不是一个简单的Hello World程序,会包含很多的功能,很多的文件。那么如何更好的组织这些文件呢?官方给了一个原则,可以供参考:
https://angular.io/guide/styleguide#application-structure-and-ngmodules。
下面,我们来通过一个例子具体解释一下。
2. 目录结构(工程结构)推荐
2.1. 总的原则
基于Angular CLI创建模块(module),组件(component)等等的内容;
源代码都放到src
文件夹下;
应用的根目录创建一个 NgModule, 并命名为app.module.ts(例如 /src/app,这个Angular CLI 会自动帮我们做)
组件具有多个伴生文件 (.ts、.html、.css 和 .spec),建议为它创建一个文件夹;(Angular CLI 会自动帮我们做)
为每一组功能(特性区)创建一个模块(NgModule
);(这个也方便我们应用惰性加载/延迟加载,预加载)
在 src/app/shared 目录中创建名叫 SharedModule 的共享模块,方便其他功能调用;
2.2. 实例
src/ 目录下:
+---app
| | app-routing.module.ts
| | app.component.css
| | app.component.html
| | app.component.spec.ts
| | app.component.ts
| | app.module.ts
| |
| +---feature1
| | | ......
| |
| +---feature1
| | | ......
| |
| +---core
| | | core.module.ts
| | | ....
| |
| \---shared
| | shared.module.ts
| |
| +---components
| +---pipes
| +---services
|
+---assets
| .gitkeep
|
\---environments
environment.prod.ts
environment.ts
说明:
根目录下的NgModule
默认是 app.module.ts
, 不要改名,方便阅读
app 下,每个目录,同时也都是一个模块-NgModule
一个项目,一般包含多个功能(feature)模块
推荐使用共享模块-SharedModule,将通用的、功能的功能(service/component/pipe等)放到改模块中。 (下文详细介绍)
推荐使用核心模块-CoreModule,可以将项目的一些全局的设置、UI等放到该模块。如header、footer组件,安全组件(服务),上下文存储服务等。
2.3. 共享模块-SharedModule
上文提到,推荐使用共享模块-SharedModule,将通用的、功能的功能(service/component/pipe等)放到改模块中。具体存放内容包括:
共享模块中声明那些可能被特性模块引用的可复用组件(Component)、指令(Directive)和管道(Pipe)。
如果放置服务(Service),由于服务的单例特性,共享模块中只建议放置无状态的服务(Service),对于有状态、或者和业务紧密相关的服务,建议放到CoreModule中。
SharedModule 中声明(declarations)和导出(exports)所有组件、指令和管道,方便其他模块调用
共享模块(SharedModule)在项目中处于底层,从逻辑上,只能由其他业务逻辑模块调用,不能调用其他模块;
共享模块(SharedModule)不建议使用延迟加载(惰性加载),因为这样会破坏服务的单例特性;
3. 高级应用 - angular库、工作空间(workspace )
想一想你是如何安装angular相关的类库的?我们使用的是npm install xxx
。那么这些第三方angular库是如何开发的呢?我们是否可以把通用的内容写成Angular类库,然后方便在多个angular项目间共享了?又或者直接发布到官方npm站点?
Angular 从6.0开始,引入了工作区的概念。使用Angular CLI,默认创建的就是一个工作空间(workspace)。一个工作空间(workspace)可以有一个主项目,同时可以有多个子项目。当然这些子项目可以是angular的application, 也可以是Library。Angular的子项目,都在app
下的projects
之下,和src
目录平级。
这样,一般来说,我们的项目中,除了主程序,还可以包含多个子类库。这样在开发时方便主程序、类库同时开发调试;也可以项目结束后,单独发布类库,实现项目之间的代码共享。
创建子项目也很简单,子项目有2种类型,application(可以启动的)和Library(Angular类库),默认都在projects
文件夹下:
创建子Applicaton
ng generate application <name> [options]
创建子类库(Angular Library)
ng generate library <name> [options]
3.1. 目录结构示意
+
| \
| \
| \
| \
| \
| \
\
+
| +
| +
| +
| | +
| | \
| \
| +
| +
| \
+
\
4. 总结
Angular CLI默认创建的Angular项目,实际是一个工作空间(workspace ),在其内部还可以创建多个子项目
子项目可以是Application,也可以是类库(Library);
对于主项目(一般来说是一个Application),以功能模块的方式进行组织;
公共内容,建议放到共享模块-SharedModule中。
对于功能模块,可以通过惰性加载(延迟)加载,提高首页加载速度;同时通过预加载技术,可以在空闲时间加载这部分模块,使用户体验更好。
---------------- END ----------------
======================
来自 https://www.cnblogs.com/skywind/p/14344898.html