<template>
<div> </div>
</template>
<script>
export default {
name: 'Count',
setup() {
const vnode = {
tag: 'div',
props: {
onClick: () => alert('hello')
},
children: 'click me'
}
function render(vnode, container) {
const el = document.createElement(vnode.tag)
for (const key in vnode.props) {
if (/^on/.test(key)) {
el.addEventListener(
key.substr(2).toLowerCase(),
vnode.props[key]
)
}
}
if (typeof vnode.children === 'string') {
el.appendChild(document.createTextNode(vnode.children))
} else if (Array.isArray(vnode.children)) {
vnode.children.forEach(child => renderer(child, el))
}
container.appendChild(el)
render(vnode, document.body)
}
}
</script>
这个为什么也不行
<template>
<div> </div>
</template>
<script>
export default {
name: 'Count',
setup() {
const vnode = {
tag: 'div',
props: {
onClick: () => { console.log('点击了.') },
class: 'red'
},
children: '点击'
}
},
function render(vnode, container){
const el = document.createElement(vnode.tag)
for(const key in vnode.props){
if(/^on/.test(key)){
el.addEventListener(
key.substring(2).toLowerCase(),
vnode.props[key]
)
}else{
el.setAttribute(key, vnode.props[key])
}
}
let children = vnode.children
if(typeof children === 'string'){
const textNode = document.createTextNode(children)
el.appendChild(textNode)
}else if( Array.isArray(children) ){
children.forEach( item=> render(item, el) )
}
container.appendChild(el)
render(vnode, docunmet.body)
}
}
</script>
不行
<!DOCTYPE html>
<html>
<script src="https://unpkg.com/vue@next"></script>
<div id="app">
</div>
<script>
const vnode = {
tag: 'div',
props: {
onClick: () => { console.log('点击了.') },
class: 'red'
},
children: '点击'
}
function render(vnode, container){
const el = document.createElement(vnode.tag)
for(const key in vnode.props){
if(/^on/.test(key)){
el.addEventListener(
key.substring(2).toLowerCase(),
vnode.props[key]
)
}else{
el.setAttribute(key, vnode.props[key])
}
}
let children = vnode.children
if(typeof children === 'string'){
const textNode = document.createTextNode(children)
el.appendChild(textNode)
}else if( Array.isArray(children) ){
children.forEach( item=> render(item, el) )
}
container.appendChild(el)
render(vnode, docunmet.body)
}
</script>
</head>
</html>
不行
<!DOCTYPE html>
<html>
<script src="https://unpkg.com/vue@next"></script>
<div id="app">
</div>
<script>
const vnode = {
tag: 'div',
props: {
onClick: () => alert('hello')
},
children: 'click me'
}
function render(vnode, container) {
const el = document.createElement(vnode.tag)
for (const key in vnode.props) {
if (/^on/.test(key)) {
el.addEventListener(
key.substr(2).toLowerCase(),
vnode.props[key]
)
}
}
if (typeof vnode.children === 'string') {
el.appendChild(document.createTextNode(vnode.children))
} else if (Array.isArray(vnode.children)) {
vnode.children.forEach(child => renderer(child, el))
}
container.appendChild(el)
render(vnode, document.body)
}
</script>
</script>
</head>
</html>
不知道该怎样写
<!DOCTYPE html>
<html>
<script src="https://unpkg.com/vue@next"></script>
<div>
</div>
<script>
const vnode = {
tag: 'div',
props: {
onClick: () => alert('hello')
},
children: 'click me'
}
function renderer(vnode, container) {
const el = document.createElement(vnode.tag)
for (const key in vnode.props) {
if (/^on/.test(key)) {
el.addEventListener(
key.substr(2).toLowerCase(),
vnode.props[key]
)
}
}
if (typeof vnode.children === 'string') {
el.appendChild(document.createTextNode(vnode.children))
} else if (Array.isArray(vnode.children)) {
vnode.children.forEach(child => renderer(child, el))
}
container.appendChild(el)
renderer(vnode, document.body)
}
</script>
</script>
</head>
</html>
|