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
Preface There are many ways to center horizontall...
When developing a website, you often need to use ...
Preface After a long time of reading various mate...
In the Linux environment, you want to check wheth...
The operating environment of this tutorial: Windo...
Today, when I was looking at the laboratory proje...
This article example shares the specific code of ...
Table of contents 1. Reduce the number of image l...
This article mainly introduces an example of impl...
1. Command Introduction The chkconfig command is ...
Search Page: search.wxml page: <view class=&qu...
There is such a scenario: a circular container, t...
Table of contents Preface 1. Introduction to Axio...
Currently, almost all large websites and applicat...
Table of contents Discover: Application of displa...