简体   繁体   English

Angular 和 Vue 模板和绑定

[英]Angular and Vue Templates and Bindings

I a new developer.我是一个新的开发人员。 I have been working to get up to speed.我一直在努力加快速度。 Part of that has made me review Angular, React, and Vue.部分原因让我回顾了 Angular、React 和 Vue。 Each of these seem to use something called "declarative binding" and "templating".这些似乎都使用了称为“声明性绑定”和“模板”的东西。 I believe I understand what these are.我相信我明白这些是什么 However, I do not understand why I need them.但是,我不明白为什么我需要它们。 My question is:我的问题是:

  1. Why should I care about declarative binding?为什么我应该关心声明式绑定?
  2. Why should I care about templating?我为什么要关心模板?

They basically seem like two ways to generate HTML.它们基本上看起来像是生成 HTML 的两种方法。 For that reason, I feel like I'm missing something.出于这个原因,我觉得我错过了一些东西。 Can someone help me understand this?有人可以帮助我理解这一点吗?

Thanks谢谢

HTML precedes Javascript. HTML 在 Javascript 之前。 So now HTML is exposed to Javascript as a tree of objects that JS code can read and write to.所以现在 HTML 作为对象树暴露给 Javascript,JS 代码可以读取和写入。

That's why the old way to give life to a web site (JQuery era) consists in running a layer of JS code on top of a preexisting HTML page.这就是为网站赋予生命的旧方法(JQuery 时代)是在预先存在的 HTML 页面之上运行一层 JS 代码的原因。 The JS program finds interactive parts of pages and attach code in response to user events. JS 程序查找页面的交互部分并附加代码以响应用户事件。 JS code also finds movable parts of the page so to complete or update them. JS 代码还会找到页面的可移动部分,以便完成或更新它们。

But when JS application are written this way, they are difficult to maintain and improve.但是这样编写JS应用程序,就很难维护和改进。 As soon as you want to change things in HTML (moving a button from a page to another, turning a list into a table, adding another display of some numerical value, etc), the JS code in charge of powering the HTML gets outdated, often broken.一旦你想改变 HTML 中的东西(将一个按钮从一个页面移动到另一个页面,将一个列表变成一个表格,添加另一个数值的显示等),负责支持 HTML 的 JS 代码就会过时,经常坏。 Plus, once a page turns wrong, there is no easy way to track back which JS code is responsible of what's on screen.另外,一旦页面出错,就没有简单的方法来追踪哪些 JS 代码负责屏幕上的内容。

If you think twice about it, you want to invert the relationship between JS and HTML when developing complex applications.如果你三思而后行,你想在开发复杂的应用程序时颠倒 JS 和 HTML 之间的关系。 That's the purpose of templating/binding middleware.这就是模板/绑定中间件的目的。 They invert the relationship between JS and HTML so that JS Objects become now the basis layer on which you build the User Interface.它们颠倒了 JS 和 HTML 之间的关系,因此 JS 对象现在成为您构建用户界面的基础层。

A framework like Angular or a library like Knockout, enriches HTML with directives so that now HTML page works like real programs running on top of Javascript.像 Angular 这样的框架或像 Knockout 这样的库,用指令丰富了 HTML,这样现在 HTML 页面就像运行在 Javascript 之上的真实程序一样工作。 The Javascript layer only powers the application logic, and doesn't depend anymore on how the user interface is made. Javascript 层仅支持应用程序逻辑,不再依赖于用户界面的制作方式。 It won't break when you move a button, replace something by another, split a page in two, and so on.当您移动一个按钮、用另一个替换某些内容、将页面一分为二等时,它不会中断。

Templating is the first step of this approach.模板化是这种方法的第一步。 It consists in putting directly names of JS variables into Html.它包括将 JS 变量的名称直接放入 Html。 The template engine will substitute these variables with their values when the template is executed.模板引擎会在执行模板时用它们的值替换这些变量。

But templating is not enough.但是模板是不够的。 You still have to manually code the execution of the template when any displayed variable has changed.当任何显示的变量发生变化时,您仍然必须手动编码模板的执行。 This is the worst part a the programmer job, tracking every possible events that can change a page component and manually adding binding to these events so to update components.这是程序员工作中最糟糕的部分,跟踪可以更改页面组件的每个可能的事件,并手动为这些事件添加绑定以更新组件。

A truely bidirectional data-binding middleware does much more than a template engine.一个真正的双向数据绑定中间件不仅仅是一个模板引擎。 It works like a template engine at first, but it also tracks all substituted variables/expressions and will update the HTML document autoMAGICally as soon as the values change.它起初像模板引擎一样工作,但它也跟踪所有替换的变量/表达式,并在值更改时自动更新 HTML 文档。 It can also "magically" apply changes in JS variables when the user edits form fields and the like.当用户编辑表单字段等时,它还可以“神奇地”应用 JS 变量中的更改。

It's exactly like when you write a number in a Excel spreadsheet and a formula nearby is automatically updated.这就像您在 Excel 电子表格中写入数字并且附近的公式会自动更新一样。 An excel formula is a form of "declarative binding". excel 公式是“声明式绑定”的一种形式。 You don't need to write codes to make the formula result being updated, Excel makes the tedious work for you.您无需编写代码来更新公式结果,Excel 为您完成繁琐的工作。

The reason this new way of coding is a true progress is that in Real Life(tm), the View Layer of an evolving application changes 10 times more than its logic layer (thinks about your bank web site).这种新的编码方式是真正进步的原因在于,在 Real Life(tm) 中,不断发展的应用程序的视图层比其逻辑层(想想您的银行网站)的变化多 10 倍。 So being able to revamp a page layout without breaking everything in your JS logic is a web developer bliss.因此,能够在不破坏 JS 逻辑中的所有内容的情况下修改页面布局是 Web 开发人员的福气。

Data bindings and templates are the glue that join your html and your javascript.数据绑定和模板是连接 html 和 javascript 的粘合剂。

If you don't use one of those frameworks you would need to code a lot of logic to keep in sync DOM and app state (javascript), for example update text on a input element, create a list ( <ul> </ul> ) that display data of an array, handle button's click...如果您不使用这些框架之一,则需要编写大量逻辑以保持 DOM 和应用程序状态 (javascript) 同步,例如更新输入元素上的文本、创建列表 ( <ul> </ul> ) 显示数组的数据,处理按钮的点击...

Besides that, frameworks that you mentioned usually offers other features like routing, animations, resource handling.除此之外,您提到的框架通常提供其他功能,例如路由、动画、资源处理。

If you are new developer you should read about MVVM pattern and probably start for a easier framework like knockout .如果您是新开发人员,您应该阅读 MVVM 模式,并可能开始使用更简单的框架,如Knockout Knockout's tutorial is quite simple and comprehensible Knockout的教程非常简单易懂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM