1. Conventional writing in vue2// The parent component provides 'foo' var Provider = { data(){ return { foo: 'bar' } } provide: fooProvide: this.fooFn // pass a reference type function}, methods:{ fooFn() { return this.foo } } } var Child = { inject: ['fooProvide'], computed:{ fooComputed(){ return this.fooProvide() // Because the function passed in is a reference type} } created () { console.log(this.fooComputed) } // ... } 2. The unconventional way of writing in vue2, but comfortable to use. (The usage is probably the same, but the value passed becomes this--> the entire instance)// The parent component provides 'foo' var Provider = { data(){ return { foo: 'bar', other:'...' } } provide: app: this // pass the entire this past}, mounted(){ const that = this setTimeout(()=>{ that.foo = 'Change value' },4000) } } var Child = { inject: ['app'], created () { console.log(this.app.foo) // Everything below this.app is responsive because they are all references to the same instance} // ... } 3. vue2 + ts (because if you haven't used ts before, you really don't know how to use it, so here's an example) Provide method: Inject method: Example: // The parent component provides 'fooProvide' @Provide('fooProvide') // Name it whatever you want, just pass the same value as the one you receive. But generally keep it the same as the variable below fooProvide = this.refreshNumFn // Variable receives the value to be passed refreshNumFn() { return this.refreshNum } // Child component receives @Inject('fooProvide') fooProvide: any get valueA(): any { return this.fooProvide() } mounted(){ console.log(this.valueA) // ... } This is the end of this article about vue2's implementation of provide inject to deliver responsiveness. For more related vue2 provide inject content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: Tutorial on importing and exporting Docker containers
After many difficult single-step debugging late a...
Table of contents cause reason Introduction to NP...
Background: Some experiments need to be completed...
This article shares the specific code for JavaScr...
I've been learning Docker recently, and I oft...
Table of contents Overview in operator refinement...
Syntax composition: 1 Annotation information 2 Co...
Table of contents 1. Basic use of axio 2. How to ...
Using the internal function instr in MySQL can re...
Better-scroll scrolling principle As a parent con...
Due to the company's business requirements, t...
The effect achievedImplementation Code html <d...
This article shares the specific process of js ob...
This section provides an overview of some other i...
1. Type introduction 1.1 Domain-based virtual hos...