Vue 3 与 TypeScript:最佳实践详解

简介: Vue 3 与 TypeScript:最佳实践详解

很多人问我为什么要用TypeScript

因为 Vue3 喜欢它!

开个玩笑...

在我们开始探索 Vue 3 和 TypeScript 最佳实践之前,让我们先打个比方。

如果你曾经尝试过在没有 GPS 的情况下开车到一个陌生的地方,你可能会知道那种迷失方向的感觉。

而 TypeScript 就像是你编程旅程中的 GPS,帮助你避免错误并顺利到达目标。

现在,让我们进入正题,看看如何在 Vue 3 中使用 TypeScript,并了解一些最佳实践。

目录

  1. 1. 准备工作
  2. 2. 项目初始化
  3. 3. 配置 TypeScript
  4. 4. 组件开发
  • • 定义 Props
  • • 使用 Ref
  • • 组件事件
  1. 5. 组合式 API 与 TypeScript
  2. 6. Vue 3 与 TypeScript 最佳实践
  3. 7. 测试与调试
  4. 8. 结论

准备工作

在开始之前,请确保你的开发环境已经安装了以下工具:

  • Node.js(推荐使用最新的 LTS 版本)
  • npmYarn
  • Vue CLI(确保你安装的是最新版本)

  • 安装 Vue CLI

如果你还没有安装 Vue CLI,请先安装:

npm install -g @vue/cli

你可以使用以下命令来检查版本:

vue --version

确保你使用的是最新版本的 Vue CLI。


项目初始化

接下来,我们将使用 Vue CLI 初始化一个新的 Vue 3 项目,并启用 TypeScript 支持。

vue create vue3-typescript-demo

在创建项目过程中,选择以下选项:

  • • Manually select features
  • • Babel, TypeScript, Router, Vuex, Linter/Formatter
  • • Use class-style component syntax? No
  • • Use Babel alongside TypeScript? Yes

项目创建完成后,进入项目目录:

cd vue3-typescript-demo


启动开发服务器

确保一切正常工作,启动开发服务器:

npm run serve

打开浏览器,访问 http://localhost:8080,你应该会看到一个 Vue 欢迎页面。


配置 TypeScript

在这个项目中,TypeScript 已经通过 Vue CLI 自动配置好了。不过,我们仍然需要了解一些关键配置项。

tsconfig.json

tsconfig.json 文件是 TypeScript 的配置文件。以下是一些关键配置项:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "sourceMap": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    },
    "lib": ["esnext", "dom"]
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
  "exclude": ["node_modules"]
}


shims-vue.d.ts

为了让 TypeScript 正确识别 .vue 文件,我们需要在项目根目录下创建一个 shims-vue.d.ts 文件:

declare module '*.vue' {
  import { DefineComponent } from 'vue';
  const component: DefineComponent<{}, {}, any>;
  export default component;
}


组件开发

现在,我们已经配置好了 TypeScript,可以开始编写 Vue 组件了。

定义 Props

在 Vue 3 中,使用 TypeScript 定义组件的 Props 非常简单直观。

示例

<template>
  <div>
    <h1>{{ title }}</h1>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  name: 'HelloWorld',
  props: {
    title: {
      type: String,
      required: true
    }
  }
});
</script>


使用 Ref

在 Vue 3 中,使用 ref 进行数据绑定和操作变得更为强大和灵活。这里展示如何在 TypeScript 中使用 ref

示例

<template>
  <div>
    <input v-model="message" placeholder="Enter a message" />
    <p>{{ message }}</p>
    <button @click="reset">Reset</button>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
export default defineComponent({
  name: 'MessageInput',
  setup() {
    const message = ref<string>('');
    const reset = () => {
      message.value = '';
    };
    return {
      message,
      reset
    };
  }
});
</script>


组件事件

处理组件事件和事件类型是 TypeScript 的另一个重要方面。

示例

<template>
  <div>
    <button @click="handleClick">Click Me</button>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  name: 'ClickButton',
  emits: ['customClick'],
  methods: {
    handleClick(event: MouseEvent) {
      this.$emit('customClick', event);
    }
  }
});
</script>

使用 emit 函数

在组合式 API 中,你可以使用 emit 函数来触发事件。

示例

<template>
  <div>
    <button @click="handleClick">Click Me</button>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
export default defineComponent({
  name: 'ClickButton',
  emits: ['customClick'],
  setup(props, { emit }) {
    const handleClick = (event: MouseEvent) => {
      emit('customClick', event);
    };
    return {
      handleClick
    };
  }
});
</script>


组合式 API 与 TypeScript

Vue 3 的组合式 API 与 TypeScript 的结合让代码更具模块化和可读性。

使用 reactive

reactive 允许你创建响应式的数据对象。

示例

