Introduction
On September 3, 2024, the official Vue team released Vue.js 3.5, a stable version containing no breaking changes but including internal improvements and useful new features. Let’s explore some interesting changes.
Reactive Props Destructuring
Before Vue 3.5, you used toRef()
to destructure props reactively:
|
|
In Vue 3.5, you can directly destructure reactively:
|
|
Access to a destructured variable like count is automatically compiled into props.count by the compiler, so it is tracked on access.
Default Prop Values
Before Vue 3.5, default props values were written like this:
|
|
In Vue 3.5, default prop values can be written like this:
|
|
You can assign default values directly like in JavaScript object destructuring, which is pretty nice!
Obtaining Template References with useTemplateRef()
In Vue 3, you can use ref
for template references to access the DOM and child components, but ref
can be confusing. For example, is a ref
variable reactive data or a DOM element? Vue 3.5 introduces useTemplateRef
, which solves these issues perfectly.
|
|
When a template-bound ref has multiple elements with the same name, useTemplateRef()
returns an array, as shown with the v-for
rendered list.
Teleport Component Improvements
<Teleport>
Specific usage can be found in this article
The <Teleport>
component has a known limitation where its target element must exist when the component is mounted. In Vue 3.5, <Teleport>
introduces a defer prop to mount it after the current render cycle:
|
|
Since <div id="cont"></div>
is rendered after the Teleport, you need the defer prop. If <div id="cont"></div>
is rendered before, defer is not needed.
Generating Unique Application IDs with useId()
useId()
is used to generate unique IDs for each application, ensuring stability across server and client renders. It can be used for generating IDs for form elements and accessibility attributes without causing ID conflicts in SSR applications:
|
|
参考引用
For more updates in Vue 3.5, check out the Announcing Vue 3.5 link for more information.