当前位置: 首页 > news >正文

虚拟dom及diff算法之 —— snabbdom

源码:https://github.com/snabbdom/snabbdom

测试环境搭建

npm i -S snabbdom
安装好的node_modules提供了js和ts的代码:build:js代码,src:ts代码

npm i -D webpack@5 webpack-cli@3 webpack-dev-server@3
webpack:构建工具,必须安装5版本的,因为5版本才支持exports导出的能力
webpack-dev-server:提供8080端口访问

package.json中配置运行文件的入口

"scripts": {"dev": "webpack-dev-server"
},

www/index.html

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title></head><body><!-- src/index.js中需要一个container容器 --><div id="container">你好</div><script src="/xuni/bundle.js"></script></body>
</html>

src/index.js

// 官方demo,参照https://github.com/snabbdom/snabbdom配置
import { init, classModule, propsModule, styleModule, eventListenersModule, h } from 'snabbdom'const patch = init([// Init patch function with chosen modulesclassModule, // makes it easy to toggle classespropsModule, // for setting properties on DOM elementsstyleModule, // handles styling on elements with support for animationseventListenersModule // attaches event listeners
])const container = document.getElementById('container')const vnode = h('div#container.two.classes', { on: { click: () => console.log('div clicked') } }, [h('span', { style: { fontWeight: 'bold' } }, 'This is bold'),' and this is just normal text',h('a', { props: { href: '/foo' } }, "I'll take you places!")
])
// Patch into empty DOM element – this modifies the DOM as a side effect
patch(container, vnode)const newVnode = h('div#container.two.classes', { on: { click: () => console.log('updated div clicked') } }, [h('span', { style: { fontWeight: 'normal', fontStyle: 'italic' } }, 'This is now italic type'),' and this is still just normal text',h('a', { props: { href: '/bar' } }, "I'll take you places!")
])
// Second `patch` invocation
patch(vnode, newVnode) // Snabbdom efficiently updates the old view to the new state

运行:yarn dev

访问:http://localhost:8080/

http://www.lryc.cn/news/218554.html

相关文章:

  • 毅速丨3D打印结合拓扑优化让轻量化制造更容易
  • CentOS 7使用RPM包安装MySQL5.7
  • UI设计工具都哪些常用的,推荐这5款
  • 小饭店点餐系统,小餐馆点餐怎么方便,操作简单的酒店点单软件
  • 面试经典150题——Day31
  • chinese_llama_aplaca训练和代码分析
  • 大数据Doris(十七):关于 Partition 和 Bucket 的数量和数据量的建议
  • 进击的巨人 完结篇 后篇-中文下载
  • 力扣刷题-二叉树-二叉树的非递归遍历
  • react_15
  • 关于ROS的网络通讯方式TCP/UDP
  • Leetcode—421.数组中两个数的最大异或值【中等】明天写一下字典树做法!!!
  • 数智赋能!麒麟信安参展全球智慧城市大会
  • 基础课21——知识库管理
  • 网络运维Day01
  • 从零配置一台linux主机
  • 【蓝桥每日一题]-倍增(保姆级教程 篇1)
  • CNN(卷积神经网络)、RNN(循环神经网络)和GCN(图卷积神经网络)
  • 在markdown中怎么画表格
  • 每天五分钟计算机视觉:搭建手写字体识别的卷积神经网络
  • 【React】【react-globe.gl】3D Objects效果
  • 目标检测YOLO系列从入门到精通技术详解100篇-【目标检测】SLAM(补充篇)
  • Pytorch 缓解过拟合和网络退化
  • 【算法】昂贵的聘礼(dijkstra算法)
  • hackergame2023菜菜WP
  • ubuntu20.04.6使用FTP-及相关安全配置
  • C++中不允许复制的类
  • 使用Python 脚自动化操作服务器配置
  • DL Homework 6
  • 软考高项论文-绩效域