Skip to content

Props with Vue3 and Typescript #86

@Lalaluka

Description

@Lalaluka

Hi,
I had a go on migrating some of our Vue2 Microfrontends to Vue3.
Also, I threw Typescript into the mix.

I'm not sure if it's an issue with the Package itself or just a missing bit of the documentation.

I encountered a problem with the usage of props. The SingleSpaOptsVue3 type doesn't have the props defined to typescript won't compile it. This is of course Typescript exclusive

const vueLifecycles = singleSpaVue({
  createApp,
  appOptions: {
    render() {
      return h(App, {
        // single-spa props are available on the "this" object. Forward them to your component as needed.
        // https://single-spa.js.org/docs/building-applications#lifecyle-props
        // if you uncomment these, remember to add matching prop definitions for them in your App.vue file.
        /*
        name: this.name,
        mountParcel: this.mountParcel,
        */
        singleSpa: this.singleSpa,
        //Property 'singleSpa' does not exist on type 'AppOptions | ((opts: SingleSpaOptsVue2 | SingleSpaOptsVue3, props: object) => Promise<AppOptions>)'.
        //Property 'singleSpa' does not exist on type '(opts: SingleSpaOptsVue2 | SingleSpaOptsVue3, props: object) => Promise<AppOptions>'.ts(2339)
      });
    },
  },
});

It can easily be fixed with an this alis (which is kind of ugly and talint doesn't like):

const vueLifecycles = singleSpaVue({
  createApp,
  appOptions: {
    render() {
      const thisalias: any = this;
      return h(App, {
        // single-spa props are available on the "this" object. Forward them to your component as needed.
        // https://single-spa.js.org/docs/building-applications#lifecyle-props
        // if you uncomment these, remember to add matching prop definitions for them in your App.vue file.
        /*
        name: this.name,
        mountParcel: this.mountParcel,
        */
        singleSpa: thisalias.singleSpa,
      });
    },
  },
});

Using appOptions as an async function works of course (with some types on the props).

const vueLifecycles = singleSpaVue({
  createApp,
  async appOptions(props: any) {
    return {
      render: h(App, {
        singleSpa: props.singleSpa,
      })
    }
  },
});

Maybe this should be described a bit better in the Docs? Or also improved in the generation? The example Comment suggesting to uncomment the Props is a bit wrong if Typescript is in place.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions