Vue3 expose() Function
In Vue 3,expose() The function is a tool used in the Composition API to expose a component's internal methods and properties.
expose() It is typically used together with the setup() function, allowing developers to explicitly specify which internal properties and methods of the component can be accessed externally.
Why Do We Need the expose() Function?
In Vue 2, a component's properties and methods are public by default and can be accessed directly from outside. However, this design can lead to some problems, for example:
- Security Issues: Internal methods being exposed accidentally may lead to unsafe operations.
- Maintainability Issues: It is hard to track which properties and methods are used externally, increasing maintenance complexity.
Vue 3 introduced the expose() function, allowing developers to control a component's public interface more precisely, thereby solving the above problems.
How to Use the expose() Function?
Basic Syntax
Example
export
default
{
setup
(
props
,
context
)
{
const
internalMethod
=
(
)
=>
{
console.
log
(
'This is an internal method'
)
;
}
;
const
publicMethod
=
(
)
=>
{
console.
log
(
'This is a public method'
)
;
}
;
// Expose the publicMethod
context.
expose
(
{
publicMethod
}
)
;
return
{
internalMethod
}
;
}
}
;
Explanation
In the code above, we defined two methods:internalMethod and publicMethod. Through context.expose(), we only exposed publicMethod, while internalMethod will not be accessible from outside.
Example Scenarios
1. Parent-Child Component Communication
Suppose we have a parent component and a child component, and the parent needs to call a certain method of the child.
Child component
Example
export
default
{
setup
(
props
,
context
)
{
const
childMethod
=
(
)
=>
{
console.
log
(
'Child method called'
)
;
}
;
context.
expose
(
{
childMethod
}
)
;
return
{
}
;
}
}
;
Parent component
Example
import
{
ref
,
onMounted
}
from
'vue'
;
import
ChildComponent from
'./ChildComponent.vue'
;
export
default
{
components
:
{
ChildComponent
}
,
setup
(
)
{
const
childRef
=
ref
(
null
)
;
onMounted
(
(
)
=>
{
if
(
childRef.
value
)
{
childRef.
value
.
childMethod
(
)
;
}
}
)
;
return
{
childRef
}
;
}
}
;
2. Encapsulating a Component Library
When developing a component library, we may want to expose only a portion of methods and properties to maintain the component's encapsulation and security.
Example
export
default
{
setup
(
props
,
context
)
{
const
internalState
=
ref
(
'internal state'
)
;
const
publicState
=
ref
(
'public state'
)
;
const
publicMethod
=
(
)
=>
{
console.
log
(
'Public method called'
)
;
}
;
context.
expose
(
{
publicState
,
publicMethod
}
)
;
return
{
internalState
}
;
}
}
;
Notes
- Calling expose() Multiple Times: If called multiple times
expose(), only the content of the last call will be exposed. - Using with ref: In the parent component, you can use
refto reference the child component and call its exposed methods. - Compatibility:
expose()is a new feature in Vue 3; ensure your project uses Vue 3 or a later version.
Vue3 Options API