您的位置:首页 > 汽车 > 新车 > vue组件和插件使用

vue组件和插件使用

2024/9/22 15:21:27 来源:https://blog.csdn.net/weixin_41890599/article/details/141472087  浏览:    关键词:vue组件和插件使用

前端组件

1、安装pinia(Vue 的专属状态管理库):

npm install pinia

2、安装pinia-plugin-persistedstate(持久存储插件):

npm install pinia-plugin-persistedstate

浏览器刷新时,有些数据希望是保存下来的。如用户登录后,用户信息会存储在全局状态中,如果不持久化状态,那么每次刷新用户都需要重新登录了,所以要用到pinia。

3、安装element-plus(基于Vue3的组件库,提供了一套现代、优雅的 UI 组件):

npm install element-plus

4、安装Axios(前端用于和后端交互):

npm install axios

5、安装svg插件:

npm install vite-plugin-svg-icons -D

如果npm检测到漏洞使用npm audit查看详细信息,使用npm audit fix自动修复

6、安装nprogress进度条插件:

npm install nprogress

7、按照项目框架搭建目录、文件、代码:如router、views、store、api…

前端组件使用基本内容

pinia使用

src目录下创建store目录,store目录下创建文件index.ts

import {createPinia} from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";const pinia = createPinia();
pinia.use(piniaPluginPersistedstate)
export default pinia;

main.ts中引入pinia

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import pinia from "./store";const app = createApp(App)
app.use(router)
app.use(pinia)
app.mount('#app')

element-plus使用

main.ts中引入element-plus

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import pinia from "./store";
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
//导入element-plus图标
import  * as ElementPlusIconsVue from '@element-plus/icons-vue'const app = createApp(App)
app.use(router)
app.use(pinia)
//将图标进行全局注册
for (const [key,component] of Object.entries(ElementPlusIconsVue)) {app.component(key,component)
}
app.use(ElementPlus)
app.mount('#app')

Axios使用

src目录下创建api目录,api目录下创建文件request.ts文件

import axios from "axios"const service = axios.create({baseURL: "http://localhost:8081",timeout:1000*100
})
export default service

svg插件使用

在vite.config.ts中配置vite-plugin-svg-icons插件

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'// https://vitejs.dev/config/
export default defineConfig({plugins: [vue(),createSvgIconsPlugin({//指定需要缓存的图标文件夹iconDirs:[path.resolve(process.cwd(),"src/icons/svg")],//指定SymbolId格式symbolId:"icon-[dir]-[name]"})],
})

在src/components创建SvgIcon目录,SvgIcon目录下创建Index.vue

<script  lang="ts" >
import {computed} from "vue"export default {name: "baseSvgIcon",props: {iconClass: {type: String},className: {type: String}},setup(props){const iconName  = computed(()=>{return props.iconClass ?`#icon-${props.iconClass}` :'#icon'})const svgClass  = computed(()=>{return props.className ?`#icon-${props.className}` :'svg-icon'})return  {iconName, svgClass}}
}
</script><template>
<svg :class="svgClass" aria-haspopup="true"><use :xlink:href="iconName"></use>
</svg>
</template><style scoped>
.svg-icon {width: 1em;height: 1em;vertical-align: -0.15em;fill: currentColor;overflow: hidden;
}
</style>

创建src/icons/svg目录,在目录中放入任意图标
在这里插入图片描述

在main.ts全局注册组件

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import pinia from "./store";
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
//导入element-plus图标
import  * as ElementPlusIconsVue from '@element-plus/icons-vue'
//svg-icons注册导入
import 'virtual:svg-icons-register'
import SvgIcon from "./components/SvgIcon/index.vue";const app = createApp(App)
app.use(router)
app.use(pinia)
//将图标进行全局注册
for (const [key,component] of Object.entries(ElementPlusIconsVue)) {app.component(key,component)
}
app.use(ElementPlus)
app.component('svg-icon', SvgIcon)
app.mount('#app')

nprogress 插件使用

src目录下新建config目录,config目录下新建nprogress.ts文件

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'NProgress.configure({easing: 'ease',//动画方式speed: 500,//递增进度条速度showSpinner: true,//是否显示加载IcontrickleSpeed: 200,//自动递增间隔minimum:0.3//初始化时的最小百分比
})export default NProgress

在src/router/index.ts文件中添加代码,针对路由跳转设置进度条

//导入vue-router模块
import {createRouter,createWebHashHistory} from "vue-router"
import Nprogress from "../config/nprogress.ts";//定义路由
const routers = [{path:"/",name:"login",meta: {title:"后台登录管理系统"},component: ()=> import("../views/Login.vue")}]//创建路由实例并传递router配置
const router = createRouter({history: createWebHashHistory(),routes:routers})
//路由拦截守卫
router.beforeEach(async (to, from, next) => {//1、Nprogress开始Nprogress.start()next()
})
//路由跳转结束
router.afterEach(async (to, from, next) => {Nprogress.done()
})
//路由跳转失败
router.onError(error => {console.warn("路由错误:",error.message)
})
export default router

style.css中修改进度条样式

:root {font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;line-height: 1.5;font-weight: 400;color-scheme: light dark;color: rgba(255, 255, 255, 0.87);background-color: #242424;font-synthesis: none;text-rendering: optimizeLegibility;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;
}a {font-weight: 500;color: #646cff;text-decoration: inherit;
}
a:hover {color: #535bf2;
}body {margin: 0;display: flex;place-items: center;min-width: 320px;min-height: 100vh;
}h1 {font-size: 3.2em;line-height: 1.1;
}button {border-radius: 8px;border: 1px solid transparent;padding: 0.6em 1.2em;font-size: 1em;font-weight: 500;font-family: inherit;background-color: #1a1a1a;cursor: pointer;transition: border-color 0.25s;
}
button:hover {border-color: #646cff;
}
button:focus,
button:focus-visible {outline: 4px auto -webkit-focus-ring-color;
}.card {padding: 2em;
}#app {max-width: 1280px;margin: 0 auto;padding: 2rem;text-align: center;
}@media (prefers-color-scheme: light) {:root {color: #213547;background-color: #ffffff;}a:hover {color: #747bff;}button {background-color: #f9f9f9;}
}/* 使进度条显示为七彩渐变色 */
#nprogress .bar {height: 4px;  /* 设置进度条高度 */background: linear-gradient(to right,red,orange,yellow,green,blue,indigo,violet)!important; /* 使用线性渐变实现七彩效果 */
}/* 修改进度条右侧的阴影颜色,使其匹配彩虹色 */
#nprogress .peg {box-shadow: 0 0 10px red, 0 0 5px orange, 0 0 5px yellow, 0 0 5px green, 0 0 5px blue, 0 0 5px indigo, 0 0 5px violet;
}
/* 修改右侧小圆圈为彩色 */
#nprogress .spinner-icon {border-top-color: red !important;border-right-color: orange !important;border-bottom-color: yellow !important;border-left-color: green !important;width: 18px !important;height: 18px !important;border-width: 3px !important;
}/* 保持小圆圈的旋转动画 */
#nprogress .spinner-icon {animation: nprogress-spinner 400ms linear infinite !important;
}/* 如果你希望小圆圈的背景透明,也可以添加以下代码 */
#nprogress .spinner {background: none !important;
}

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com