import { watchEffect } from 'vue' const { count } = defineProps(['count']) watchEffect(() => { // 每当父组件中的 count 属性发生变化时,这里就会记录 console.log(count) })尤雨溪说:“几乎每一个在实际项目中大规模使用响应式属性解构的开发者都反馈良好。他们表示喜欢这个特性,希望看到这个特性被稳定下来。”
// vite.config.js export default { plugins: [ vue({ script: { propsDestructure: false } }) ] } useTemplateRef <script setup> // 首先定义一个值为 undefined 或 null 的 ref,并随意命名结果变量 const divEl = ref(); </script> <template> <!-- 然后在模板中的某个地方使用与 `ref` 属性值相同的名称 --> <div ref="divEl" ></div> </template>这种方法存在两个问题:
2.此外,这限制了你只能在组件的 script 里定义模版的 refs。这意味着自定义 composables 想要访问 DOM 元素的话,必须接受一个 template 引用作为参数。
// MyComposable export const useMyComposable = (options = { templateRef: 'el' })=>{ // 由于函数名称的关系,很明显这是一个模板引用👇 const theEl = useTemplateRef(options.templateRef); } // MyComponent <script setup> // 堆代码 duidaima.com // 无需在组件中定义模板引用,这可以是组合函数的工作 useMyComposable() useMyComposable({ templateRef: 'el2' }) </script> <template> <div ref="el"></div> <div ref="el2"></div> </template>useId
<!--MyCustomInput--> <script setup> defineProps({ label: String help: String //... }) const inputId = uesId(); const helpTextId = useId(); </setup> <template> <label :for="inputId">{{label}}</label> <input :id="inputId" :aria-describedby="helpTextId"/> <p :id="helpTextId">{{ help }}</p> </template>内存改进 === 更快的应用
2.有选择地允许水合不匹配[4],使处理在客户端和服务器之间永远不会相同的数据(如日期)变得更容易。