Vue.js provide / inject stepped pit

Recently learning JavaScript, and use vuejs, the first use of dependency injection, the results of stepped pit, fell almost to the screen. . The assembly can not always get the property, this is always the subject provide this object subcomponents

Slowly I have felt for some vuejs some door doorway Road. . . .

Error Code 1: this object is undefined error

Parent components 
Provide: { 
            . GetCustomerId: the this 
        }, 


subassembly 
Inject: [ '. GetCustomerId' ], 

subassembly call: 
the this .getCustomerId
 // case: getCustomerId is undefined

Error code 2: this object is undefined error

Parent components 
Provide: { 
        . GetCustomerId () { 
            return  this 
            } 
        }, 


subassembly 
Inject: [ '. GetCustomerId' ], 

subassembly call: 
this .getCustomerId
 // case: this is the return of this sub-assembly, in which case the injection is getCustomerId this method, but this method is not declared in the component's methods

Correct codes:

Parent component
provide(){
            return { getCustomerId: this.getCustomerId}
        },
 
 
   methods: {
            getCustomerId(){

            },
      }

Subassembly 
Inject: [ 'getCustomerId' ], 

subassembly call: 
the this .getCustomerId
 // At this time: this time on the right, the injection method is getCustomerId methods defined in the parent components, and Provide () method to change the definition, when performing this method, this object is provide in the parent of this object components,

 

Guess you like

Origin www.cnblogs.com/maoyuanwai/p/12099534.html