首先先看一下Typescript代码:

import template = require('art-template/lib/template-web');

interface TemplateBindConfig {
el: string
data: object
} class TmpBind { el: string template: any data: object renderFn: any // 构造函数
constructor(config:TemplateBindConfig) {
// 绑定的容器id
this.el = config.el; // 注入的数据
this.data = config.data; this.renderFn = null
var nodes=document.querySelector(config.el).children;
var i=nodes.length;
if(i>0){
while(i--){
if(nodes[i].tagName.toLowerCase()=="script" && nodes[i].getAttribute("type")=="text/html"){
// 模版id
this.template = nodes[i].innerHTML;
break;
}
}
} this.render()
} // 渲染模版
render(data?): void {
if (data) {
this.data = data;
}
// 解析模版
if(!this.renderFn){
this.renderFn= template.compile(this.template);
} const source = this.renderFn(this.data);
// 更新容器的值
document.querySelector(this.el).innerHTML = source; } setData(value: any): void {
this.data=value;
this.render();
} // 重新设置模板
setTemplate(value: any): void {
this.template = value;
this.renderFn= template.compile(value);
this.render();
} // 获取数据
getData(): object {
return this.data;
} }

编译后的JavaScript代码:

    var TmpBind = /** @class */ (function () {
// 构造函数
function TmpBind(config) {
// 绑定的容器id
this.el = config.el;
// 注入的数据
this.data = config.data;
this.renderFn = null;
var nodes = document.querySelector(config.el).children;
var i = nodes.length;
if (i > 0) {
while (i--) {
if (nodes[i].tagName.toLowerCase() == "script" && nodes[i].getAttribute("type") == "text/html") {
// 模版id
this.template = nodes[i].innerHTML;
break;
}
}
}
this.render();
}
// 渲染模版
TmpBind.prototype.render = function (data) {
if (data) {
this.data = data;
}
// 解析模版
if (!this.renderFn) {
this.renderFn = template.compile(this.template);
}
var source = this.renderFn(this.data);
// 更新容器的值
document.querySelector(this.el).innerHTML = source;
};
TmpBind.prototype.setData = function (value) {
this.data = value;
this.render();
};
// 重新设置模板
TmpBind.prototype.setTemplate = function (value) {
this.template = value;
this.renderFn = template.compile(value);
this.render();
};
// 获取数据
TmpBind.prototype.getData = function () {
return this.data;
};
return TmpBind;
}());

因为是基于art-template/lib/template-web.js 请下载并引用一下。https://github.com/aui/art-template

实用示例:

<button id="button1">设置新值</button>
<button id="button2">获取值</button>
<div id="div1">
<script type="text/html">
{{msg}}
<div>
<img src="{{url}}"/>
</div>
<ul>
{{each list as item i}}
<li>{{item}}</li>
{{/each}}
</ul>
</script>
</div>
var vm = new TmpBind({
el: "#div1",
data: {msg: "欢迎来到模版绑定的世界", list: ['1', 2, 3, 4, 5], url: "https://www.baidu.com/img/bd_logo1.png"}
});
// 设置值
document.querySelector("#button1").addEventListener("click", function (evt) {
var data = vm.getData()
data.msg="欢迎来到模版绑定的世界222222222";
vm.setData(data);
})
// 获取值
document.querySelector("#button2").addEventListener("click", function (evt) {
alert(JSON.stringify(vm.getData()))
})

实现效果如下:

如果这篇文章对您有帮助,您可以打赏我

技术交流QQ群:15129679

