Skip to content

Commit

Permalink
实现provide-inject及实现自定义渲染器customRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
jindy committed Jun 9, 2022
1 parent 0e7c24e commit 11b3f99
Show file tree
Hide file tree
Showing 16 changed files with 709 additions and 329 deletions.
50 changes: 50 additions & 0 deletions example/ApiProvide/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { h, provide, inject } from "../../lib/guide-mini-vue.esm.js"

const Provider = {
name: 'Provider',
setup() {
provide('foo', 'fooVal')
provide('bar', 'barVal')
},
render() {
return h('div', {}, [h('p', {}, 'Provider'), h(Pro)])
}
}

const Pro = {
name: 'Pro',
setup() {
provide('foo', 'ProVal')
const foo = inject('foo')
return {
foo
}
},
render() {
return h('div', {}, [h('p', {}, 'Pro---' + this.foo), h(Consumer)])
}
}

const Consumer = {
name: 'Consumer',
setup() {
const foo = inject('foo')
const bar = inject('bar')
// const baz = inject('baz', 'defaultVal')
const baz = inject('baz', () => 'defaultVal')
return {
foo,bar, baz
}
},
render() {
return h('div', {}, `Consumer - ${this.foo} - ${this.bar} - ${this.baz}`)
}
}

export const App = {
name: 'App',
setup() {},
render() {
return h('div', {}, [h('p', {}, 'ApiProvide'), h(Provider)])
}
}
16 changes: 16 additions & 0 deletions example/ApiProvide/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>provide-inject</title>
</head>

<body>
<div id="app"></div>
<script src="./main.js" type="module"></script>
</body>

</html>
6 changes: 6 additions & 0 deletions example/ApiProvide/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

import { createApp } from '../../lib/guide-mini-vue.esm.js'
import { App } from './App.js'

const rootContainer = document.querySelector('#app')
createApp(App).mount(rootContainer)
13 changes: 13 additions & 0 deletions example/CustomRenderer/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { h } from '../../lib/guide-mini-vue.esm.js'
export const App = {
name: 'App',
setup() {
return {
x: 100,
y: 100
}
},
render() {
return h('rect', {x: this.x, y: this.y})
},
}
26 changes: 26 additions & 0 deletions example/CustomRenderer/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.red {
color: red;
}

.blue {
color: blue
}
</style>
<script src="https://pixijs.download/release/pixi.js"></script>
</head>

<body>
<div id="app"></div>
<script src="./main.js" type="module"></script>
</body>

</html>
35 changes: 35 additions & 0 deletions example/CustomRenderer/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import { createRenderer } from '../../lib/guide-mini-vue.esm.js'
import { App } from './App.js'

// const rootContainer = document.querySelector('#app')
// createApp(App).mount(rootContainer)

console.log('pixi:', PIXI)

const geme = new PIXI.Application({
width: 500,
height: 500
})

document.body.append(geme.view)

const renderer = createRenderer({
createElement(type){
if(type === 'rect') {
const rect = new PIXI.Graphics()
rect.beginFill(0xff0000)
rect.drawRect(0,0,100,100)
rect.endFill()
return rect
}
},
patchProp(el, key, val){
el[key] = val
},
insert(el, parent){
parent.addChild(el)
}
})

renderer.createApp(App).mount(geme.stage)
Loading

0 comments on commit 11b3f99

Please sign in to comment.