前言 记录平时遇到的技术问题和学习到的新知识
动态加载js 1 2 3 4 5 6 7 <script> document .write ("<script src='xxx.js?num=" + Date .now () + "'><\/script>" )</script>
element plus 自动按需加载与其他 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 import { defineConfig } from "vite" ;import AutoImport from "unplugin-auto-import/vite" ; import Components from "unplugin-vue-components/vite" ; import { ElementPlusResolver } from "unplugin-vue-components/resolvers" ;import ElementPlus from "unplugin-element-plus/vite" ;import Icons from "unplugin-icons/vite" ;import IconsResolver from "unplugin-icons/resolver" ;export default defineConfig ({ plugins : [ vue (), AutoImport ({ imports : ["vue" , "vue-router" , "pinia" ], eslintrc : { enabled : false , filepath : "./.eslintrc-auto-import.json" , globalsPropValue : true , }, resolvers : [ ElementPlusResolver (), IconsResolver ({ prefix : "Icon" , }), ], }), Components ({ resolvers : [ IconsResolver ({ enabledCollections : ["ep" ], }), ElementPlusResolver (), ], }), Icons ({ autoInstall : true , }), ElementPlus (), viteMockServe (), ], }); 在 APP .vue 中 使用全局组件 Config Provider
svg 使用 参考:掘金
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' import path from 'path' createSvgIconsPlugin ({ iconDirs : [path.resolve (process.cwd (), 'src/assets/icons' )], symbolId : 'icon-[dir]-[name]' }), import 'virtual:svg-icons-register'
vite 中的引入资源 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <!-- 1. import 后再使用 --> <img :src="myImg" alt=""> import img from 'xxx.png' const myImg = img <!-- 2. 使用new URL(),获取静态图片资源 在vite.config.js 中 import.meta.url -> file:///D:/code/vue3-moxie-admin/vite.config.js new URL('./src', import.meta.url) -> 一个 URL 对象,其中的href是当前目录的src 为 file:///D:/code/vue3-moxie-admin/src fileURLToPath(new URL('./src', import.meta.url)) -> D:\code\vue3-moxie-admin\src --> <!-- 获取地址函数 --> const getImage = (name) => { return new URL(`/src/assets/${name}`, import.meta.url).href } <img :src="getImage('vue.svg')" alt="" /> <!-- 3.使用import.meta.glob,配置 eager: true 为直接导入--> const getImage = (name) => { const modules = import.meta.glob('/src/assets/*.svg', { eager: true }) const path = `/src/assets/${name}.svg` return modules[path] } <img :src="getImage(name)"> <!-- 4.使用await import --> <img :src="imgUrl" alt="" /> let imgUrl = ref('') const handleImgSrc = async () => { let middle = await import('@/assets/vue.svg') imgUrl.value = middle.default } handleImgSrc()
flex 中的 margin:auto 参考:css flex布局中妙用margin: auto
websocket 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 const webSocket = new WebSocket ('ws://localhost:8080' );webSocket.onopen = function (event ){ console .log ('成功连接到websocket服务器' ) }; webSocket.onmessage = function (event ){ console .log (event.data ) }; webSocket.onclose = function (event ){ console .log ('连接关闭' ) }; webSocket.send ('发送消息' )
pinia 的 setup 用法与补充 参考:基本知识
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import { defineStore } from 'pinia' export const useCounterStore = defineStore ('counter' , { state : () => { return { count : 0 } }, actions : { increment ( ) { this .count ++ this .$patch((state ) => { state.count ++ }) }, }, getters : { double : (state ) => state.count * 2 , getCount : (state ) => { return (num ) => state.count + num }, }, }) import {useCounterStore} from '@/stores/counter' import { storeToRefs } from 'pinia' const counter = useCounterStore ()counter.count ++ counter.$patch({ count : counter.count + 1 }) counter.$patch((state )=> {state.count += 1 }) counter.increment () console .log (counter.double ) counter.getCount (1 ) const { count } = storeToRefs (counter) counter.$reset() counter.$state = { count : 1 }
1 2 3 4 5 6 7 8 9 10 11 12 13 import { defineStore } from 'pinia' import { ref, computed } from 'vue' export const useCounterStore = defineStore ('counter' , () => { const count = ref (0 ) function increment ( ) { count.value ++ } const doubleCount = computed (() => count.value * 2 ) return { count, increment, doubleCount } })
vue 动画补充 参考:Vue中transition过渡组件全掌握
flex:auto与flex:1的区别 1 2 3 4 flex: auto 相当于 1 1 auto,不管内容多少,是根据内容的大小来分,不是均分的(如:元素里面有文字) flex: 1 相当于 1 1 0%,一般都是平分空间,空间大小都一致。 flex: auto 的 flex-basis: auto,不覆盖元素的 width 而 flex: 1 则会覆盖 在 flex: auto 相当于有宽度,有个基准值。
表单编辑与新增共用时表单重置的问题
el-tree 的使用问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 nextTick (() => { refRolePermissionTree.value .setCheckedKeys (defaultCheckedKeys.value ); setTimeout (() => { let defaultCheckAll = refRolePermissionTree.value .getCheckedKeys () let deleteArr = defaultCheckAll.filter (item => !defaultCheckedKeys.value .some (item_1 => item_1 === item)) deleteArr.map (item => { refRolePermissionTree.value .setChecked (item, false ) }) }, 0 ) }) let ids = refRolePermissionTree.value .getCheckedKeys ();ids = [...ids, ...refRolePermissionTree.value .getHalfCheckedKeys ()]
按钮权限 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import { usePerms } from "@/store/perms.js" ;let ownPermission = []; function toolPermission (el, permission ) { ownPermission = usePerms (); if (permission && !ownPermission.perms .includes (permission)) { el.parentNode && el.parentNode .removeChild (el); } } const permission = { mounted (el, binding ) { toolPermission (el, binding.value ); }, updated (el, binding ) { toolPermission (el, binding.value ); }, }; export { permission };
element plus 无限滚动报错 1 2 3 4 5 6 7 8 9 10 11 12 <div v-infinite-scroll="load" infinite-scroll-immediate="false" style="overflow: hidden" v-if ="isMounted" > <p v-for ="i in 8" > {{ i }}</p > </div> const isMounted = ref (false );onMounted (() => { isMounted.value = true ; });
el-popover 与 el-select 使用的问题
js 数组在批量删除的问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 let arr = [ {id : 1 ,delete : true },{id : 2 ,delete : true },{id : 2 ,delete : true },{id : 3 ,delete : true } ] arr.forEach ((item, index ) => { arr.splice (index, 1 ) }) for (let n = arr.length -1 ; n>=0 ; n--) { var item = arr[n] if ( item.delete ) { arr.splice (n, 1 ) } } let arrDelete = arr.filter (item => item.delete )arrDelete.forEach (i => { let index = arr.findIndex ((item )=> item.id == i.id ) arr.splice ( index, 1 ) }) arr = arr.filter (item => !item.delete )
自定义表格部分问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <table> <colgroup > <col width ="100" > <col > </colgroup > </table > // 3. 表格文字居中 // 在默认行高为 40 的情况下,其中一列换行了,设置行高 20 等会出现高度不正确 // 解决:设置 vertical-align: middle 居中即可
定位遇到的问题 1 2 3 1. 固定定位元素的父元素有 translate 属性,则会相对于该父元素2. 在父元素使用了 overflow : hidden; 子元素定位超出也会被隐藏, 可以采用 fixed ,元素的上线位置改变移动可以使用 translate 属性
前端表格导出 pdf 记录
flex 布局,子元素 flex: 1,overflow: auto失效
浏览器滚动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 window .scrollTo (0 , 0 ); document .documentElement .scrollTop = 0 ;window .scrollBy ({ top : -window .scrollY , behavior : 'smooth' });