跪拜 Guibai
← All articles
Frontend Engineering

Vue 3 Admin Dashboard from Scratch: A 19-Step Guide with Real Gotchas

By ai全栈开发_陈js ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A reproducible 19-step setup like this saves developers days of assembling tooling and debugging mismatched versions. It also documents non-obvious failure points—forgetting to register VueQueryPlugin, pnpm 12 ignoring build scripts, or TypeScript 6 deprecating baseUrl—that typically only surface during real builds, making it a practical reference for anyone building Vue 3 admin dashboards.

Summary

Starting from an empty folder, the guide scaffolds a Vue 3 + TypeScript project with pnpm and Vite, adds ESLint flat config, Prettier, and strict TypeScript, and establishes a domain-based directory structure with a path alias configured in both Vite and TypeScript. Core features, each with a minimal example, include Element Plus menus and tables, Vue Router with lazy loading and a login guard, Pinia stores for user and UI state (with persistence for theme and locale), an Axios wrapper with interceptors for auth tokens and idempotency keys, and TanStack Query for server state with caching and invalidation.

The guide then covers internationalization with vue-i18n, a v-permission directive for button-level access control, ECharts via a reusable wrapper component, Day.js helpers with timezone discipline, CSS-variable theming that syncs with Element Plus dark mode, mock data fallbacks (with a warning against shipping mock code), and Vitest unit tests focused on pure utilities. It finishes with git hooks via simple-git-hooks and lint-staged for pre-commit checks, a command cheat sheet, and a detailed list of nine real-world gotchas, including pnpm 12 blocking install scripts, forgetting to register VueQueryPlugin, and ESLint getting polluted by Chrome profile files. The guide deliberately keeps each step minimal and defers production-grade patterns to a separate theoretical notebook.

Takeaways
— Use pnpm-lock.yaml with --frozen-lockfile in CI to guarantee identical dependency versions.
— Pin Node and pnpm versions via engines in package.json and a .nvmrc file.
— Configure ESLint 9+ flat config with vue-eslint-parser and tseslint.parser to handle <script setup lang="ts">.
— Register @tanstack/vue-query's VueQueryPlugin in main.ts; forgetting it causes a white screen with 'No queryClient found in Vue context'.
— Configure the path alias in both vite.config.ts and tsconfig.app.json; TypeScript won't recognize @ otherwise.
— Use unplugin-auto-import and unplugin-vue-components to auto-import Vue APIs and Element Plus components, generating .d.ts files.
— Implement a v-permission directive that removes elements from the DOM when the user lacks required permission codes; backend must still enforce authorization.
— Use CSS variables for theming rather than SCSS variables, since CSS variables update at runtime.
— For mock data, prefer environment-controlled loading or MSW; avoid mixing mock logic into production API functions.
— Write unit tests only for utility functions and pure logic; avoid testing full business pages with component mocks.
— Set up simple-git-hooks with lint-staged to run linting and formatting only on staged files, keeping commits fast.
— Use pnpm approve-builds --all to allow dependencies like vue-demi and simple-git-hooks to run install scripts under pnpm 12.
— Exclude screenshot, CI, and temporary directories from ESLint's ignores to prevent thousands of false errors.
Conclusions

The guide's deliberate separation of a minimal working example from a 'theoretical notebook' for production patterns reflects a practical learning approach that prevents beginners from drowning in advanced configurations while still pointing to them.

The gotchas list shows that modern toolchains have moving parts that trip up even seasoned developers; documenting such failures is often more valuable than the happy-path instructions.

The decision to test only pure utilities and skip component tests for business pages challenges the notion that all code needs unit tests, arguing that the maintenance cost of mocking routes, stores, and APIs often outweighs the benefit.

The emphasis on not persisting tokens and using HTTP-only refresh tokens for production highlights how even a beginner demo respects security boundaries.

Concepts & terms
pnpm-lock.yaml
The lockfile generated by pnpm that records the exact versions of dependencies and their resolution, ensuring reproducible installs across environments.
ESLint flat config
The default configuration format in ESLint 9+, where config is an array of objects instead of the legacy .eslintrc, allowing flexible merging and scope.
unplugin-auto-import
A Vite plugin that automatically imports Vue APIs (ref, computed) and UI library functions (like ElMessage) as you use them, generating type declarations for TypeScript.
TanStack Query's queryKey
An array that uniquely identifies a query in the cache; changing the key triggers a refetch, and it's used with invalidateQueries for cache invalidation.
invalidateQueries
A method on QueryClient that marks cached queries matching a prefix as stale, causing them to refetch on next use.
v-permission
A custom Vue directive that removes an element from the DOM if the user's permission list doesn't contain the required code, enabling button-level access control.
Idempotency-Key
A request header used to prevent duplicate side effects on retries, commonly applied to non-GET requests.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