Accessing props in vue component data function

前端 未结 5 790
醉话见心
醉话见心 2021-02-03 16:59

I am passing a props to a component:





        
相关标签:
5条回答
  • 2021-02-03 17:09

    Note that this does not work if you are using an arrow function for assigning your data:

    data: () => ({
      somevar: this.messageId // undefined
    }),
    

    Because this will not point to the component. Instead, use a plain function:

    data: function() {
      return { somevar: this.messageId }
    },
    

    or using ES6 object method shorthand as Siva Tumma suggested:

    data() {
        return { somevar: this.messageId }
    }
    
    0 讨论(0)
  • 2021-02-03 17:19

    To assign a data property equal to a props, you can use watcher, as following:

    <script>
       export default {
          props: ['messageId'],
          data: function(){
             var theData={
                somevar: "",
                // other object attributes
             }
          },
          watch: {
            messageId: function(newVal) { 
               this.somevar = newVal
            }
          }
       }
    
    0 讨论(0)
  • 2021-02-03 17:21

    I think you have done your solution since the question posted a couple of month earlier. I faced the same problem yesterday, so tried out above solutions without no luck. However, I would like to share an alternative solution for this case that helps someone considerably. watch has some attributes to handle those type of cases. Below scrips shows that how do we accept the props value is data.

    <script>
       export default {
          props: {
                messageId: {
                    type: String,
                    required: true
                }
            }
          data: function(){
             var theData= {
                somevar: "",
                // other object attributes
             }
    
             return theData;
          },
          watch: {
            messageId: {
                    // Run as soon as the component loads
                    immediate: true,
                    handler() {
                        // Set the 'somevar' value as props
                        this.somevar = this.messageId;
                    }
                }
          }
       }
    </script>
    
    0 讨论(0)
  • 2021-02-03 17:31
    <template>
       {{messaged}}
       // other HTML code
    </template>
    
    <script>
       export default {
          props: ['messaged'],
          data: function(){
             return () {
               some_var: this.messaged
             }
          },
          methods: {
          post_order: function () {
            console.log({
              some_var: this.some_var.id
            })
          }
        }
    
       }
    </script>
    
    0 讨论(0)
  • 2021-02-03 17:33

    From the data() method, you can reference the component's properties using this.

    So in your case:

    data: function() {
      var theData = {
        somevar: this.messageId,
        // other object attributes
      }
    
      return theData;
    }
    
    0 讨论(0)
提交回复
热议问题