Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vue3 ts

作者:似水韶华该惜谁ら   发布日期:2025-03-20   浏览:165

// Vue 3 with TypeScript 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() {
    const state = reactive({ count: 0 });

    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>

解释说明:

  1. 模板部分 (<template>):

    • 使用了简单的 HTML 结构,绑定了一个 message 和一个按钮。按钮点击时调用 increment 方法,并显示当前的 count 值。
  2. 脚本部分 (<script lang="ts">):

    • 引入了 Vue 3 的 defineComponentreactive 函数。
    • 使用 defineComponent 定义组件,确保类型推断正确。
    • setup 函数返回的对象将暴露给模板使用。
    • reactive 创建了一个响应式对象 state,包含 count 属性。
    • increment 方法用于递增 count
  3. 样式部分 (<style scoped>):

    • 添加了一些基本的样式,作用范围仅限于当前组件。

这个示例展示了如何在 Vue 3 中使用 TypeScript 来创建一个简单的计数器应用。

上一篇:vue 组件

下一篇:vue开发

大家都在看

vue.config.js configu

node.js vue

vue查看版本

vue等待几秒

vue3 setup computed

vue screenfull

vue json.stringify

vue 遍历list

typescript vue

vue 复选框

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站