B
B
BonBon Slick2021-06-30 00:12:37
typescript
BonBon Slick, 2021-06-30 00:12:37

Default parameters in a method when passing an object?

[ADD_ITEMS](state, data: {items: object, addToTop?: boolean}) : void{

vuex takes parameters as the second mutation parameter, that is, when there are several of them, it is necessary to group them into an object.
However, when passed as an object, default parameters cannot be accessed
https://www.typescripttutorial.net/typescript-tuto...
https://www.typescriptlang.org/docs/handbook/funct...

[ADD_ITEMS](state, items: object, addToTop?: boolean) : void{

this won't work because the 3rd parameter is ala underhood type via vuex tricks hooks.

If any solution other than this?
[ADD_ITEMS](state, data: {items: object, addToTop?: boolean}) : void{
        let items = data.items ?? {};
        let  addToTop = data.addToTop ?? false;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-06-30
@BonBonSlick

However, when passed by an object, the default parameters cannot be networked.
who told you that?
[ADD_ITEMS](state, {items = {}, addToTop = false}: {items?: object; addToTop?: boolean} = {}): void {
    // ...
},
https://www.typescriptlang.org/play?#code/MYewdgzg...
PS don't use the object type, it's an extremely useless type

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question