如何在Angular 2中解释MVC模式? [英] How MVC pattern can be explained in Angular 2?

查看:165
本文介绍了如何在Angular 2中解释MVC模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular:MVC实施找到了有用的博客 ,但仍在为 Angular 2

推荐答案

常规

我认为该模式实际上与语言无关.也就是说,设计/体系结构模式是相当抽象的,并且以这种更抽象的表示形式以不同的语言实现它们.这并不是说我们不能在给出诸如C#或Java之类的示例语言或诸如Angular2之类的示例框架的情况下使其更加具体.

General

I think that the pattern is really quite language agnostic. That is to say, the design/architecture patterns are rather abstract, and implementing them in different languages follows this more abstract presentation. That is not to say we can not make it more concrete given an example language, such as C# or Java, or an example framework such as Angular2.

鉴于您已经阅读了有关MVC模式的一些资料,我假设您已经了解了该模式在其抽象形式中的外观.它是模型,视图和控制器的分离.我将不进一步探讨这一抽象表示.让我们看一下Angular2.

Given that you already read some source on the MVC pattern, I am assuming that you have an understanding of how the pattern looks like in it's abstract form. It's a separation of Model, View and Controller. I will not dive further into the abstract presentation of this. Let's just take a look at Angular2.

假设您正在使用 angular2-cli .创建新组件时,将为您生成一堆文件.为了理解MVC,重要的是那些以component.htmlcomponent.ts结尾的代码.它们分别是视图和控制器. HTML是呈现给用户的内容(以及一些CSS用于布局).很容易看出这是如何表示视图的.在它旁边,我们有随附的component.ts文件.这是控制器.本质上,它可以选择使用各种形式的绑定将哪些数据推送到我们的视图(.html).

Assuming that you are using angular2-cli. When you create a new component, a bunch of files are generated for you. The important ones are the ones ending on component.html and component.ts for understanding MVC. These are the view and controller respectively. The HTML, is what is presented to the user (along with some CSS for layout). It is easy to see how this represents the view. Next to it, we have the accompanying component.ts file. This is the controller. Essentially, it can choose which data to push to our view (.html) with the various forms of binding.

如果不使用angular2-cli,则可以将它们合并到一个文件中. HTML部分是我们的视图,Typescript是我们的控制器.

最后,我们有了模型.在angular2中,模型将主要是我们的services,我们可以通过我们的控制器进行访问. (旁注:服务可以视为另一种模式,单一模式.旁注的旁注:在大多数情况下是正确的,但请寻求SO解决方法))

Lastly, we have the model. In angular2, the model will mostly be our services, which we can access through our controller. (sidenote: the services can be seen as another pattern, singleton pattern. Sidenote to the sidenote: this is true in most cases but look on SO for some workaround))

尽管我们的模型可以扩展到更远的范围.我们的后端"可以有更多的类,例如,我们的服务可以使用这些类来处理或存储信息.这也可以属于我们的模型.我们可以为Person创建一个类文件.然后,PersonService可以管理Person的数组.我们可以说Person仍然是模型类.

Though our model can extend beyond that. Our 'backend' can have more classes, that our services use to process or store information for example. This can belong to our model as well. We can have a class file for Person. A PersonService can then manage an array of Person. We could say that Person is still a model class.

这篇关于如何在Angular 2中解释MVC模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