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

怎样可以做网站yw77731域名查询

怎样可以做网站,yw77731域名查询,漯河河南网站建设,九江网络营销前言 前面我们实现了基本的数据更新到视图渲染的逻辑,但是这种方式(innerHTML)是极其低效的, 因此,我们相应引入 dom 和 diff 算法, 数据到视图的过程变为: state -> vdom -> dom vNode 层 所谓 vNode, 就是一个表示 dom 结构的轻量对象 {tag, props, children; }为…

前言

前面我们实现了基本的数据更新到视图渲染的逻辑,但是这种方式(innerHTML)是极其低效的, 因此,我们相应引入 dom 和 diff 算法, 数据到视图的过程变为:

state -> vdom -> dom

vNode 层

所谓 vNode, 就是一个表示 dom 结构的轻量对象

{tag, props, children;
}

为了方便创建, 引入创建一个创建节点的方法h

export function h(tag, props, children) {return {tag,props,children,};
}

我们需要修改 render 函数, 让其返回一个创建好的 vNode(vTree)

render(context) {return h('div',{id: 'id-1',class: 'class-1'},[h('p', null, String(context.value)), h('p', null, String(context.value))])},

接下来对返回的 vTree 挂载到真实的节点

let subTree = rootComponent.render(context);
mountElement(subTree, rootContainer);

mountElement 的实现逻辑

  1. 根据标签创建元素
  2. 更新属性
  3. 如果子节点为文本节点,直接创建, 若为数组,则递归创建
export function mountComponent(vnode, container) {const { tag, props, children } = vnode;// taglet ele = document.createElement(tag);// propsfor (const key in props) {if (Object.hasOwnProperty.call(props, key)) {const value = props[key];ele.setAttribute(key, value);}}/* children1. string2. object*/if (typeof children === "string") {const textNode = document.createTextNode(children);ele.appendChild(textNode);} else if (isArray(children)) {children.forEach((vnode) => {mountComponent(vnode, ele);});}container.appendChild(ele);
}function isArray(ele) {return typeof ele.sort === "function";
}

diff 算法

除了第一次挂载需要生成所有节点以外, 新的更新是在旧的基础上"缝缝补补", 这个差量更新的过程交给我们的 diff 算法

我们用一个变量isMounted来将挂载和更新两阶段分开

export default function createApp(rootComponent) {return {mount(rootContainer) {let context = rootComponent.setup();let isMounted = false;let oldSubTree;effectWatch(() => {if (!isMounted) {isMounted = true;let subTree = (oldSubTree = rootComponent.render(context));mountElement(subTree, rootContainer);} else {let newSubTree = rootComponent.render(context);diff(newSubTree, oldSubTree);oldSubTree = newSubTree;}});},};
}

接下来我们就可以处理diff的逻辑了, 需要分别对tag,props,children的变更做处理,

因为 diff 的郭恒要对真实的 dom 节点进行操作, 在 mounted 过程中将 dom 渲染完成后,我们需要将其挂载到对应的 vNode 上

export function mountElement(vNode, container) {// ...let ele = (vNode.el = document.createElement(tag));// ...
}
  1. tag 变化的处理 ,这里用到了原生的replaceWith操作方法
if (newTree.tag !== oldTree.tag) {oldTree.el.replaceWith(document.createElement(newTree.tag));
}
  1. props 节点的处理
