当前位置: 首页 > news >正文

【TypeScript】ts在vue中的使用

目录

一、Vue 3 + TypeScript 

1. 项目创建与配置

项目创建

 关键配置文件

2.完整项目结构示例

3. 组件 Props 类型定义

4. 响应式数据与 Ref

5. Composition 函数复用

二、组件开发

1.组合式API(Composition API)

2.选项式API(Options API)

三、Vue 2 + TypeScript

1. 安装依赖

2. 类组件(Vue Class Component)

3.Vuex类型安全

四、状态管理(Pinia + TypeScript)

1. 定义 Store

2. 在组件中使用

五、高级类型操作

1.全局属性扩展

2. 为无类型库添加声明

3.泛型组件

4.使用 Vue Router

六、最佳实践

严格类型检查:

类型导入:

避免 any:

性能优化

七、常见问题

Q1:模板中的类型检查

Q2:处理$refs类型

Q3:动态路由类型(Vue  Router)

Q2: 如何处理模板中的类型检查?

八、学习资源


一、Vue 3 + TypeScript 

Vue 3 原生支持 TypeScript,推荐使用 <script setup> 语法糖和 Composition API。

1. 项目创建与配置
项目创建

使用 Vue CLI 或 Vite 创建支持 TypeScript 的 Vue 项目:

# 使用 Vite 创建 Vue + TS 项目(推荐)
npm create vite@latest my-vue-app -- --template vue-ts# 进入项目并运行
cd my-vue-app
npm install
npm run dev# 使用 Vue CLI(需全局安装 @vue/cli)
vue create my-vue-app
# 选择 "Manually select features" → 勾选 TypeScript
 关键配置文件

tsconfig.json

{"compilerOptions": {"target": "ESNext","module": "ESNext","strict": true,                   // 启用严格模式"jsx": "preserve",                 // 支持 JSX(可选)"moduleResolution": "node","baseUrl": "./","paths": {"@/*": ["src/*"]                 // 路径别名},"types": ["vite/client"]           // Vite 环境类型},"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.vue"]
}

vite.config.ts(vite项目)

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';export default defineConfig({plugins: [vue()],resolve: {alias: {'@': '/src' // 路径别名}}
});
2.完整项目结构示例
src/components/Button.vue      // 基础组件GenericList.vue // 泛型组件stores/counter.ts      // Pinia Storetypes/global.d.ts     // 全局类型扩展api.d.ts        // API 响应类型views/Home.vueApp.vuemain.tsvite-env.d.ts
3. 组件 Props 类型定义
<script setup lang="ts">
// 定义 Props 类型
interface Props {title: string;count?: number;   // 可选参数isActive: boolean;
}// 声明 Props(withDefaults 设置默认值)
const props = withDefaults(defineProps<Props>(), {count: 0,isActive: false
});// 使用 Props
console.log(props.title);
</script><template><div :class="{ active: props.isActive }">{{ title }} - {{ count }}</div>
</template>
4. 响应式数据与 Ref
<script setup lang="ts">
import { ref, computed } from 'vue';// 定义响应式数据(自动推断类型)
const count = ref(0); // 类型为 Ref<number>// 显式指定复杂类型
interface User {name: string;age: number;
}
const user = ref<User>({ name: 'Alice', age: 30 });// 计算属性
const doubleCount = computed(() => count.value * 2);// 函数
const increment = () => {count.value++;
};
</script>
5. Composition 函数复用
// composables/useCounter.ts
import { ref } from 'vue';export default function useCounter(initialValue: number = 0) {const count = ref(initialValue);const increment = () => {count.value++;};return {count,increment};
}// 在组件中使用
<script setup lang="ts">
import useCounter from '@/composables/useCounter';const { count, increment } = useCounter(10);
</script>

二、组件开发

1.组合式API(Composition API)
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';// Props 类型定义
interface Props {title: string;initialCount?: number;
}
const props = withDefaults(defineProps<Props>(), {initialCount: 0
});// 响应式数据
const count = ref(props.initialCount);
const doubleCount = computed(() => count.value * 2);// 事件触发
const emit = defineEmits<{(e: 'count-change', newCount: number): void;
}>();// 方法
const increment = () => {count.value++;emit('count-change', count.value);
};// 生命周期
onMounted(() => {console.log('Component mounted');
});
</script><template><div><h2>{{ title }}</h2><p>Count: {{ count }}, Double: {{ doubleCount }}</p><button @click="increment">+1</button></div>
</template>
2.选项式API(Options API)
<script lang="ts">
import { defineComponent } from 'vue';interface State {message: string;count: number;
}export default defineComponent({props: {title: {type: String,required: true}},data(): State {return {message: 'Hello Vue!',count: 0};},computed: {reversedMessage(): string {return this.message.split('').reverse().join('');}},methods: {increment() {this.count++;}}
});
</script>




