// TypeScript with Vue 3 Example
<template>
<div id="app">
<p>{{ message }}</p>
<button @click="increment">Count is: {{ state.count }}</button>
</div>
</template>
<script lang="ts">
import { defineComponent, reactive } from 'vue';
export default defineComponent({
name: 'App',
setup() {
// Reactive state
const state = reactive({ count: 0 });
// Methods
function increment() {
state.count++;
}
return {
state,
increment,
message: 'Hello Vue 3 with TypeScript!'
};
}
});
</script>
<style scoped>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
模板部分 (<template>):
message 和 state.count 的值,并绑定了一个按钮的点击事件。脚本部分 (<script>):
setup 函数)。reactive 用于创建响应式对象 state,其中包含一个 count 属性。increment 方法用于增加 count 的值。defineComponent 是 Vue 3 提供的一个工具函数,用于定义组件并确保类型推断正确。样式部分 (<style scoped>):
scoped 属性确保样式只应用于当前组件。上一篇:vue 获取元素宽度
下一篇:npm create vue
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站