// Vue 3 + TypeScript 示例代码
// 1. 安装 Vue CLI (如果还没有安装)
// npm install -g @vue/cli
// 2. 创建一个新的 Vue 项目
// vue create my-project
// 在创建过程中选择 TypeScript 支持
// 3. 编写一个简单的 Vue 组件
<template>
<div id="app">
<h1>{{ message }}</h1>
<button @click="increment">点击我</button>
<p>计数: {{ count }}</p>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
export default defineComponent({
name: 'App',
setup() {
// 使用 ref 创建响应式数据
const message = ref('Hello Vue 3 with TypeScript!');
const count = ref(0);
// 定义方法
const increment = () => {
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
的值,并绑定了一个按钮点击事件来调用 increment
方法。脚本部分 (<script lang="ts">
):
defineComponent
是 Vue 3 提供的一个函数,用于定义组件,确保类型推断更准确。ref
是 Vue 3 中用于创建响应式数据的函数,返回一个包含 .value
属性的对象。setup
函数是 Composition API 的入口,返回的对象中的属性和方法可以直接在模板中使用。样式部分 (<style scoped>
):
上一篇:vue 生成uuid
下一篇:vue 引入图片
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站