利用art.template模仿VUE
首先先看一下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的更多相关文章
- 利用art.template模仿VUE 一次渲染多个模版
TypeScript代码 import template = require('art-template/lib/template-web'); interface TemplateBindConfi ...
- 利用 vue-cli 构建一个 Vue 项目
一.项目初始构建 现在如果要构建一个 Vue 的项目,最方便的方式,莫过于使用官方的 vue-cli . 首先,咱们先来全局安装 vue-cli ,打开命令行工具,输入以下命令: $ npm inst ...
- art template前端模板引擎
偶然看到后台有一段代码 采用的是art template的模板引擎 地址为 http://aui.github.io/artTemplate/ 这段代码很简洁 var html = template( ...
- 用ES6的class模仿Vue写一个双向绑定
原文地址:用ES6的class模仿Vue写一个双向绑定 点击在线尝试一下 最终效果如下: 构造器(constructor) 构造一个TinyVue对象,包含基本的el,data,methods cla ...
- 利用Atomic, ThreadLocal, 模仿AQS, ReentrantLock
/** * @description 队列同步器,利用原子整形模仿AQS,非公平锁(简单自适应自旋) * @since 2020/2/4 */ public class QueueSynchroniz ...
- 利用vue-cli3快速搭建vue项目详细过程
一.介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@vue/cli 全局安装的 npm 包,提供了终端里的vue命令(如:vue create .vue ...
- 利用模板template动态渲染jsp页面
一.场景 在js中写html简直是噩梦,刚进新公司,在codereview的时候得知可以通过将html模板写在jsp页面,然后由js调取模板,利用replace()方法替换传值的方式避免在js中拼接h ...
- 利用VUE-CLI脚手架搭建VUE项目
前言 在学习完vue基础语法之后,学着利用vue-cli脚手架搭建一个项目,本篇随笔主要记录搭建的过程,供大家一起学习. 具体内容 搭建vue项目的准备工作 1.安装Nodejs.NPM以及VSCod ...
- art.template 循环里面分组。
后台提供给我们一个数组,我们要用模版实现上面的格式输出怎么版呢? 下面就是解决方案: <h2>循环4个一组</h2> <script type="text/ht ...
随机推荐
- ES读写索引内幕分析
一.简介 ES中的索引都进行分片,每个分片都会保存多个副本.这些副本称为复制组,在添加或删除索引时必须同步副本.如果不这样,从不同的副本中读取的索引可能截然不同.保持分片副本同步并从中提供读取的过程被 ...
- PHP 根据配置转换数组中的键名 方便给前端的时候改键名
/** * 根据配置转换数组中的键名 * @param array $data 数据,必须为二维数组格式 [0=>[]] * @param array $keyNameMapArr 键名转换配置 ...
- CASE WHEN 函数
--Case函数: --有两种格式: -- 1.简单Case函数. -- 2.Case搜索函数. --1.简单Case函数: -- CASE [COLUMN_NAME] -- WHEN ['条件参数' ...
- linux查看redis安装目录
1.在redis下查看安装目录 如果命令 which 和whereis 都找不到安装目录,可使用以下办法 ps -ef|grep redis 得到了进程号 xxxx 然后 ls -l /proc/xx ...
- Unicode原理和互转中文
代码点Unicode标准的本意很简单:希望给世界上每一种文字系统的每一个字符,都分配一个唯一的整数,这些整数叫做代码点(Code Points). 代码空间所有的代码点构成一个代码空间(Code Sp ...
- 微信小程序中padding-right和margin-right无效
在小程序中遇到样式padding-right和margin-right无效,调试发现设置了padding后,宽度已经大于页面的实际宽度,除了设置float:right之外,找不到办法让右侧paddin ...
- C#写的WebServices可运行于树莓派
阅读目录 Raspkate - 基于.NET的可运行于树莓派的轻量型Web服务器 Raspkate项目 演示 回到目录 Raspkate - 基于.NET的可运行于树莓派的轻量型Web服务器 最近 ...
- 最小圆覆盖(洛谷 P1742 增量法)
题意:给定N个点,求最小圆覆盖的圆心喝半径.保留10位小数点. N<1e5: 思路:因为精度要求较高,而且N比较大,所以三分套三分的复杂度耶比较高,而且容易出错. 然是写下增量法吧. 伪代码加深 ...
- axure快速上手
Axure RP是一个专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写.Axure RP是美国Axur ...
- jQuery的下载以及使用
一.概述 1.版本选择 jquery官网 jQuery的版本有很多,大家在选择版本的时候,一般原则是越新越好,但其实不然,jQuery版本是在不断进步和发展的,最新版是当时最高技术水平,也是最先进的技 ...