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

Teach you how to achieve vertical centering elegantly (recommended)

Preface There are many ways to center horizontall...

How to create a database in navicat 8 for mysql

When developing a website, you often need to use ...

How to use selenium+testng to realize web automation in docker

Preface After a long time of reading various mate...

Detailed explanation of how to upgrade software package versions under Linux

In the Linux environment, you want to check wheth...

Slot arrangement and usage analysis in Vue

The operating environment of this tutorial: Windo...

How to query the minimum available id value in the Mysql table

Today, when I was looking at the laboratory proje...

Vue implementation counter case

This article example shares the specific code of ...

An example of implementing a simple finger click animation with CSS3 Animation

This article mainly introduces an example of impl...

Use of Linux chkconfig command

1. Command Introduction The chkconfig command is ...

WeChat applet implements search function and jumps to search results page

Search Page: search.wxml page: <view class=&qu...

Axios project with 77.9K GitHub repository: What are the things worth learning?

Table of contents Preface 1. Introduction to Axio...

Research on the effect of page sidebar realized by JS

Table of contents Discover: Application of displa...