// Vue3 + TypeScript 示例代码
<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() {
// 定义一个响应式的字符串变量
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>):
<h1>)、一个按钮 (<button>) 和一个段落 (<p>)。{{ message }} 和 {{ count }} 是 Vue 的插值语法,用于显示响应式数据。脚本部分 (<script lang="ts">):
defineComponent 来定义一个 Vue 组件,并使用 TypeScript 类型推断。ref 是 Vue 3 中的一个函数,用于创建响应式引用。message 和 count 都是响应式变量。increment 方法用于增加 count 的值。setup 函数返回的对象决定了哪些属性和方法可以在模板中使用。样式部分 (<style scoped>):
scoped 属性确保这些样式只应用于当前组件。上一篇:vue3 跳转路由
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站