Vue2 implements provide inject to deliver responsiveness

Vue2 implements provide inject to deliver responsiveness

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:
1. @Provide() foo = 'foo'
2. @Provide('bar') baz = 'bar'

Inject method:
1. @Inject() foo: string
2. @Inject('bar') bar: string
3. @Inject(s) baz: string

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:
  • Vue solves the problem of provide and inject response
  • Talk about the detailed application of provide/inject in Vue
  • A brief analysis of the use of provide / inject in Vue
  • Responsive monitoring solution for provide inject in Vue

<<:  Detailed explanation of the solution to the error in creating a user and granting permissions in mysql8.0

>>:  Tutorial on importing and exporting Docker containers

Recommend

Beginners understand MySQL deadlock problem from source code

After many difficult single-step debugging late a...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...

Detailed tutorial of pycharm and ssh remote access server docker

Background: Some experiments need to be completed...

JavaScript implements div mouse drag effect

This article shares the specific code for JavaScr...

Implementation of local migration of docker images

I've been learning Docker recently, and I oft...

Summary of common commands in Dockerfile

Syntax composition: 1 Annotation information 2 Co...

Summary of changes in the use of axios in vue3 study notes

Table of contents 1. Basic use of axio 2. How to ...

Introduction to fuzzy query method using instr in mysql

Using the internal function instr in MySQL can re...

Example code for implementing bottom alignment in multiple ways with CSS

Due to the company's business requirements, t...

3D tunnel effect implemented by CSS3

The effect achievedImplementation Code html <d...

How to get USB scanner data using js

This article shares the specific process of js ob...

W3C Tutorial (16): Other W3C Activities

This section provides an overview of some other i...

Nginx configuration based on multiple domain names, ports, IP virtual hosts

1. Type introduction 1.1 Domain-based virtual hos...