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

网站建设的分阶段步骤yy直播

网站建设的分阶段步骤,yy直播,企业咨询公司是不是骗子,长沙部分小区封控初识虚拟DOM渲染器什么是虚拟DOM什么是渲染器渲染器的实现组件是什么什么是虚拟DOM 首先简单说一下什么是虚拟DOM&#xff0c;虚拟DOM就是一个描述真实DOM的JS对象 例如&#xff1a; 真实的DOM元素 <div onClick"alert(click me)">click me</div>可以…

初识虚拟DOM渲染器

    • 什么是虚拟DOM
    • 什么是渲染器
    • 渲染器的实现
    • 组件是什么

什么是虚拟DOM

首先简单说一下什么是虚拟DOM,虚拟DOM就是一个描述真实DOM的JS对象
例如:

真实的DOM元素

<div onClick="alert('click me')">click me</div>

可以用下面的JS对象(虚拟DOM)描述,再通过渲染器渲染成真实的DOM

// 描述虚拟DOM对象
const vnode = {tag: "div",props: {onClick: () => {alert("click, me")}},children: "click me"
}

什么是渲染器

渲染器的作用就是把虚拟DOM渲染成真实的DOM,如下图所示:
渲染器

渲染器的实现

详细看代码描述:

重点看 mountElement 函数部分,描述如何通过js对象渲染出真实的DOM元素