利用art.template模仿VUE的更多相关文章

  1. 利用art.template模仿VUE 一次渲染多个模版

    TypeScript代码 import template = require('art-template/lib/template-web'); interface TemplateBindConfi ...

  2. 利用 vue-cli 构建一个 Vue 项目

    一.项目初始构建 现在如果要构建一个 Vue 的项目,最方便的方式,莫过于使用官方的 vue-cli . 首先,咱们先来全局安装 vue-cli ,打开命令行工具,输入以下命令: $ npm inst ...

  3. art template前端模板引擎

    偶然看到后台有一段代码 采用的是art template的模板引擎 地址为 http://aui.github.io/artTemplate/ 这段代码很简洁 var html = template( ...

  4. 用ES6的class模仿Vue写一个双向绑定

    原文地址:用ES6的class模仿Vue写一个双向绑定 点击在线尝试一下 最终效果如下: 构造器(constructor) 构造一个TinyVue对象,包含基本的el,data,methods cla ...

  5. 利用Atomic, ThreadLocal, 模仿AQS, ReentrantLock

    /** * @description 队列同步器,利用原子整形模仿AQS,非公平锁(简单自适应自旋) * @since 2020/2/4 */ public class QueueSynchroniz ...

  6. 利用vue-cli3快速搭建vue项目详细过程

    一.介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@vue/cli 全局安装的 npm 包,提供了终端里的vue命令(如:vue create .vue ...

  7. 利用模板template动态渲染jsp页面

    一.场景 在js中写html简直是噩梦,刚进新公司,在codereview的时候得知可以通过将html模板写在jsp页面,然后由js调取模板,利用replace()方法替换传值的方式避免在js中拼接h ...

  8. 利用VUE-CLI脚手架搭建VUE项目

    前言 在学习完vue基础语法之后,学着利用vue-cli脚手架搭建一个项目,本篇随笔主要记录搭建的过程,供大家一起学习. 具体内容 搭建vue项目的准备工作 1.安装Nodejs.NPM以及VSCod ...

  9. art.template 循环里面分组。

    后台提供给我们一个数组,我们要用模版实现上面的格式输出怎么版呢? 下面就是解决方案: <h2>循环4个一组</h2> <script type="text/ht ...

随机推荐

  1. 常用的PHP字符串操作函数

    1.strlen 但是要注意!如果字符串中是汉字等其他字符时候呢? $str = "我"; echo strlen($str); //一个汉字,在UTF8格式下,显示3, ANSI ...

  2. Linux crontab命令参数和时间格式说明

    crontab 是用来让使用者在固定时间或固定间隔执行程序之用 参数说明 选项 功能 -e 编辑crontab定时任务 -l 查询crontab任务 -r 删除当前用户所有的crontab任务 时间格 ...

  3. Flume实战案例运维篇

    Flume实战案例运维篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Flume概述 1>.什么是Flume Flume是一个分布式.可靠.高可用的海量日志聚合系统,支 ...

  4. 第五次作业——Alpha项目测试

    第五次作业——Alpha项目测试 格式描述: 这个作业属于哪个课程 2019秋软工17级系统分析与设计 这个作业要求在哪里 作业要求 团队名称 杨荣模杰和他的佶祥虎 这个作业的目标 测试其他组项目并写 ...

  5. Linux/Raspbian 每个目录用途说明

    本文转自无聊小博,很多刚接触树莓派/Linux 的同学会在给树莓派安装.卸载.配置软件时,软件和配置文件等存放在哪儿产生疑惑.也会遇到诸如“磁盘分区”.U盘挂载等涉及到的目录路径问题.Linux 的目 ...

  6. tensorflow API _ 6 (tf.gfile)

    一.gfile模块是什么 tf.gfile模块的主要角色是:1.提供一个接近Python文件对象的API,以及2.提供基于TensorFlow C ++ FileSystem API的实现. C ++ ...

  7. windows查看端口被占用情况

    首先,使用cmd命令打开CMD命令窗口 使用下面的命令来查看某端口被占用的情况,以8035为例: netstat -ano|findstr " 结果如下图: 最后一列的6532为PID号,根 ...

  8. 算法笔记求序列A每个元素左边比它小的数的个数(树状数组和离散化)

    #include <iostream> #include <algorithm> #include <cstring> using namespace std ; ...

  9. Spring Security 认证执行流程

    本文基于 Spring Security 5.x 推荐阅读: 项目集成Spring Security SpringSecurity 整合 JWT 一.外层-正常登陆调用 项目启动后会自动寻找 User ...

  10. gin内置验证器使用

    gin内置验证器使用 func TopicUrl(f1 validator.FieldLevel) bool { return true //返回true表示验证成功 } func main(){ r ...