Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readme.md vue demo problem #25

Open
ponkans opened this issue Nov 24, 2022 · 1 comment
Open

readme.md vue demo problem #25

ponkans opened this issue Nov 24, 2022 · 1 comment

Comments

@ponkans
Copy link

ponkans commented Nov 24, 2022

The problem:

A caveat to readme.md's vue demo is that if a module is reused more than once on the same page, vueInstance can be shared more than once. react won't have this problem because the reatc update is based on container, which is unique.

import Vue from 'vue/index';
import App from './component/Hello.vue';

let vueInstance = null;

export async function bootstrap() {
  console.log('vue app bootstraped');
}

export async function mount(container, props) {
  console.log('magic-microservices-component-vue mount >>> ', props);
  vueInstance = Vue.createApp({
    ...App,
    data() {
      return props;
    },
  }).mount(container);
}

export async function updated(attrName, value) {
  console.log('magic-microservices-component-vue update', attrName, ' >>> ', value);
  vueInstance[attrName] = value;
  vueInstance.$forceUpdate();
}

export async function unmount() {
  console.log('vue app will unmount');
}

hack:

import { createApp } from 'vue';
import App from './App.vue';

/**
 * Multiple vue instances are identified by container as unique identifiers
 */
const vueInstanceMap = new WeakMap();

export async function mount(container: Element, props: any) {
  const vueInstance = createApp({
    ...App,
    data() {
      return props;
    },
  }).mount(container);
  vueInstanceMap.set(container, vueInstance)
}

export async function updated(attrName: string, value: any, container: Element,) {
  const vueInstance = vueInstanceMap.get(container);
  vueInstance[attrName] = value;
  vueInstance.$forceUpdate();
}

export function unmount(magicInstance: any, container: any) {
  const vueInstance = vueInstanceMap.get(container);
  vueInstance.unmount();
}
@divikshrivastava
Copy link

Can I take a stab at this ? @jerryOnlyZRJ @erjanmx @jijiaxin1808 @ChelesteWang @h-a-n-a @0xflotus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants