【问题标题】:vue-property-decorator: Prop being mutated?vue-property-decorator:道具正在变异?
【发布时间】:2019-02-27 02:35:18
【问题描述】:

我使用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>

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 1970-01-01
      • 2021-06-04
      • 2019-05-19
      • 2020-12-13
      • 2019-09-08
      • 2020-09-15
      • 2020-11-02
      相关资源
      最近更新 更多