Vue 3 app.config Property: A Deep Dive into Global Configuration
app.config is a very important global configuration object that allows you to make various settings and configurations at the application level.
1. What is app.config?
In Vue 3, when you create an application instance, you can do so via createApp function. The application instance returned by this function contains a config property, namely app.config. This object is used to store and configure the application's global settings.
Example
import
{
createApp
}
from
'vue'
;
const
app
=
createApp
(
{
}
)
;
2. Commonly Used app.config Properties
app.config provides several properties for configuring the application's global behavior. Here are some commonly used properties and their descriptions:
2.1. app.config.errorHandler
errorHandler is a global error-handling function. When any component in the application throws an error, this function is called.
Example
app.
config
.
errorHandler
=
(
err
,
vm
,
info
)
=>
{
console.
error
(
'Error:'
,
err
)
;
console.
log
(
'Component:'
,
vm
)
;
console.
log
(
'Info:'
,
info
)
;
}
;
In this example, when a component in the application throws an error, the error message, component instance, and extra information are printed to the console.
2.2. app.config.warnHandler
warnHandler is a global warning-handling function. When Vue detects a potential problem, this function is called.
Example
app.
config
.
warnHandler
=
(
msg
,
vm
,
trace
)
=>
{
console.
warn
(
'Warning:'
,
msg
)
;
console.
log
(
'Component:'
,
vm
)
;
console.
log
(
'Trace:'
,
trace
)
;
}
;
Through customizing warnHandler, you can better control and record warning messages in the application.
2.3. app.config.performance
performance is a boolean used to enable or disable Vue's performance tracking. When set to true , Vue records component rendering performance information.
Example
app.
config
.
performance
=
true
;
In a development environment, enabling performance tracking helps you analyze and optimize component rendering performance.
2.4. app.config.globalProperties
globalProperties is an object used to add global properties or methods that can be accessed in any component instance.
Example
app.
config
.
globalProperties
.$myGlobalMethod
=
function
(
)
{
console.
log
(
'This is a global method'
)
;
}
;
In components, you can use this.$myGlobalMethod() to call this global method.
2.5. app.config.isCustomElement
isCustomElement is a function used to tell Vue which elements are custom elements (i.e., non-Vue components).
Example
app.
config
.
isCustomElement
=
(
tag
)
=>
tag.
startsWith
(
'my-'
)
;
In this example, all tags starting with my- are recognized by Vue as custom elements rather than Vue components.
3. Practical Application Scenarios
3.1. Global Error Handling
In large applications, global error handling is very useful. Through app.config.errorHandler, you can centrally handle all uncaught errors and send error information to the server for logging.
3.2. Global Properties and Methods
app.config.globalProperties can be used to add global utility methods or configuration items. For example, you can add a global version number or utility function to share across all components.
3.3. Custom Element Support
If your application uses custom elements (such as Web Components), you can use app.config.isCustomElement to tell Vue that these elements should not be treated as Vue components.
Vue 3 Global API