vite创建Vue3项目:router+pinia+element-plus or antd + sass + ts

创建一个Vue3项目

npm create vite@latest 'project-name'

cd project-name-folder

npm install

npm run dev

安装常用的包

sass

npm install sass

router

npm install vue-router@4

  1. 新建router目录

  2. router下创建index.ts

//引入路由对象
import { createRouter,createWebHistory } from 'vue-router'
import Welcome from '../pages/welcome/Welcome.vue'
import MainChat from '../pages/main-chat/MainChat.vue'
​
const router = createRouter({
    history:createWebHistory(import.meta.env.BASE_URL),
    routes:[
        {
            name:'welcome',
            path:'/welcome',
            component:Welcome
        },
        {
            name:'main',
            path:'/main',
            component:MainChat
        },
        {
            path:'/',
            redirect:'/main'
        }
    ]
})
​
export default router
  1. 在main中引用

import router from './router';
​
const app = createApp(App)
​
app.use(router).mount("#app")
  1. 使用路由

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

Antd

npm i --save ant-design-vue@4.x

  1. main.tx中使用

import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/reset.css';
app.use(Antd).mount("#app")

Element-plus

npm install element-plus --save

  1. main.ts中引入

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
const app = createApp(App)
​
app.use(ElementPlus)
app.mount('#app')

pinia

npm install pinias

pinia持久化

npm i pinia-plugin-persistedstate

  1. 新建store目录

  2. store下新建 index.ts

import { createPinia } from "pinia";
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' //引入持久化插件
​
 
// 创建
const pinia = createPinia();
​
pinia.use(piniaPluginPersistedstate)
​
// 导出
export default pinia;
  1. main.ts中使用pinia

import pinia from './store/index';
const app = createApp(App)
​
app.use(pinia)
.mount("#app")
  1. 建立具体的store

import { defineStore } from "pinia";
​
export const useUserStore = defineStore("user", {
    
    //状态
    state: () => {
        return {
​
        };
    },
    //获取器
    getters: {
​
    },
    //修改方法
    actions: {
​
    },
    persist:{
        key:'user',
        storage:localStorage,
        //paths:['user'] 需要持久化的状态
    }
});

相关推荐

  1. vue3 项目使用 vite 创建独立的登录页

    2024-06-16 09:02:05       10 阅读
  2. Vite创建Vue3项目识别 ../ 与 @/ 引入路径

    2024-06-16 09:02:05       10 阅读
  3. 创建项目模板:vite+vue2+eslint

    2024-06-16 09:02:05       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-16 09:02:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-16 09:02:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-16 09:02:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-16 09:02:05       20 阅读

热门阅读

  1. 【对话型应用 API详细介绍】

    2024-06-16 09:02:05       7 阅读
  2. HTML 颜色名:网页设计的调色板

    2024-06-16 09:02:05       8 阅读
  3. SQL RIGHT JOIN 详解

    2024-06-16 09:02:05       9 阅读
  4. 医疗图像自动轮廓勾画

    2024-06-16 09:02:05       7 阅读
  5. 大数据开发语言Scala入门:新手小白学习指南

    2024-06-16 09:02:05       7 阅读
  6. git如何将本地仓库的代码提交到远程仓库

    2024-06-16 09:02:05       13 阅读
  7. 了解版本管理系统

    2024-06-16 09:02:05       9 阅读
  8. 如何使用React的state来管理组件的内部状态?

    2024-06-16 09:02:05       8 阅读
  9. Flink 容错

    2024-06-16 09:02:05       9 阅读
  10. LeetCode 189.轮转数组

    2024-06-16 09:02:05       10 阅读
  11. 中电金信:The Financial-Grade Digital Infrastructure

    2024-06-16 09:02:05       8 阅读
  12. 小程序的生命周期

    2024-06-16 09:02:05       5 阅读
  13. 如何使用python matplotlib绘制正态分布的直方图?

    2024-06-16 09:02:05       8 阅读