0

I want to bind the field of an object using the vue-property-decorator. The following code should illustrate what I am trying to achieve:

<template>
  <textarea v-model="this.box.description" placeholder="The description for the box"></textarea>
</template>

<script lang="tsx">
import { Component, Prop, Vue } from 'vue-property-decorator';

export default class BoxScreen extends Vue {
  @Prop() private box!: Box;

  public created() {
    // init box with an Axios request
  }
}
</script>

The description within the textarea gets properly initialized, but the binding doesn't seem to work once I start editing the text. I need the same solution as for this question, but working with vue-property-decorator: Vue.js bind object properties.

4
  • 1
    Try with PropSync Commented Jan 31, 2021 at 15:29
  • Could you be more specific? Commented Jan 31, 2021 at 17:42
  • Does box really need to be a public prop (set by a parent component)? It looks like you just initialize it within the component, so it could probably be private. If that's the case, you could just use a normal class field, and your v-model would work as you expected in your example. Commented Jan 31, 2021 at 18:20
  • github.com/kaorun343/vue-property-decorator#PropSync was saying to use this property as it similar to computed one which results in reactive state Commented Feb 1, 2021 at 12:30

1 Answer 1

0

I believe the issue is because you're trying to mutate a prop. Try the following instead:

<template>
  <textarea
    v-model="description"
    placeholder="The description for the box"
  ></textarea>
</template>

<script lang="tsx">
import { Component, Prop, Watch, Vue } from 'vue-property-decorator';

export default class BoxScreen extends Vue {
  @Prop() private box!: Box;

  description: string;

  @Watch('box.description', { immediate: true })
  onChangeBoxDescription(): void {
    this.description = this.box.description;
  }

  public created() {
    // init box with an Axios request
  }
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I need to make sure that this.box.description is always in sync with the textarea. It seems with your solution the textarea will be updated when the box description changes, but not the other way around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.