虚拟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中配置运行文件的入口

javascript 复制代码
"scripts": {
 "dev": "webpack-dev-server"
},

www/index.html

javascript 复制代码
<!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

javascript 复制代码
// 官方demo,参照https://github.com/snabbdom/snabbdom配置
import { init, classModule, propsModule, styleModule, eventListenersModule, h } from 'snabbdom'

const patch = init([
  // Init patch function with chosen modules
  classModule, // makes it easy to toggle classes
  propsModule, // for setting properties on DOM elements
  styleModule, // handles styling on elements with support for animations
  eventListenersModule // 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/

相关推荐
码力码力我爱你4 小时前
QT + WebAssembly + Vue环境搭建
vue.js·vue·wasm·webassembly·emscripten
尸僵打怪兽12 小时前
后台数据管理系统 - 项目架构设计-Vue3+axios+Element-plus(0916)
vue.js·vue
是梦终空21 小时前
JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·源代码·交通订票
天下无贼!1 天前
2024年最新版Vue3学习笔记
前端·vue.js·笔记·学习·vue
这个需求建议不做1 天前
vue3打包配置 vite、router、nginx配置
前端·nginx·vue
QGC二次开发1 天前
Vue3 : Pinia的性质与作用
前端·javascript·vue.js·typescript·前端框架·vue
罗_三金1 天前
前端框架对比和选择?
javascript·前端框架·vue·react·angular
一个很帅的帅哥2 天前
实现浏览器的下拉加载功能(类似知乎)
开发语言·javascript·mysql·mongodb·node.js·vue·express
孟诸2 天前
计算机专业毕设-校园新闻网站
java·vue·毕业设计·springboot·课程设计
Sca_杰2 天前
vue2使用npm引入依赖(例如axios),报错Module parse failed: Unexpected token解决方案
前端·javascript·vue