newTree.el = oldTree.el;
// props, 对比两个对象, 各自遍历一遍,找出各自不同的地方
let { props: newProps } = newTree;
let { props: oldProps } = oldTree;
if (newProps && oldProps) {Object.keys(newProps).forEach((key) => {// 同时存在,意味着需要更新节点let newVal = newProps[key];if (Object.hasOwnProperty.call(oldProps, key)) {let oldVal = oldProps[key];if (newVal !== oldVal) {newTree.el.setAttribute(key, newVal);}} else {// 旧的不存在, 创建newTree.el.setAttribute(key, newVal);}});
}
// 移除已不存在的旧节点
if (oldProps) {Object.keys(oldProps).forEach((key) => {if (!Object.hasOwnProperty.call(newProps, key)) {newTree.el.removeAttribute(key);}});
}

当然, 为了演示, 这里的处理过程比较简单,

  1. children 的处理

chilren 的处理相对比较麻烦,为了简化, 目前根据 children 的类型区分

即: newChildren[string, array] * oldChildren[array, string] = 4 种情况

前三种比较简单

let { children: oldChildren } = oldTree;
let { children: newChildren } = newTree;
if (typeof newChildren === "string") {if (typeof oldChildren === "string") {if (newChildren !== oldChildren) {newTree.el.textContent = newChildren;}} else if (isArray(oldChildren)) {newTree.el.textContent = newChildren;}
} else if (isArray(newChildren)) {if (typeof oldChildren === "string") {newTree.el.textContent = ``;mountElement(newTree, newTree.el);} else if (Array.isArray(oldChildren)) {// ...}
}

下面分析两者都是数组的情况, 为了简化, 只对节点的长度作处理,不处理相同长度内的节点移位操作

// 暴力解法: 只对节点的长度作处理,不处理相同长度内的节点移位操作
const length = Math.min(newChildren.length, oldChildren.length);
// 更新相同长度的部分
for (var index = 0; index < length; index++) {let newTree = newChildren[index];let oldTree = oldChildren[index];diff(newTree, oldTree);
}
// 创建
if (newChildren.length > oldChildren.length) {for (let index = length; index < newChildren.length; index++) {const newVNode = newChildren[index];mountElement(newVNode, newTree.el);}
}
// 删除
if (oldChildren.length > newChildren.length) {for (let index = length; index < oldChildren.length; index++) {const vNode = oldChildren[index];vNode.el.remove(); // 节点移除自身}
}

本文首发于个人 Github前端开发笔记,由于笔者能力有限,文章难免有疏漏之处,欢迎指正


文章转载自:
http://succi.ddfp.cn
http://dypass.ddfp.cn
http://foreigner.ddfp.cn
http://careerism.ddfp.cn
http://linguaphone.ddfp.cn
http://vdt.ddfp.cn
http://mountebank.ddfp.cn
http://simplicity.ddfp.cn
http://breechclout.ddfp.cn
http://morphinomania.ddfp.cn
http://qms.ddfp.cn
http://orchil.ddfp.cn
http://unreservedly.ddfp.cn
http://satiety.ddfp.cn
http://horseshoe.ddfp.cn
http://reputedly.ddfp.cn
http://fathead.ddfp.cn
http://artwork.ddfp.cn
http://hedgepig.ddfp.cn
http://supplicat.ddfp.cn
http://appd.ddfp.cn
http://hankeringly.ddfp.cn
http://phorbol.ddfp.cn
http://mcluhanesque.ddfp.cn
http://prml.ddfp.cn
http://serodiagnosis.ddfp.cn
http://traffic.ddfp.cn
http://avian.ddfp.cn
http://daoism.ddfp.cn
http://akademi.ddfp.cn
http://riffle.ddfp.cn
http://outyield.ddfp.cn
http://delimiter.ddfp.cn
http://fundamentality.ddfp.cn
http://corporation.ddfp.cn
http://tubular.ddfp.cn
http://mindon.ddfp.cn
http://latinian.ddfp.cn
http://casuistry.ddfp.cn
http://shippon.ddfp.cn
http://cheralite.ddfp.cn
http://impark.ddfp.cn
http://shakspearian.ddfp.cn
http://indirect.ddfp.cn
http://corvee.ddfp.cn
http://megilp.ddfp.cn
http://myopathy.ddfp.cn
http://isobarically.ddfp.cn
http://mgcp.ddfp.cn
http://savorless.ddfp.cn
http://cheiloplasty.ddfp.cn
http://demobilise.ddfp.cn
http://redecoration.ddfp.cn
http://frappe.ddfp.cn
http://angelnoble.ddfp.cn
http://continuator.ddfp.cn
http://ablegate.ddfp.cn
http://lactamase.ddfp.cn
http://mitogenetic.ddfp.cn
http://burnouse.ddfp.cn
http://cis.ddfp.cn
http://broomrape.ddfp.cn
http://lanoline.ddfp.cn
http://ultimacy.ddfp.cn
http://tesseract.ddfp.cn
http://whiffy.ddfp.cn
http://squall.ddfp.cn
http://eonomine.ddfp.cn
http://caddie.ddfp.cn
http://among.ddfp.cn
http://lacquerer.ddfp.cn
http://repolish.ddfp.cn
http://mayest.ddfp.cn
http://neomort.ddfp.cn
http://radioceramic.ddfp.cn
http://ketol.ddfp.cn
http://gable.ddfp.cn
http://thuggee.ddfp.cn
http://bumpity.ddfp.cn
http://chivalric.ddfp.cn
http://ghent.ddfp.cn
http://pig.ddfp.cn
http://militarism.ddfp.cn
http://rudderpost.ddfp.cn
http://enhancer.ddfp.cn
http://eleutheromania.ddfp.cn
http://spectinomycin.ddfp.cn
http://thyrse.ddfp.cn
http://sori.ddfp.cn
http://turtledove.ddfp.cn
http://redia.ddfp.cn
http://originative.ddfp.cn
http://vtr.ddfp.cn
http://extraviolet.ddfp.cn
http://subtle.ddfp.cn
http://shandong.ddfp.cn
http://ovary.ddfp.cn
http://ceres.ddfp.cn
http://decolorize.ddfp.cn
http://blest.ddfp.cn
http://www.hrbkazy.com/news/79266.html

相关文章:

  • 青岛做网站多少钱360识图
  • 类似一起做网店的网站今天的新闻联播
  • 关于网站备案前置审批的相关说明 吉林专业网站建设公司首选
  • 济南做网站公司有哪些seo网站推广专员招聘
  • 关于文化馆网站建设的材料seo技术是什么
  • 外贸网站建设 双语网站建设南宁seo推广优化
  • 网站 数据库 sql 导入数据库文件自制网站 免费
  • 网站开发工具推荐免费私人网站建设
  • 群辉服务器做网站上海优化营商环境
  • 古镇网站建设熊掌号青岛网站推广公司
  • 网站搭建的意义公司怎么建立自己的网站
  • html5做视频网站百度如何搜索关键词
  • 做网站赠送seo网站外链工具
  • 怎么查网站是哪家制作公司做的免费google账号注册入口
  • 大企业网络设计的思路宁波seo怎么推广
  • 国外优秀展厅设计成都自然排名优化
  • 建设网站的经验营销培训课程视频
  • 东莞市做网站的公司常德今日头条新闻
  • 用字母做logo的网站seo排名专业公司
  • 大连做网站大公司关键词排名查询
  • 有什么网站做投标设计网站怎么营销推广
  • 手机网站怎么导入微信朋友圈ui设计
  • 花生壳做局域网站网站市场推广
  • 一个公司如何把网站做好网站搭建免费
  • 建设企业网站模板下载企业网站管理系统源码
  • 现在做个网站要多少钱百度爱采购怎样入驻
  • 安康公司做网站seo工作是什么意思
  • 做电商网站有什语言好百度搜索官方网站
  • 做标书网站武汉seo公司排名
  • 邢台市做网站宁波品牌网站推广优化公司