Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 app.provide() Function

Vue 3 app.provide() Function


Vue3 Global API Vue 3 Global API

app.provide() is a very important function that allows us to provide data or methods at the top level of the application, so they can be accessed and used in any component throughout the application.

Pass app.provide() and inject combination, we can easily share data between multiple components, avoiding the tedious passing of props and events.


1. What is the app.provide()?

app.provide() is a dependency injection mechanism provided in Vue 3. Through it, we can define some global data or methods at the top level of the application, and then in any child component retrieve inject those data or methods. This approach is ideal for sharing data or functionality between multiple components without passing them layer by layer through props or events.

1.1 Why Do We Need app.provide()?

In large Vue applications, we often need to share data or methods between multiple components. Passing these via props or events makes the code complex and hard to maintain. Whereas app.provide() provides a more concise and efficient way to solve this problem.


2. app.provide() Basic syntax of

app.provide() is very simple; it accepts two parameters:

Example

app.

provide

(

key

,

value

)


3. How to Use app.provide()?

3.1 Provide Data or Methods at the Application Top Level

First, we need to use app.provide() to provide data or methods. Usually in main.js file we configure:

Example

import

{

createApp

}

from

'vue'

import

App from

'./App.vue'

const

app

=

createApp

(

App

)

// provide global data

app.

provide

(

'message'

,

'Hello, Vue3!'

)

// provide global method

app.

provide

(

'logMessage'

,

(

msg

)

=>

{

  console.

log

(

msg

)

}

)

app.

mount

(

'#app'

)

In the code above, we used app.provide() to provide a string 'Hello, Vue3!' and a method logMessage.

3.2 Use in Child Components inject Retrieve data or methods

In child components, we can use inject to retrieve the data or methods provided at the application top level. For example:

Example

<

template

<

div

<

p

{

{

message

}

}

p

<

button

@

click

=

"log"

Click me

button

 

div

template

<

script

export

default

{

inject

:

[

'message'

,

'logMessage'

]

,

  methods

:

{

    log

(

)

{

     

this

.

logMessage

(

'Button clicked!'

)

   

}

 

}

}

script

In this component, we used inject to retrieve message and logMessageand used them in the template and methods.


4. Practical Application Example

Suppose we are developing a user management system and need to share the current user's information across multiple components. We can use app.provide() to provide user information and retrieve and use it in the components that need it.

4.1 Provide User Information at the Application Top Level

Example

import

{

createApp

}

from

'vue'

import

App from

'./App.vue'

const

app

=

createApp

(

App

)

// provide current user information

app.

provide

(

'currentUser'

,

{

  name

:

'Zhang San'

,

  age

:

25

,

  email

:

'zhangsan@example.com'

}

)

app.

mount

(

'#app'

)

4.2 Use User Information in Child Components

Example

<

template

<

div

<

h1

User information

h1

   

<

p

Name

:

{

{

user.

name

}

}

p

   

<

p

Age

:

{

{

user.

age

}

}

p

   

<

p

Email

:

{

{

user.

email

}

}

p

 

div

template

<

script

export

default

{

inject

:

[

'currentUser'

]

,

  computed

:

{

    user

(

)

{

     

return

this

.

currentUser

   

}

 

}

}

script

Through this approach, we can easily share and use user information across multiple components without passing it through props every time.