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

Props can be provided with a setter. #175

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

infinnie
Copy link

@ycmjason
Copy link

I'm totally loving this idea.

@ycmjason
Copy link

On second thought, it will be quite weird if I did props.x = 3 but it doesn't actually set x to 3.

@phiter
Copy link

phiter commented May 28, 2020

I like the idea because I see myself doing this quite often:

<dialog v-model="isOpen" />

props: {
    open: {
        type: Boolean,
        default: false,
    }
},
computed: {
    isOpen: {
        get() {
            return this.open;
        },
        set(v) {
            this.$emit('update:open', v);
        }
    }
}

Adding a setter to the props would definitely reduce this boilerplate that I have to use for some wrapper components.

@pikax
Copy link
Member

pikax commented Jun 5, 2020

if using composition-api you can have a helper to handle that:

function useVModel(props, name, emit){
return computed({ 
  get(){
    return props[name]
  },
  set(v){
    emit('update:'+name, v);
  }
})
}

defineComponent({
      template: `<div>
      <label :for="child-input">Update value</label>
      <input name="child-input" v-model="value">
      </div>`,
      props: {
        value: String
      },
      setup(props, {emit}) {
        const value = useVModel(props, "value", emit);

        return {
          value
        };
      }
    })

With typescript

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

Successfully merging this pull request may close these issues.

None yet

4 participants