<template>
  <div>
    <p>{{ state.count }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>
<script lang="ts">
import { defineComponent, reactive } from 'vue';
export default defineComponent({
  name: 'Counter',
  setup() {
    const state = reactive({
      count: 0
    });
    const increment = () => {
      state.count++;
    };
    return {
      state,
      increment
    };
  }
});
</script>


使用 computed

computed 允许你创建基于其他响应式数据的计算属性。

示例

<template>
  <div>
    <p>Double: {{ doubleCount }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>
<script lang="ts">
import { defineComponent, reactive, computed } from 'vue';
export default defineComponent({
  name: 'ComputedCounter',
  setup() {
    const state = reactive({
      count: 0
    });
    const doubleCount = computed(() => state.count * 2);
    const increment = () => {
      state.count++;
    };
    return {
      state,
      doubleCount,
      increment
    };
  }
});
</script>


Vue 3 与 TypeScript 最佳实践

在使用 Vue 3 和 TypeScript 开发过程中,遵循一些最佳实践可以帮助你编写高质量的代码。

类型推断

TypeScript 的强大之处在于其类型推断功能。尽量依赖 TypeScript 的类型推断,而不是显式地声明类型。

示例

const count = ref(0); // TypeScript 会自动推断出 count 是 number 类型


避免 any 类型

尽量避免使用 any 类型,因为它会消除 TypeScript 的类型检查机制。

示例

const message: any = 'Hello'; // 尽量避免


使用类型声明文件

将公共类型定义提取到单独的文件中,可以提高代码的可维护性。

示例

types.ts
export interface User {
  id: number;
  name: string;
  email: string;
}

在组件中使用:

import { User } from '@/types';
const user: User = {
  id: 1,
  name: 'John Doe',
  email: 'john.doe@example.com'
};

使用 ESLint 和 Prettier

使用代码检查工具和格式化工具可以帮助你保持代码的一致性和质量。Vue CLI 允许你在项目初始化时选择 ESLint 和 Prettier。

安装 ESLint 和 Prettier

如果你在项目创建时没有选择 ESLint 和 Prettier,可以通过以下命令手动安装:

npm install --save-dev eslint prettier eslint-plugin-vue eslint-plugin-prettier eslint-config-prettier

配置 ESLint

在项目根目录创建一个 .eslintrc.js 文件:

module.exports = {
  root: true,
  env: {
    node: true
  },
  extends: [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/typescript/recommended',
    'plugin:prettier/recommended'
  ],
  parserOptions: {
    ecmaVersion: 2020
  },
  rules: {
    'vue/no-multiple-template-root': 'off',
    'prettier/prettier': [
      'error',
      {
        singleQuote: true,
        semi: false
      }
    ]
  }
}

配置 Prettier

在项目根目录创建一个 .prettierrc 文件:

{
  "singleQuote": true,
  "semi": false
}


使用 Vuex 和 TypeScript

Vuex 是 Vue 的状态管理模式,结合 TypeScript 可以更好地管理应用状态。

安装 Vuex

npm install vuex@next

创建一个 Vuex Store

store/index.ts
import { createStore } from 'vuex'
interface State {
  count: number
}
const store = createStore<State>({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  },
  actions: {
    increment(context) {
      context.commit('increment')
    }
  },
  getters: {
    doubleCount(state) {
      return state.count * 2
    }
  }
})
export default store

在组件中使用 Vuex

<template>
  <div>
    <p>{{ count }}</p>
    <p>{{ doubleCount }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>
<script lang="ts">
import { defineComponent, computed } from 'vue'
import { useStore } from 'vuex'
export default defineComponent({
  name: 'VuexCounter',
  setup() {
    const store = useStore()
    const count = computed(() => store.state.count)
    const doubleCount = computed(() => store.getters.doubleCount)
    const increment = () => {
      store.dispatch('increment')
    }
    return {
      count,
      doubleCount,
      increment
    }
  }
})
</script>


测试与调试

良好的测试和调试习惯是确保代码质量的重要保证。我们将使用 Jest 进行单元测试,并展示如何在 Vue 3 项目中进行调试。


安装 Jest

npm install --save-dev jest @vue/test-utils @types/jest ts-jest


配置 Jest

在项目根目录创建一个 jest.config.js 文件:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'jsdom',
  transform: {
    '^.+\\.vue$': 'vue-jest',
    '^.+\\.[t|j]sx?$': 'babel-jest'
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'vue'],
  transformIgnorePatterns: ['/node_modules/']
}


编写单元测试

创建一个简单的组件测试文件 tests/unit/HelloWorld.spec.ts

import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'
describe('HelloWorld.vue', () => {
  it('renders props.msg when passed', () => {
    const msg = 'new message'
    const wrapper = shallowMount(HelloWorld, {
      props: { msg }
    })
    expect(wrapper.text()).toMatch(msg)
  })
})


运行测试

npm run test:unit


调试

在 Vue 3 中,你可以使用浏览器的开发者工具进行调试。以下是一些调试技巧:

  • 断点调试:在源码中设置断点,然后在浏览器中运行应用,代码执行到断点处会自动暂停。
  • Vue Devtools:安装 Vue Devtools 浏览器扩展,可以方便
  • Vue Devtools:安装 Vue Devtools 浏览器扩展,可以方便地查看和调试 Vue 组件的状态、事件、Vuex 状态等。你可以从这里下载并安装 Vue Devtools。

结论

在本文中,我们详细介绍了在 Vue 3 项目中使用 TypeScript 的最佳实践。从项目初始化、配置 TypeScript,到组件开发、组合式 API、Vuex 集成以及测试和调试,我们涵盖了开发过程中会遇到的主要环节。

TypeScript 的类型检查和智能提示功能大大提高了代码的可靠性和可维护性。结合 Vue 3 的强大功能,开发者可以更高效地构建复杂且健壮的前端应用。

相关文章
|
2月前
|
JavaScript 安全 前端开发
Gzm Design:开源神器!用 Vue3、Vite4、TypeScript 革新海报设计,免费开源的海报设计器,主流技术打造,轻松高效
海报设计在各个领域都有着广泛的应用,无论是商业广告、活动宣传还是个人创意表达。今天要给大家介绍一款免费开源的海报设计器——Gzm Design,它基于最新的主流技术开发,为用户提供了丰富的功能,让海报设计变得轻松又高效。
187 64
|
2月前
|
JavaScript 数据安全/隐私保护
Vue Amazing UI 组件库(Vue3+TypeScript+Vite 等最新技术栈开发)
Vue Amazing UI 是一个基于 Vue 3、TypeScript、Vite 等最新技术栈开发构建的现代化组件库,包含丰富的 UI 组件和常用工具函数,并且持续不断维护更新中。另外,组件库全量使用 TypeScript,支持自动按需引入和 Tree Shaking 等,能够显著提升开发效率,降低开发成本。
155 5
Vue Amazing UI 组件库(Vue3+TypeScript+Vite 等最新技术栈开发)
|
8月前
|
JavaScript 前端开发 IDE
[译] 用 Typescript + Composition API 重构 Vue 3 组件
[译] 用 Typescript + Composition API 重构 Vue 3 组件
[译] 用 Typescript + Composition API 重构 Vue 3 组件
|
6月前
|
JavaScript 前端开发 开发者
深入理解TypeScript:类型系统与最佳实践
【10月更文挑战第8天】深入理解TypeScript:类型系统与最佳实践
|
6月前
|
JavaScript 安全 开发工具
在 Vue 3 中使用 TypeScript
【10月更文挑战第3天】
|
8月前
|
JavaScript API
如何使用Vue 3和Type Script进行组件化设计
【8月更文挑战第16天】如何使用Vue 3和Type Script进行组件化设计
80 3
|
8月前
|
JavaScript API
如何使用Vue 3和Type Script进行组件化设计
【8月更文挑战第16天】如何使用Vue 3和Type Script进行组件化设计
146 1
|
8月前
|
资源调度 JavaScript 前端开发
Vue3+TypeScript前端项目新纪元:揭秘高效事件总线Mitt,轻松驾驭组件间通信的艺术!
【8月更文挑战第3天】Vue3结合TypeScript强化了类型安全与组件化开发。面对大型应用中复杂的组件通信挑战,可通过引入轻量级事件发射器Mitt实现事件总线模式。Mitt易于集成,通过简单几步即可完成安装与配置:安装Mitt、创建事件总线实例、并在组件中使用`emit`与`on`方法发送及监听事件。此外,利用TypeScript的强大类型系统确保事件处理器正确无误。这种方式有助于保持代码整洁、解耦组件,同时提高应用的可维护性和扩展性。不过,在大规模项目中需谨慎使用,以防事件流过于复杂难以管理。
220 1
|
8月前
|
JavaScript 前端开发 安全
解锁Vue3与TypeScript的完美搭档:getCurrentInstance带你高效打造未来前端
【8月更文挑战第21天】Vue3的getCurrentInstance方法作为Composition API的一部分,让开发者能在组件内访问实例。结合TypeScript,可通过定义组件实例类型实现更好的代码提示与类型检查,提升开发效率与代码质量。例如,定义一个带有特定属性(如myData)的组件实例类型,可以在setup中获取并安全地修改这些属性。这种方式确保了一致性和减少了运行时错误,使开发更加高效和安全。
305 0
|
2月前
|
存储 安全 JavaScript
TypeScript-内置应用程序类型-Recode
通过使用 `Record` 类型,开发者可以显著提升代码的安全性和可维护性。无论是配置对象、字典结构还是动态表单,`Record` 类型都提供了一个简洁、类型安全的解决方案。
205 82
下一篇
oss创建bucket