<template>
<div class="my-component">
<h1>{{ message }}</h1>
<button @click="increment">点击我</button>
<p>当前计数: {{ count }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue';
// 定义一个响应式变量
const message = ref('Hello Vue 3!');
const count = ref(0);
// 定义一个方法
function increment() {
count.value++;
}
</script>
<style scoped>
.my-component {
text-align: center;
margin-top: 50px;
}
button {
padding: 10px 20px;
font-size: 16px;
}
</style>
模板部分 (<template>):
div 元素包裹整个组件的内容。{{ message }} 来绑定 message 变量的值。@click 事件,点击时调用 increment 方法。脚本部分 (<script setup>):
<script setup> 语法糖,简化了组件的定义。ref 函数来创建响应式变量 message 和 count。increment 方法,用于增加 count 的值。样式部分 (<style scoped>):
scoped 样式,确保样式只作用于当前组件。上一篇:vue3 i18n
下一篇:vue预览pdf
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站