// Vue 3 + TypeScript 示例代码
<template>
<div id="app">
<h1>{{ message }}</h1>
<button @click="increment">Count is: {{ count }}</button>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
export default defineComponent({
name: 'App',
setup() {
const message = ref('Hello Vue 3 with TypeScript!');
const count = ref(0);
const increment = (): void => {
count.value++;
};
return {
message,
count,
increment,
};
},
});
</script>
<style scoped>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
模板部分 (<template>
):
{{ message }}
和 {{ count }}
,分别用于显示消息和计数器的值。@click="increment"
,当按钮被点击时调用 increment
方法。脚本部分 (<script lang="ts">
):
defineComponent
来定义组件,并使用 ref
创建响应式数据 message
和 count
。increment
,每次点击按钮时会增加 count
的值。样式部分 (<style scoped>
):
scoped
属性确保这些样式只应用于当前组件。上一篇:vue store
下一篇:vue foreach
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站