Vue VueUse-基于Vue组合式API的实用工具集 - 附完整示例
warning:
这篇文章距离上次修改已过202天,其中的内容可能已经有所变动。
<template>
<div>
<h1>VueUse 示例</h1>
<p>{{ count }}</p>
<button @click="increment">增加</button>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
import { useCounter } from '@vueuse/core';
// 创建响应式引用
const counter = ref(0);
// 使用VueUse提供的useCounter函数
const { count, increment, decrement } = useCounter(counter);
</script>
这个例子展示了如何在Vue应用中使用VueUse库的useCounter
函数来创建一个简单的计数器。ref
用于创建响应式的基本数据类型,useCounter
是VueUse库提供的一个函数,它接受一个ref作为参数,并返回一个带有count
、increment
和decrement
方法的对象。在模板中,我们可以直接使用这些方法和变量来显示计数器的值和响应用户的点击事件来增加或减少计数。
评论已关闭