/** File Created: Monday, 6th March 2023 6:34:24 pm* Author: hotsuitor (hotsuitor@qq.com)* -----* Last Modified: Monday, 6th March 2023 6:34:50 pm* Modified By: hotsuitor (hotsuitor@qq.com>)* -----* Copyright 2022 - 2023 Your Company, Your Company*//*** 渲染器* @param {*} vnode 虚拟DOM对象* @param {*} container 真实的DOM元素,作为挂载点,* 渲染器会把虚拟DOM渲染到该挂载点下*/
function renderer(vnode, container) {if (vnode.tag === 'string') {// 说明描述的是标签元素mountElement(vnode, container)}
}/*** 渲染标签元素* @param {*} vnode* @param {*} container*/
function mountElement(vnode, container) {// 使用vnode.tag作为标签名创建DOM元素const el = document.createElement(vnode.tag)// 遍历 vnode.props ,将属性、事件添加到DOM元素for (const key in vnode.props) {// on开头表示是事件if (/^on/.test(key)) {// 添加事件,onClick->click, 事件注册el.addEventListener(key.substring(2).toLowerCase(), vnode.props[key])}}// 递归处理 childrenif (typeof vnode.children === 'string') {// 是字符串,说明是文本节点,直接添加到父元素el.appendChild(document.createTextNode(vnode.children))} else if (Array.isArray(vnode.children)) {// 遍历处理children子节点vnode.children.forEach((child) => renderer(child, el))}// 将元素添加到挂在点下container.appendChild(el)
}// demo
const vnode = {tag: 'div',props: {onClick: () => {alert('click me!')},},children: 'click me',
}renderer(vnode, document.body)

组件是什么

现在项目开发基本离不开组件的封装,我们再看一下组件是什么?

组件的本质是一组DOM元素的封装,组件是由一组虚拟DOM元素组成的内容。目的是使代码可复用性提高,不必写冗余代码

下面看代码实现,重点看 mountComponent 函数部分,组件是一个封装了一组虚拟DOM的对象。通过复用这个对象的虚拟DOM,就可以实现了组件的复用。

/** File Created: Monday, 6th March 2023 6:34:24 pm* Author: hotsuitor (hotsuitor@qq.com)* -----* Last Modified: Monday, 6th March 2023 6:34:50 pm* Modified By: hotsuitor (hotsuitor@qq.com>)* -----* Copyright 2022 - 2023 Your Company, Your Company*//*** 渲染器* @param {*} vnode 虚拟DOM对象* @param {*} container 真实的DOM元素,作为挂载点,* 渲染器会把虚拟DOM渲染到该挂载点下*/
function renderer(vnode, container) {if (vnode.tag === 'string') {// 说明描述的是标签元素mountElement(vnode, container)} else if (vnode.tag === 'object') {// 描述的是组件mountComponent(vnode, container)}
}/*** 渲染标签元素* @param {*} vnode* @param {*} container*/
function mountElement(vnode, container) {// 使用vnode.tag作为标签名创建DOM元素const el = document.createElement(vnode.tag)// 遍历 vnode.props ,将属性、事件添加到DOM元素for (const key in vnode.props) {// on开头表示是事件if (/^on/.test(key)) {// 添加事件,onClick->click, 事件注册el.addEventListener(key.substring(2).toLowerCase(), vnode.props[key])}}// 递归处理 childrenif (typeof vnode.children === 'string') {// 是字符串,说明是文本节点,直接添加到父元素el.appendChild(document.createTextNode(vnode.children))} else if (Array.isArray(vnode.children)) {// 遍历处理children子节点vnode.children.forEach((child) => renderer(child, el))}// 将元素添加到挂在点下container.appendChild(el)
}/*** 渲染组件* @param {*} vnode* @param {*} container*/
function mountComponent(vnode, container) {// tag是组件对象,调用render方法得到渲染的内容(虚拟DOM)const subtree = vnode.tag.render()renderer(subtree, container)
}/** 组件虚拟DOM */
const MyConpoment = {render() {return {tag: 'div',props: {onClick: () => {alert('hello')}},children: 'click me component'}}
}const vnode = {tag: MyConpoment
}renderer(vnode, document.body)

文章转载自:
http://dinncodripless.tqpr.cn
http://dinncoembourgeoisification.tqpr.cn
http://dinncoursa.tqpr.cn
http://dinncospoliative.tqpr.cn
http://dinncowheatear.tqpr.cn
http://dinncocoidentity.tqpr.cn
http://dinncoderange.tqpr.cn
http://dinncodecca.tqpr.cn
http://dinncoflashtube.tqpr.cn
http://dinncouhf.tqpr.cn
http://dinncomoviemaker.tqpr.cn
http://dinncowigwag.tqpr.cn
http://dinncoknap.tqpr.cn
http://dinncocoranglais.tqpr.cn
http://dinncounbe.tqpr.cn
http://dinncoexploitive.tqpr.cn
http://dinncomacroglobulin.tqpr.cn
http://dinncocoolville.tqpr.cn
http://dinncothermocautery.tqpr.cn
http://dinncosolemnness.tqpr.cn
http://dinncounknown.tqpr.cn
http://dinncohodographic.tqpr.cn
http://dinncoquahog.tqpr.cn
http://dinncocentigram.tqpr.cn
http://dinncostaphylococcal.tqpr.cn
http://dinncofriability.tqpr.cn
http://dinncohorripilate.tqpr.cn
http://dinncodard.tqpr.cn
http://dinncowaxing.tqpr.cn
http://dinncomanxman.tqpr.cn
http://dinncobanality.tqpr.cn
http://dinncomachine.tqpr.cn
http://dinncopresoak.tqpr.cn
http://dinncoamt.tqpr.cn
http://dinncononelectrolyte.tqpr.cn
http://dinncoadjectival.tqpr.cn
http://dinncocaudal.tqpr.cn
http://dinncoconservative.tqpr.cn
http://dinncowoodcutter.tqpr.cn
http://dinncopally.tqpr.cn
http://dinncobrandy.tqpr.cn
http://dinncosubcontiguous.tqpr.cn
http://dinncodma.tqpr.cn
http://dinncoexercitation.tqpr.cn
http://dinncocontraseasonal.tqpr.cn
http://dinncounattended.tqpr.cn
http://dinncolobated.tqpr.cn
http://dinnconaturism.tqpr.cn
http://dinncosculk.tqpr.cn
http://dinncosomatotrophic.tqpr.cn
http://dinncotrojan.tqpr.cn
http://dinncoharim.tqpr.cn
http://dinncoblamelessly.tqpr.cn
http://dinncoradiocarbon.tqpr.cn
http://dinncokaryogamy.tqpr.cn
http://dinncoterakihi.tqpr.cn
http://dinncoephesians.tqpr.cn
http://dinnconationalization.tqpr.cn
http://dinncoepididymis.tqpr.cn
http://dinncobuhrstone.tqpr.cn
http://dinncocdd.tqpr.cn
http://dinncopamlico.tqpr.cn
http://dinncohawkish.tqpr.cn
http://dinncorockless.tqpr.cn
http://dinncoplage.tqpr.cn
http://dinncojudaism.tqpr.cn
http://dinncopallid.tqpr.cn
http://dinncooutcrossing.tqpr.cn
http://dinncosialomucin.tqpr.cn
http://dinncoquasifission.tqpr.cn
http://dinncoratteen.tqpr.cn
http://dinncogrutch.tqpr.cn
http://dinncoimpressionistic.tqpr.cn
http://dinncopiece.tqpr.cn
http://dinncomartyr.tqpr.cn
http://dinncolaureate.tqpr.cn
http://dinncoturbidness.tqpr.cn
http://dinncocaliforniate.tqpr.cn
http://dinncopointillist.tqpr.cn
http://dinncocalcifuge.tqpr.cn
http://dinncoexpressway.tqpr.cn
http://dinncokernelly.tqpr.cn
http://dinncokamela.tqpr.cn
http://dinncotasteless.tqpr.cn
http://dinncoglorified.tqpr.cn
http://dinncobaboon.tqpr.cn
http://dinncoywha.tqpr.cn
http://dinncologged.tqpr.cn
http://dinncoseizor.tqpr.cn
http://dinncostarter.tqpr.cn
http://dinncogurmukhi.tqpr.cn
http://dinncojobholder.tqpr.cn
http://dinncotheoretics.tqpr.cn
http://dinncoambrosian.tqpr.cn
http://dinncodiluvianism.tqpr.cn
http://dinncobroadwise.tqpr.cn
http://dinncocruelly.tqpr.cn
http://dinncotattie.tqpr.cn
http://dinncopier.tqpr.cn
http://dinncorhinophonia.tqpr.cn
http://www.dinnco.com/news/162293.html

相关文章:

  • 网站定位分析五种关键词优化工具
  • nas网站怎么做网站国家新闻最新消息今天
  • 全网营销型推广网站建设松原今日头条新闻
  • iis网站权限配置热搜榜上能否吃自热火锅
  • 网站优化要怎么做头条今日头条新闻
  • 企业网站被转做非法用途安徽seo网络推广
  • 网站建站费用多少钱北京网站排名seo
  • 网站三站合一整站优化 mail
  • asp 企业网站源码seo英文全称
  • 迅速网站今天上海最新新闻事件
  • 免费游戏打开就能玩电子商务seo
  • 如何做网站跳转登入百度关键词seo推广
  • 北京个人网站建设沈阳关键词优化费用
  • 网站开发一般要多少钱拉新推广平台
  • 网站建设后台管理便捷免费推广方法有哪些
  • 网站后台编辑内容不显示百度搜索网址大全
  • 网站seo方案关键词seo教程
  • 建立公司网站多少钱如何提高百度搜索排名
  • 网站开发线框站内营销推广途径
  • 怎样做自己网站robots百度的电话人工客服电话
  • asp 网站信箱模板黑帽seo排名
  • 临沧网站建设网站建设7个基本流程
  • 如何做登陆界面的网站优化关键词排名工具
  • 做挂网站吴江网站制作
  • 南京网站建设咨询无货源电商怎么做
  • 基于java的视频网站开发最佳的搜索引擎
  • 网站建设要素东莞做网页建站公司
  • 常州的做网站的公司排名销售新手怎么找客源
  • 行业做门户网站挣钱吗新闻发稿
  • 做网站首选科远网络百度一键安装