Skip to content

oliver139/vue3-ts-base

Repository files navigation

vue-ts-base

en zh-hant zh-hans

A Vue 3 base with common useful libraries setup for cloning to start a new website development. Vite + Vue 3 composition API + TypeScript are used.

Table of Contents

Setup

Recommended IDE Setup

VSCode + Volar (and disable Vetur) + TypeScript Vue Plugin (Volar).

Type Support for .vue Imports in TS

TypeScript cannot handle type information for .vue imports by default, so we replace the tsc CLI with vue-tsc for type checking. In editors, we need TypeScript Vue Plugin (Volar) to make the TypeScript language service aware of .vue types.

If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a Take Over Mode that is more performant. You can enable it by the following steps:

  1. Disable the built-in TypeScript Extension
    1. Run Extensions: Show Built-in Extensions from VSCode's command palette
    2. Find TypeScript and JavaScript Language Features, right click and select Disable (Workspace)
  2. Reload the VSCode window by running Developer: Reload Window from the command palette.

Feature included

Vue

  • Vue router
  • Pinia (Global State Management)
  • Iconify (Open Source Icon Library, Available for Commerical Use)
  • VueUse (Useful utility library)
  • vue-i18n (Multi-languages support)
  • vue-gtag [Optional --no-optional] (Connect Google Analytics)

Unplugin

  • unplugin-auto-import, allow to use below keywords without importing:
    • Vue (ref, reactive, computed etc.)
    • Vue Router (useRoute, useRouter etc.)
    • Vue i18n (Check the below for details)
      • usei18n()
      • $t, $d, $n, $local
      • _changeLang
    • VueUse (Functions in @vueuse/core)
  • unplugin-vue-components, Allow to use components without importing
    • All components under /src/components
    • VueUse (Both components and directives)
    • Types:
      • vue-router
      • @iconify/vue

CSS

JavaScript / TypeScript

Git

Eslint is set to be run before git commit with use of below:

Vite

The below will be dropped upon production build:

  • debugger
  • console.log
  • console.debug

Multi-language Support with Vue-i18n

vue-i18n is used for handling multi-language. The below functions are auto imported with the help of unplugin-auto-import, so that you can directly use them:

Switch Language

When you want to switch language, please use _changeLang(targetLocale)

function _changeLang(targetLocale:string) {
  i18n.global.locale.value = targetLocale;
  localStorage.site_lang = targetLocale;
  document.documentElement.setAttribute("lang", targetLocale);
}

This function helps change:

  • The locale value in vue-i18n
  • site_lang value in LocalStorage
  • lang attribute of <html> tag

Add More Language

With the help of vite's import.meta.glob, you just need to create new <locale>.json under /src/i18n and the file will be loaded automatically.

Project Setup

PNPM with node 20.10.0 is used.

pnpm install

Compile and Hot-Reload for Development

pnpm dev

Type-Check, Compile and Minify for Production

pnpm build

Lint with ESLint

pnpm lint

About

A nice template for starting a vue-ts project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published