简体   繁体   中英

Passing VueJS Prop into TypeScript class component

I have a simple typescript component class that requires one prop:

export default class ColorsPallet extends Vue {
        @Prop({type: String, required: true}) readonly name!: string;
        private readonly view: StorageItem;
        private readonly stored: StorageItem;
        private readonly colors: ColorItems;

        constructor() {
            super();

            this.colors = db.colors[this.name]['items'];
            this.storedColor = new StorageItem(this.name + '-stored-color', localStorage, db.colors[this.name]['default']);
            this.viewColor = new StorageItem(this.name + '-view-color', sessionStorage, this.storedColor.get());
        }
}

I would love to initialize this class in a different typescript component class to get a specific instance variable:

constructor() {
    super();

    const colors = new ColorsPallet();
    console.log(color.$data.viewColor.get());
}

This gives me an obvious error:

[Vue warn]: Missing required prop: "name"

(found in <Root>)

So I changed the initialization to:

const colors = new ColorsPallet({props: ['name']})

That still gives me a type error as I am not really passing anything:

[Vue warn]: Error in data(): "TypeError: Cannot read property 'items' of undefined"

(found in <Root>)

Without requiring a prop this code works perfectly for me in different cases. However I am not able to get this working with passing down a prop. How do I go about it?

Edit:

Passing prop like this also does not work:

const colors = new ColorsPallet({
  name: 'foo'
})

Results into this error:

TS2345: Argument of type '{ props: { name: string; }; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.   Types of property 'props' are incompatible.     Type '{ name: string; }' is not assignable to type 'string[] | RecordPropsDefinition<Record<string, any>> | undefined'.       Type '{ name: string; }' is not assignable to type 'undefined'.

The type of required format:

/**
 * This type should be used when an array of strings is used for a component's `props` value.
 */
export type ThisTypedComponentOptionsWithArrayProps<V extends Vue, Data, Methods, Computed, PropNames extends string> =
  object &
  ComponentOptions<V, DataDef<Data, Record<PropNames, any>, V>, Methods, Computed, PropNames[], Record<PropNames, any>> &
  ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Record<PropNames, any>>>>;

One solution would be to set a default value:

@Prop({type: String, required: true, default: 'foo'}) readonly name!: string;

However, in your above example I believe you would need:

const colors = new ColorsPallet({
  propsData: {
    name: 'foo'
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM