Data () {..} returns an object or does not return an object

Time:2021-2-23

Write one, return an object

data() { return {
    req: {}
}}

Write method 2, do not return the object, directly mount to this

data() {
    this.req= {}
}

The above two methods can get req objects, but the differences at least include:
1. The req obtained by writing method 1 is responsive, while the req obtained by writing method 2 is nonresponsive;
2. When multiple mixins are mixed, writing method 2 may need to consider the sequence, and an error may be reported if the sequence is reversed.

//In writing method 2, the order of multiple mixins should be considered. If the order is reversed, an error may be reported
//The execution order is from right to left, and it may be used mixins.pop ()
mixins: [mixinReq, mixinDb], 
//mixins: [mixinDb, mixinReq],

When multiple mixins are mixed, it is a good idea to always let the data() function return an object. Even if an empty object is returned, in some cases, the error can be avoided. Therefore, when you do not need responsive data, you can use a writing method similar to the following

data() {
    this.req=  {} // no responsive data is required
    Return {} // always returns an empty object
}

This work adoptsCC agreementReprint must indicate the author and the link of this article