SoFunction
Updated on 2025-04-05

vite creates a standard vue3+ts+pinia project

Use vite to create an implementation example of a standard vue3+ts+pinia project

【01】Use Yarn to create a project:

1. Execute the command

yarn create vite my-vue-app --template vue-ts

3. cd my-vue-app //Enter into the project
4.yarn // Installation dependencies
5.yarn dev // Run the project

import path from 'path' // @types/node needs to be installed and configure "allowSyntheticDefaultImports" in compilerOptions: trueimport { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
function _resolve(dir) {
  return (__dirname, dir);
}
// /config/
export default defineConfig({
  plugins: [vue()],
  server:{
    host: '0.0.0.0', // Listen to all local IPs    port: 3010 // Project port  },
  resolve:{
    alias:{
      '@': _resolve('src') // Alias    }
  }
})

【02】Use pinia in the project

pinia official website

1. Install pinia

yarn add pinia

2. Quote to the project

import { createApp } from 'vue'
import App from './'
import { createPinia } from 'pinia' // Import pinia
const app = createApp(App)
(createPinia()) // Register pinia
('#app')

3. Use pinia Demo

// ./src/stores/

import { defineStore } from 'pinia',

const useCounterStore = defineStore('counterStore', {
  state: () => ({
    counter: 0
  })
})

​
// Used in setup
import { useCounterStore } from '../stores/counterStore'

export default {
  setup() {
    const counterStore = useCounterStore()

    return { counterStore }
  },
  computed: {
    tripleCounter() {
      return  * 3
    },
  },
}

【03】Add vue-router

1. Install router

yarn add vue-router

2. How to use

1). Create a router

// src/router/
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

const routes: RouteRecordRaw[] = [
  {
    path: '/login', // Browser access address    name: 'Login',
    component: () => import(/* webpackChunkName: "login"*/ new URL('../pages/Login/', ).href ),
    mate:{}
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

export default router

2). Quote to the project

// 
import router from './router'

(router)

【04】 Install the automatic import plug-in on demand

1. First, you need to install two plugins: unplugin-auto-import and unplugin-vue-components

- unplugin-auto-import: Automatically import api [github link](/antfu/unplugin-auto-import)
- unplugin-vue-components: Automatically import components used [github link](/antfu/unplugin-vue-components)

yarn add unplugin-auto-import unplugin-vue-components -D

2. Configuration

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'

export default defineConfig({
  plugins: [
    // Automatically import API method    AutoImport({
      imports: [  // Automatically import API configuration        'vue', 
        'vue-router',
        'pinia',
      ],
      resolvers: [], // custom resolvers
      dts: 'src/typings/', // Import storage address    }),
    // Automatically import components    Components({
      resolvers: [], // custom resolvers
      dts: 'src/typings/',
    }),
  ]
})

【05】Add element-plus component library

1. Install the dependency package first

yarn add element-plus

2. Automatically import styles and components

1). First, you need to install two plugins: unplugin-vue-components and unplugin-auto-import

yarn add unplugin-vue-components unplugin-auto-import -D

2). Configure to vite

// 

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  plugins: [
    // Automatically import Element Plus related functions, such as: ElMessage, ElMessageBox... (with style)    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    // Automatically import Element Plus components    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
})

3. element-plus icon library

1). Install the dependency package

// Installation packageyarn add @element-plus/icons-vue

2). Automatically import icon component configuration

// Automatically import any icon collection from Iconify using unplugin-icons and unplugin-auto-importyarn add unplugin-auto-import unplugin-icons -D
// 

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// Automatically import element iconimport Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Inspect from 'vite-plugin-inspect'
const path = require('path');
function _resolve(dir) {
  return (__dirname, dir);
}
// /config/
export default defineConfig({
  plugins: [
    vue(),
    // Automatically import Element Plus related functions, such as: ElMessage, ElMessageBox... (with style)    AutoImport({
      resolvers: [
        ElementPlusResolver(),
        // Automatically import icon components        IconsResolver({
          prefix: 'Icon',
        }),
        
      ],
      dts: (_resolve('src'), ''),
    }),
    // Automatically import Element Plus components    Components({
      resolvers: [
        // Automatically register icon component        IconsResolver({
          enabledCollections: ['ep'],
        }),
        ElementPlusResolver()],
    }),
    Icons({
      autoInstall: true, // Whether it is automatically loaded    }),
    Inspect(),
  ],
  server:{
    host: '0.0.0.0', // Listen to all local IPs    port: 3010 // Project port  },
  resolve:{
    alias:{
      '@': _resolve('src') // Alias    }
  }
})

3). How to use

<template>
    <i-ep-add-location />
    <IEpRefresh />
</template>

【06】Add sass

1. Installation

yarn add sass sass-loader -D

2. Configure sass global variables

// 
export default {
  css:{
    preprocessorOptions: {
      scss: {
        additionalData: "@import './src/assets/css/';",
      },
    },
  }
}

【07】 Install prettier and eslint

1. Install dependencies

// Install prettier---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------yarn add prettier eslint-config-prettier eslint-plugin-prettier -D

// Install eslint--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------yarn add eslint eslint-plugin-vue eslint-config-airbnb-base eslint-plugin-import @typescript-eslint/eslint-plugin @typescript-eslint/parser -D

2. Add the root directory. File

// .

 = {
  // Set forced single quotes  singleQuote: true,
  autoFix: false,
  printWidth: 140,
  semi: false,
  trailingComma: "none",
  arrowParens: "avoid",
  endOfLine: "LF",
};

3. Add the root directory. File

eslint official website

// .

 = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    "plugin:vue/vue3-essential",
    "airbnb-base",
    "@vue/typescript/recommended",
    "@vue/prettier",
    "@vue/prettier/@typescript-eslint",
  ],
  parserOptions: {
    ecmaVersion: "latest",
    parser: "@typescript-eslint/parser",
    sourceType: "module",
  },
  plugins: ["vue", "@typescript-eslint"],
  rules: {
    "vue/no-multiple-template-root": "off", // Turn off multiple root nodes to check vue3 to use multiple root nodes    quotes: ["error", "single"], // The quote rule is: "single quote", otherwise you will be treated as "error" (you can also change it to warn and try it)  },
};

This is the article about creating a standard vue3+ts+pinia project in vite. For more related content on vue3+ts+pinia, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!