Skip to content

Commit

Permalink
feat: add vue3 use vue2 demo
Browse files Browse the repository at this point in the history
  • Loading branch information
doerme committed Oct 28, 2024
1 parent c0ab34b commit fcdd5ab
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 58 deletions.
6 changes: 3 additions & 3 deletions demos/vue2/vue-2-base/src/components/Content.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export default {
DynamicButton: () => import('./Button'),
},
props: {
dataProps: {type: Number, required: false},
methodProps: {type: Function, required: false},
// dataProps: {type: Number, required: false},
// methodProps: {type: Function, required: false},
},
data() {
return {
Expand All @@ -42,7 +42,7 @@ export default {
}
},
mounted() {
this.methodProps && this.methodProps()
// this.methodProps && this.methodProps()
// console.log(this.$props)
// console.log(this.$attrs)
// console.log(this.$listeners)
Expand Down
9 changes: 9 additions & 0 deletions demos/vue3_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# vue3中使用vue2

`demo`展示如何在`vue3`项目中使用`vue2`基站的远程组件

# 运行
```bash
pnpm i
pnpm dev
```
12 changes: 12 additions & 0 deletions demos/vue3_2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "v3u2-demo",
"ignored": true,
"version": "0.0.1",
"scripts": {
"dev": "pnpm --filter 'v3u2-*' --parallel dev",
"build": "pnpm --filter v3u2-* --parallel build",
"serve": "pnpm --filter v3u2-* --parallel serve",
"stat": "pnpm --filter v3u2-* --parallel stat"
}
}

3 changes: 1 addition & 2 deletions demos/vue3_2/vue-2-base/emp.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ export default defineConfig(store => {
},
empRuntime: {
runtimeLib: "https://unpkg.yy.com/@empjs/share@3.1.5/output/sdk.js",
frameworkLib: "https://unpkg.com/@empjs/lib-vue-2@0.0.1/dist",
shareLib: {
vue: 'Vue@https://unpkg.com/vue@2.7.14/dist/vue.min.js',
vuex: "Vuex@https://unpkg.com/vuex@3.6.2/dist/vuex.min.js",
'element-ui': [
'ELEMENT@https://unpkg.com/element-ui/lib/index.js',
"https://unpkg.com/element-ui/lib/theme-chalk/index.css",
Expand Down
2 changes: 1 addition & 1 deletion demos/vue3_2/vue-2-base/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "vue2-base",
"name": "v3u2-vue2-base",
"version": "1.0.0",
"description": "",
"main": "index.js",
Expand Down
10 changes: 5 additions & 5 deletions demos/vue3_2/vue-2-base/src/components/Content.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<template>
<div>
<button class="button bigSize" @click="increment">Vuex Store : {{ $store.state.count }}</button>
<!-- <button class="button bigSize" @click="increment">Vuex Store : {{ $store.state.count }}</button> -->
<p @click="showMore">More... update from base</p>
<div class="more">
<ul v-if="isMore === true">
<li>
<button class="button" @click="increment">Vuex Store : {{ $store.state.count }}</button>
<!-- <button class="button" @click="increment">Vuex Store : {{ $store.state.count }}</button> -->
</li>
<li>
<h2>{{ title }}</h2>
Expand All @@ -19,7 +19,7 @@
</ul>
</div>
<h2>JSX Dynamic Component</h2>
<Hello />
<!-- <Hello /> -->
</div>
</template>

Expand All @@ -28,7 +28,7 @@ import Button from './Button.vue'
export default {
components: {
Button,
Hello: () => import('./Hello'),
// Hello: () => import('./Hello'),
DynamicButton: () => import('./Button'),
},
props: {
Expand All @@ -52,7 +52,7 @@ export default {
this.isMore = !this.isMore
},
increment() {
this.$store.commit('increment')
// this.$store.commit('increment')
// console.log(this.$store.state.count)
},
add() {
Expand Down
18 changes: 0 additions & 18 deletions demos/vue3_2/vue-2-base/src/components/Hello.jsx

This file was deleted.

3 changes: 0 additions & 3 deletions demos/vue3_2/vue-2-base/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<h1>SVGA COMPONENT LOGO:</h1>
<!-- <Logo class="svg-logo" /> -->
<h2>Hello JSX Component</h2>
<Hello />
<h2>Img Example in src</h2>
<ul class="imgbox">
<li>
Expand All @@ -26,7 +25,6 @@
<script>
import CompositionApi from '../components/CompositionApi'
import Content from '../components/Content'
import Hello from '../components/Hello'
// import Logo from './logo.svg'
import Table from '../components/table'
export default {
Expand All @@ -35,7 +33,6 @@ export default {
// Logo,
Table,
CompositionApi,
Hello,
},
}
</script>
Expand Down
15 changes: 7 additions & 8 deletions demos/vue3_2/vue-3-project/emp.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ export default defineConfig(store => {
},
empRuntime: {
runtimeLib: "https://unpkg.yy.com/@empjs/share@3.1.5/output/sdk.js",
shareLib: {
vue: 'Vue@https://unpkg.com/vue@2.7.14/dist/vue.min.js',
vuex: "Vuex@https://unpkg.com/vuex@3.6.2/dist/vuex.min.js",
'element-ui': [
'ELEMENT@https://unpkg.com/element-ui/lib/index.js',
"https://unpkg.com/element-ui/lib/theme-chalk/index.css",
],
},
frameworkLib: "https://unpkg.com/@empjs/lib-vue-2@0.0.2/dist",
// shareLib: {
// 'element-ui': [
// 'ELEMENT@https://unpkg.com/element-ui/lib/index.js',
// "https://unpkg.com/element-ui/lib/theme-chalk/index.css",
// ],
// },
framework: undefined,
},
}),
Expand Down
2 changes: 1 addition & 1 deletion demos/vue3_2/vue-3-project/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "vue3-project",
"name": "v3u2-vue3-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
Expand Down
18 changes: 10 additions & 8 deletions demos/vue3_2/vue-3-project/src/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
<div>
<h1>Vue 3 Project</h1>
<h2>=== @v2b/Content ===</h2>
<Content />
<div id="vue2Content"></div>
<vue2-content />
<h2>CompositionApi</h2>
<CompositionApi />
<!-- <CompositionApi /> -->
<h2>=== @v2b/Table ===</h2>
<Table />
<!-- <Table /> -->
</div>
</template>


<script>
//import Content from "@v2b/Content"
import Content from "@v2b/Content"
import {vue2ToVue3} from './util'
export default {
components: {
Content: () => import('@v2b/Content'),
Table: () => import('@v2b/Table'),
CompositionApi: () => import('@v2b/CompositionApi'),
vue2Content: vue2ToVue3(Content, 'vue2Content')
// Content: defineAsyncComponent(() => import('@v2b/Content')),
// Table: defineAsyncComponent(() => import('@v2b/Table')),
// CompositionApi: defineAsyncComponent(() => import('@v2b/CompositionApi')),
},
}
</script>
Expand Down
6 changes: 3 additions & 3 deletions demos/vue3_2/vue-3-project/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// import Antd from '@v3/Antd'
import {createApp} from 'vue'
import App from './App.vue'
import {router} from './router'
import Home from './home.vue'
// console.log('Antd', Antd)
const app = createApp(App)
const app = createApp(Home)
// app.use(Antd)
app.use(router)
// app.use(router)
app.mount('#emp-root')
41 changes: 41 additions & 0 deletions demos/vue3_2/vue-3-project/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

function bindSlotContext(target = {}, context) {
return Object.keys(target).map(key => {
const vnode = target[key];
vnode.context = context;
return vnode;
});
}

/*
* Transform vue2 components to DOM.
*/
export function vue2ToVue3(WrapperComponent, wrapperId) {
const Vue2 = EMP_ADAPTER_VUE.Vue
const defineAsyncComponent = Vue2.defineAsyncComponent;
let vm;
return {
mounted() {
const slots = bindSlotContext(this.$slots, this.__self);
vm = new Vue2({
render: createElement => {
return createElement(
WrapperComponent,
{
on: this.$attrs,
attrs: this.$attrs,
props: this.$props,
scopedSlots: this.$scopedSlots,
},
slots,
);
},
});
vm.$mount(`#${wrapperId}`);
},
props: WrapperComponent.props,
render() {
vm && vm.$forceUpdate();
},
};
}
Loading

0 comments on commit fcdd5ab

Please sign in to comment.