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

Dynamic Components support #126

Closed
fiti128 opened this issue Oct 7, 2020 · 2 comments
Closed

Dynamic Components support #126

fiti128 opened this issue Oct 7, 2020 · 2 comments

Comments

@fiti128
Copy link

fiti128 commented Oct 7, 2020

  1. What is the recommended way to use dynamic component in JSX? In my case it is reading component names from configuration and use it in JSX.
  2. Do you plan to add/replicate previous functionality

In vue2 with composition-api: I have the following (simplified) code:

export default defineComponent({
  name:  "c-item-description",
  props: {
    item:                  {
      type:    Object,
      default: () => ({}) 
    },
    presentationComponent: String,
  },
  setup(props, context) {
    const { dynamicComponentRef} = useItemDescriptionConfiguration(props);
    return () => {
      const DynamicComponent: any = dynamicComponentRef.value || "t-item-description";
      return (
        <keep-alive>
          <DynamicComponent
            item={props.item}
          />
        </keep-alive>
      );
    };
  }
});

I found a workaround. Is it supposed to be so?

import { defineComponent, computed, ref,getCurrentInstance } from 'vue';
export default defineComponent({
  name:  "tsx-world",
  props: {
    msg: String
  },
  setup(props, context) {
    const dynamicComponent = ref("local-title"); // read from configuration
    const myTitleRef = computed(()=>getCurrentInstance()?.appContext?.components[dynamicComponent.value]);
    return () => {
      const myTitle = myTitleRef.value;
      return (
        <myTitle
          msg="dynamic component"
        />
    );}
  }
});
@Amour1688
Copy link
Member

Amour1688 commented Oct 12, 2020

try

- const DynamicComponent: any = dynamicComponentRef.value || "t-item-description";
+ const DynamicComponent: any = dynamicComponentRef.value || resolveComponent("t-item-description");

https://v3.vuejs.org/api/global-api.html#resolvecomponent

@fiti128
Copy link
Author

fiti128 commented Oct 12, 2020

Issue is resolved. Thank you.

@fiti128 fiti128 closed this as completed Oct 12, 2020
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