簡體   English   中英

Vue.js 將 object 字段與 object 屬性裝飾器綁定

[英]Vue.js bind object field with object property decorator

我想使用vue-property-decorator綁定 object 的字段。 以下代碼應說明我要實現的目標:

<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>

textarea中的描述已正確初始化,但一旦我開始編輯文本,綁定似乎不起作用。 我需要與這個問題相同的解決方案,但使用vue-property-decoratorVue.js bind object properties

我相信這個問題是因為你試圖改變一個道具。 請嘗試以下操作:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM