vue-property-decorator:道具被变异了吗?

时间:2018-09-23 09:28:01

标签: javascript vue.js

我使用vue-property-decorator,它是一个简单的组件,却收到错误消息:

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "message"

此消息是什么意思?以及我该如何解决?

这是我的代码,例如:

<template>
    <v-layout row justify-center>
        <v-dialog v-model="dialog">........</v-dialog>
    </v-layout>
</template>

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

@Component({})
export default class SomeModal extends ... {

  @Prop() public dialog?: boolean;
  @Prop() public message?: string;

  constructor() {
    super();
  }

  public showError(er) {
    this.message = er.message;
    this.dialog = true;
  }
}
</script>

<style scoped lang="scss">
</style>

1 个答案:

答案 0 :(得分:1)

我不使用vue的这种语法,但是消息很清楚:您需要定义一个data属性或一个计算变量。这意味着:

data: {
    dialogData: ''
}

constructor() {
    super();
    this.dialogData = this.dialog;
}

或:

computed: {
    dialogData() {
        return this.dialog;
    }
}

有关计算属性,请参见vuejs文档。

编辑:使用vue-property-decorator,可能是:

@Component
export default class YourComponent extends Vue {
  // your code here...
  private _dialogData: string = '';

  constructor() {
      super();
      this._dialogData = this.dialog;
  }
}