三、Vue 2 + TypeScript

Vue 2 需通过 vue-class-component 或 vue-property-decorator 增强类型支持。

1. 安装依赖
npm install vue-class-component vue-property-decorator --save
2. 类组件(Vue Class Component)
<template><div><p>{{ message }}</p><button @click="increment">Count: {{ count }}</button></div>
</template><script lang="ts">
import { Component, Vue, Prop } from 'vue-property-decorator';@Component
export default class MyComponent extends Vue {// Props@Prop({ type: String, required: true }) readonly title!: string;@Prop({ default: 0 }) readonly initialCount!: number;// 数据private count: number = this.initialCount;// 计算属性get message(): string {return `${this.title}: ${this.count}`;}// 方法increment() {this.count++;}
}
</script>
3.Vuex类型安全
// store/modules/user.ts
import { Module } from 'vuex';interface UserState {name: string;age: number;
}const userModule: Module<UserState, RootState> = {namespaced: true,state: () => ({name: 'Alice',age: 30}),mutations: {SET_NAME(state, payload: string) {state.name = payload;}},actions: {updateName({ commit }, newName: string) {commit('SET_NAME', newName);}},getters: {fullInfo: (state) => `${state.name} (${state.age})`}
};

四、状态管理(Pinia + TypeScript)

Pinia 是 Vue 官方推荐的状态管理库,天然支持 TypeScript。

1. 定义 Store

// stores/counter.ts
import { defineStore } from 'pinia';interface CounterState {count: number;lastUpdated?: Date;
}export const useCounterStore = defineStore('counter', {state: (): CounterState => ({count: 0,lastUpdated: undefined}),actions: {increment() {this.count++;this.lastUpdated = new Date();}},getters: {doubleCount: (state) => state.count * 2}
});
2. 在组件中使用
<script setup lang="ts">
import { useCounterStore } from '@/stores/counter';const counterStore = useCounterStore();// 直接修改状态
counterStore.count++;// 通过 action 修改
counterStore.increment();// 使用 getter
console.log(counterStore.doubleCount);
</script>
 

五、高级类型操作

1.全局属性扩展
// src/types/global.d.ts
import { ComponentCustomProperties } from 'vue';declare module '@vue/runtime-core' {interface ComponentCustomProperties {$formatDate: (date: Date) => string; // 全局方法$api: typeof import('./api').default; // 全局 API 实例}
}// main.ts 中注入
app.config.globalProperties.$formatDate = (date: Date) => date.toLocaleString();
2. 为无类型库添加声明

创建 src/types/shims.d.ts

declare module 'untyped-vue-library' {export function doSomething(config: any): void;
}
3.泛型组件
<script setup lang="ts" generic="T">
import { ref } from 'vue';interface GenericListProps<T> {items: T[];keyField: keyof T;
}const props = defineProps<GenericListProps<T>>();const selectedItem = ref<T>();
</script><template><ul><li v-for="item in items" :key="item[keyField]"@click="selectedItem = item">{{ item }}</li></ul>
</template>
4.使用 Vue Router
// router/index.ts
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';const routes: Array<RouteRecordRaw> = [{path: '/',name: 'Home',component: () => import('@/views/Home.vue')}
];const router = createRouter({history: createWebHistory(),routes
});export default router;

六、最佳实践

严格类型检查

在 tsconfig.json 中启用严格模式

