updater
- parse(`Hi {{ user.name }}, {{ colon }} is awesome.`);
- // => 'Hi ' + user.name + ', ' + colon + ' is awesome.'
在 Directive 内部我们经由过程 Object.assign(this, directives[this.name]); 来注册不合的指令,所以变量 directives 的值可能是如许的:
- // directives
- export default {
- // directive `:show`
- show: {
- beforeUpdate() {},
- update(show) {
- this.node.style.display = show ? `block` : `none`;
- },
- },
- // directive `:text`
- text: {
- beforeUpdate() {},
- update(value) {
- // ...
- },
- },
- };
- Object.assign(this, {
- beforeUpdate() {},
- update(show) {
- this.node.style.display = show ? `block` : `none`;
- },
- });
所以假设某个指令的名字是 show 的话,那么 Object.assign(this, directives[this.name]); 就等同于:
表示对于指令 show,指令更新器会改变该元素 style 的 display 值,大年夜而实现对应的功能。所以你会发明,全部编译器构造设计好后,如不雅我们要拓展功能的话,只需简单地编写指令的更新器即可,这里再以指令 text 举个例子:
- // directives
- export default {
- // directive `:show`
- // show: { ... },
- // directive `:text`
- text: {
- update(value) {
- this.node.textContent = value;
- },
- },
- };
- const compiled = Compile(`<h1 :text="'Hey 🌰, ' + greeting"></h1>`, {
- greeting: `Hello World`,
- });
- compiled.view // => `<h1>Hey 🌰, Hello World</h1>`
推荐阅读
曾经和格力电器董事长董明珠有10亿赌约的小米,要做空调了? 8月10日,小米智能硬件生态链企业智米科技宣布了一款售价4399元的全直流变频空调,但品牌不是“小米&rdqu>>>详细阅读
本文标题:如何实现一个基于DOM的模板引擎
地址:http://www.17bianji.com/lsqh/36664.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示