Avoid mutating a prop directly since the value will be overwritten

前端 未结 11 887
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 01:07

I have very common problem with upgrading to Vue 2.0

I am getting warning:

Avoid mutating a prop directly since the value will be overwritte

相关标签:
11条回答
  • 2020-12-10 01:57
    var GuestMenu = Vue.extend({
       props : {
        uNpW:{type:Object}
       }
          template: `
            <div id="auth">
                <form class="form-inline pull-right">
                    <div class="form-group">
                        <label class="sr-only" for="UserName">User name</label>
                      <input type="username" v-model="uNpW.username" class="form-control" id="UserName" placeholder="username">
                    </div>
                    <div class="form-group">
                      <label class="sr-only" for="Password">Password</label>
                      <input type="password" v-model="uNpW.password" class="form-control" id="Password" placeholder="Password">
                    </div>
                </form>
            </div>`,
        });
    
    App = new Vue ({ 
       el: '#app',
      data: 
        {
          topMenuView: "guestmenu",
          contentView: "guestcontent",
          unAndPw:{username: "",password: ""}
    
        }
    })
    
    in main html
    <guest-menu :uNpW=unAndPw> </guest-menu>
    

    you dont need emit or any other thing

    0 讨论(0)
  • 2020-12-10 01:58

    From Vue 2.3.0 on you can use the .sync modifier:

    Sample from https://vuejs.org/v2/guide/components-custom-events.html#sync-Modifier:

    <text-document :title.sync="title"></text-document>
    

    and in your controller...

    this.$emit('update:title', newTitle)
    
    0 讨论(0)
  • 2020-12-10 01:59

    Follow this very easy instructions:

    //Parent Component
    <InsertForm :formSchema="formSchema" v-on:getFormSchema="setFormSchema"></InsertForm>
    <script>
    import InsertForm from "./insertForm.vue"; //select file
    
    export default {
    components: { InsertForm },
    
    data: () => ({
        formSchema:{
           id:'',
           webUrl:''
        },
    }),
    
    methods: {
          setFormSchema(data)
          {
            this.formSchema = data.formSchema;
          }
        }
    }
    </script>
    
    // From Child Component. That means from insertForm.vue file
    
    <script>
    
    export default {
        props: ["formSchema"],
        data: () => ({
    
        }),
    }
    //Where you need
    this.$emit("getFormSchema", {'formSchema':{"id":1,"webUrl":"bdprescription.com"}});
    </script>
    
    0 讨论(0)
  • 2020-12-10 02:04

    Fast solution if your prop is an object.

    You can avoid using $emit or getting that error by using Object.assign() in Javascript. This is going to work the same as v-model attribute.

    example:

    // Update the user
    Object.assign(this.userProp, user);
    
    0 讨论(0)
  • 2020-12-10 02:06

    A computed property with appropriate get and set worked for me:

    computed: {
      dialogDataProp: {
          get: function() {
            return this.dialog;
          },
          set: function() {}
        }
    }
    

    Code above for toggling a dialog box.

    0 讨论(0)
提交回复
热议问题