{"compilerOptions": {"strict": true,"noImplicitAny": false // 可逐步开启}
}
类型导入

优先使用 TypeScript 类型导入:

import type { Router } from 'vue-router';
避免 any

尽量使用精确类型,仅在紧急情况下使用 any

性能优化

1.类型安全的异步组件

// 显式定义加载的组件类型
const AsyncModal = defineAsyncComponent({loader: () => import('./Modal.vue'),loadingComponent: LoadingSpinner,delay: 200
});

2.避免不必要的类型断言

// Bad: 过度使用 as
const data = response.data as User[];// Good: 使用类型守卫
function isUserArray(data: any): data is User[] {return Array.isArray(data) && data.every(item => 'id' in item);
}
if (isUserArray(response.data)) {// 安全使用 response.data
}

七、常见问题

Q1:模板中的类型检查

安装Volar VS Code插件

禁用Vetur(避免冲突)

Q2:处理$refs类型
// main.ts
import { createApp } from 'vue';declare module '@vue/runtime-core' {interface ComponentCustomProperties {$myGlobal: string;}
}const app = createApp(App);
app.config.globalProperties.$myGlobal = 'hello';
Q3:动态路由类型(Vue  Router)
// router/index.ts
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';declare module 'vue-router' {interface RouteMeta {requiresAuth?: boolean;}
}const routes: RouteRecordRaw[] = [{path: '/user/:id',component: () => import('@/views/User.vue'),props: (route) => ({ id: Number(route.params.id) }) // 类型转换}
];

Q3: 如何为全局属性添加类型?

在 Vue 3 中:

// main.ts
import { createApp } from 'vue';declare module '@vue/runtime-core' {interface ComponentCustomProperties {$myGlobal: string;}
}const app = createApp(App);
app.config.globalProperties.$myGlobal = 'hello';
Q2: 如何处理模板中的类型检查?

Vue 3 的 Volar 插件(替代 Vetur)提供模板内类型检查,需在 VS Code 中安装。

八、学习资源

  1. Vue 3 + TypeScript 官方指南

  2. Pinia 官方文档

  3. Vue 3 TypeScript 示例项目

本文有待更新,暂时不为最终版本

码字不易,各位大佬点点赞

相关文章:

【TypeScript】ts在vue中的使用

目录 一、Vue 3 TypeScript 1. 项目创建与配置 项目创建 关键配置文件 2.完整项目结构示例 3. 组件 Props 类型定义 4. 响应式数据与 Ref 5. Composition 函数复用 二、组件开发 1.组合式API&#xff08;Composition API&#xff09; 2.选项式API&#xff08;Options…...

2025前端框架最新组件解析与实战技巧:Vue与React的革新之路

作者&#xff1a;飞天大河豚 引言 2025年的前端开发领域&#xff0c;Vue与React依然是开发者最青睐的框架。随着Vue 3的全面普及和React 18的持续优化&#xff0c;两大框架在组件化开发、性能优化、工程化支持等方面均有显著突破。本文将从最新组件特性、使用场景和编码技巧三…...

Elasticsearch 的分布式架构原理:通俗易懂版

Elasticsearch 的分布式架构原理&#xff1a;通俗易懂版 Lucene 和 Elasticsearch 的前世今生 Lucene 是一个功能强大的搜索库&#xff0c;提供了高效的全文检索能力。然而&#xff0c;直接基于 Lucene 开发非常复杂&#xff0c;即使是简单的功能也需要编写大量的 Java 代码&…...

【DeepSeek】【GPT-Academic】:DeepSeek集成到GPT-Academic(官方+第三方)

目录 1 官方deepseek 1.1 拉取学术GPT项目 1.2 安装依赖 1.3 修改配置文件中的DEEPSEEK_API_KEY 2 第三方API 2.1 修改DEEPSEEK_API_KEY 2.2 修改CUSTOM_API_KEY_PATTERM 2.3 地址重定向 2.4 修改模型参数 2.5 成功调用 2.6 尝试添加一个deepseek-r1参数 3 使用千帆…...

2.部署kafka:9092

官方文档&#xff1a;http://kafka.apache.org/documentation.html (虽然kafka中集成了zookeeper,但还是建议使用独立的zk集群) Kafka3台集群搭建环境&#xff1a; 操作系统: centos7 防火墙&#xff1a;全关 3台zookeeper集群内的机器&#xff0c;1台logstash 软件版本: …...

学习路之PHP --TP6异步执行功能 (无需安装任何框架)

学习路之PHP --异步执行功能 &#xff08;无需安装任何框架&#xff09; 简介一、工具类二、调用三、异步任务的操作四、效果&#xff1a; 简介 执行异步任务是一种很常见的需求&#xff0c;如批量发邮箱&#xff0c;短信等等执行耗时任务时&#xff0c;需要程序异步执行&…...

Uniapp 小程序复制、粘贴功能实现

在开发 Uniapp 小程序的过程中&#xff0c;复制和粘贴功能是非常实用且常见的交互需求。今天&#xff0c;我就来和大家详细分享如何在 Uniapp 中实现这两个功能。 复制功能&#xff1a;uni.setClipboardData方法 goResult() {uni.setClipboardData({data: this.copyContent, /…...

seacmsv9注入管理员账号密码+orderby+limit

一、seacmsv9 SQL注入漏洞 查看源码 <?php session_start(); require_once("include/common.php"); //前置跳转start $cs$_SERVER["REQUEST_URI"]; if($GLOBALS[cfg_mskin]3 AND $GLOBALS[isMobile]1){header("location:$cfg_mhost$cs");}…...

多通道数据采集和信号生成的模块化仪器如何重构飞机电子可靠性测试体系?

飞机的核心电子系统包括发电与配电系统&#xff0c;飞机内部所有设备和系统之间的内部数据通信系统&#xff0c;以及用于外部通信的射频设备。其他所有航空电子元件都依赖这些关键总线进行电力传输或数据通信。在本文中&#xff0c;我们将了解模块化仪器&#xff08;无论是PCIe…...

天润融通分析DeepSeek如何一键完成从PR接入,到真正的业务接入

DeepSeek出圈之后&#xff0c;市场上很快掀起了一波DeepSeek接入潮。 在客户服务领域&#xff0c;许多企业见识到DeepSeek的超强能力后&#xff0c;也迅速接入DeepSeek并获得了不错的效果。 比如在客户接待服务场景&#xff0c;有企业将DeepSeek应用到智能问答助手&#xff0…...

免费PDF工具

Smallpdf.com - A Free Solution to all your PDF Problems Smallpdf - the platform that makes it super easy to convert and edit all your PDF files. Solving all your PDF problems in one place - and yes, free. https://smallpdf.com/#rappSmallpdf.com-解决您所有PD…...

PyTorch 源码学习:GPU 内存管理之它山之石——TensorFlow BFC 算法

TensorFlow 和 PyTorch 都是常用的深度学习框架&#xff0c;各自有一套独特但又相似的 GPU 内存管理机制&#xff08;BFC 算法&#xff09;。它山之石可以攻玉。了解 TensorFlow 的 BFC 算法有助于学习 PyTorch 管理 GPU 内存的精妙之处。本文重点关注 TensorFlow BFC 算法的核…...

【学写LibreCAD】1 LibreCAD主程序

一、源码 头文件&#xff1a; #ifndef MAIN_H #define MAIN_H#include<QStringList>#define STR(x) #x #define XSTR(x) STR(x)/*** brief handleArgs* param argc cli argument counter from main()* param argv cli arguments from main()* param argClean a list…...

Android Studio超级详细讲解下载、安装配置教程(建议收藏)

博主介绍&#xff1a;✌专注于前后端、机器学习、人工智能应用领域开发的优质创作者、秉着互联网精神开源贡献精神&#xff0c;答疑解惑、坚持优质作品共享。本人是掘金/腾讯云/阿里云等平台优质作者、擅长前后端项目开发和毕业项目实战&#xff0c;深受全网粉丝喜爱与支持✌有…...

CDN与群联云防护的技术差异在哪?

CDN&#xff08;内容分发网络&#xff09;与群联云防护是两种常用于提升网站性能和安全的解决方案&#xff0c;但两者的核心目标和技术实现存在显著差异。本文将从防御机制、技术架构、适用场景和代码实现等方面详细对比两者的区别&#xff0c;并提供可直接运行的代码示例。 一…...

故障诊断 | Matlab实现基于DBO-BP-Bagging多特征分类预测/故障诊断

故障诊断 | Matlab实现基于DBO-BP-Bagging多特征分类预测/故障诊断 目录 故障诊断 | Matlab实现基于DBO-BP-Bagging多特征分类预测/故障诊断分类效果基本介绍模型描述DBO-BP-Bagging蜣螂算法优化多特征分类预测一、引言1.1、研究背景和意义1.2、研究现状1.3、研究目的与方法 二…...

Linux-SaltStack配置

文章目录 SaltStack配置 &#x1f3e1;作者主页&#xff1a;点击&#xff01; &#x1f916;Linux专栏&#xff1a;点击&#xff01; ⏰️创作时间&#xff1a;2025年02月24日20点51分 SaltStack配置 SaltStack 中既支持SSH协议也支持我们的一个客户端 #获取公钥&#xff08;…...

内网渗透测试-Vulnerable Docker靶场

靶场来源&#xff1a; Vulnerable Docker: 1 ~ VulnHub 描述&#xff1a;Down By The Docker 有没有想过在容器中玩 docker 错误配置、权限提升等&#xff1f; 下载此 VM&#xff0c;拿出您的渗透测试帽并开始使用 我们有 2 种模式&#xff1a; - HARD&#xff1a;这需要您将 d…...

云计算如何解决延迟问题?

在云计算中&#xff0c;延迟&#xff08;latency&#xff09;指的是从请求发出到收到响应之间的时间间隔。延迟过高可能会严重影响用户体验&#xff0c;特别是在需要实时响应的应用中&#xff0c;如在线游戏、视频流、金融交易等。云计算服务如何解决延迟问题&#xff0c;通常依…...

飞书webhook监控业务系统端口

钉钉告警没有额度了&#xff0c;替代方案使用企业微信或者是飞书&#xff0c;以下脚本是飞书为例 监控ping也就是活动主机 #!/bin/bash # IP Ping 监控脚本 date$(date "%Y-%m-%d %H:%M:%S") # 根据实际情况修改飞书 Webhook 地址 webhook"https://open.feish…...

ffmpeg(四):滤镜命令

FFmpeg 的滤镜命令是用于音视频处理中的强大工具&#xff0c;可以完成剪裁、缩放、加水印、调色、合成、旋转、模糊、叠加字幕等复杂的操作。其核心语法格式一般如下&#xff1a; ffmpeg -i input.mp4 -vf "滤镜参数" output.mp4或者带音频滤镜&#xff1a; ffmpeg…...

图表类系列各种样式PPT模版分享

图标图表系列PPT模版&#xff0c;柱状图PPT模版&#xff0c;线状图PPT模版&#xff0c;折线图PPT模版&#xff0c;饼状图PPT模版&#xff0c;雷达图PPT模版&#xff0c;树状图PPT模版 图表类系列各种样式PPT模版分享&#xff1a;图表系列PPT模板https://pan.quark.cn/s/20d40aa…...

Yolov8 目标检测蒸馏学习记录

yolov8系列模型蒸馏基本流程&#xff0c;代码下载&#xff1a;这里本人提交了一个demo:djdll/Yolov8_Distillation: Yolov8轻量化_蒸馏代码实现 在轻量化模型设计中&#xff0c;**知识蒸馏&#xff08;Knowledge Distillation&#xff09;**被广泛应用&#xff0c;作为提升模型…...

C++:多态机制详解

目录 一. 多态的概念 1.静态多态&#xff08;编译时多态&#xff09; 二.动态多态的定义及实现 1.多态的构成条件 2.虚函数 3.虚函数的重写/覆盖 4.虚函数重写的一些其他问题 1&#xff09;.协变 2&#xff09;.析构函数的重写 5.override 和 final关键字 1&#…...

Ubuntu Cursor升级成v1.0

0. 当前版本低 使用当前 Cursor v0.50时 GitHub Copilot Chat 打不开&#xff0c;快捷键也不好用&#xff0c;当看到 Cursor 升级后&#xff0c;还是蛮高兴的 1. 下载 Cursor 下载地址&#xff1a;https://www.cursor.com/cn/downloads 点击下载 Linux (x64) &#xff0c;…...

书籍“之“字形打印矩阵(8)0609

题目 给定一个矩阵matrix&#xff0c;按照"之"字形的方式打印这个矩阵&#xff0c;例如&#xff1a; 1 2 3 4 5 6 7 8 9 10 11 12 ”之“字形打印的结果为&#xff1a;1&#xff0c;…...

精益数据分析(98/126):电商转化率优化与网站性能的底层逻辑

精益数据分析&#xff08;98/126&#xff09;&#xff1a;电商转化率优化与网站性能的底层逻辑 在电子商务领域&#xff0c;转化率与网站性能是决定商业成败的核心指标。今天&#xff0c;我们将深入解析不同类型电商平台的转化率基准&#xff0c;探讨页面加载速度对用户行为的…...

Java中栈的多种实现类详解

Java中栈的多种实现类详解&#xff1a;Stack、LinkedList与ArrayDeque全方位对比 前言一、Stack类——Java最早的栈实现1.1 Stack类简介1.2 常用方法1.3 优缺点分析 二、LinkedList类——灵活的双端链表2.1 LinkedList类简介2.2 常用方法2.3 优缺点分析 三、ArrayDeque类——高…...

基于小程序老人监护管理系统源码数据库文档

摘 要 近年来&#xff0c;随着我国人口老龄化问题日益严重&#xff0c;独居和居住养老机构的的老年人数量越来越多。而随着老年人数量的逐步增长&#xff0c;随之而来的是日益突出的老年人问题&#xff0c;尤其是老年人的健康问题&#xff0c;尤其是老年人产生健康问题后&…...

中国政务数据安全建设细化及市场需求分析

(基于新《政务数据共享条例》及相关法规) 一、引言 近年来,中国政府高度重视数字政府建设和数据要素市场化配置改革。《政务数据共享条例》(以下简称“《共享条例》”)的发布,与《中华人民共和国数据安全法》(以下简称“《数据安全法》”)、《中华人民共和国个人信息…...