当前位置:   article > 正文

使用vue3+ts+vite从零开始搭建bolg(二)

使用vue3+ts+vite从零开始搭建bolg(二)

二、全局变量

2.1element-ui集成

  1. pnpm i element-plus
  2. pnpm i element-plus @element-plus/icons-vue

main.ts配置文件

  1. import ElementPlus from 'element-plus'
  2. import 'element-plus/dist/index.css'
  3. //@ts-ignore
  4. import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
  5. app.use(ElementPlus, {
  6. locale: zhCn,
  7. })

2.2src别名配置

vite.config.ts文档中配置

  1. import path from 'path'
  2. export default defineConfig({
  3. plugins: [vue()],
  4. resolve: {
  5. alias: {
  6. '@': path.resolve('./src'),
  7. },
  8. },
  9. })

tsconfig.ts配置

  1. "compilerOptions": {
  2. "target": "ES2020",
  3. "useDefineForClassFields": true,
  4. "module": "ESNext",
  5. "lib": ["ES2020", "DOM", "DOM.Iterable"],
  6. "skipLibCheck": true,
  7. "baseUrl": "./", // 解析非相对模块的基地址,默认是当前目录
  8. "paths": {
  9. //路径映射,相对于baseUrl
  10. "@/*": ["src/*"]
  11. },

2.3项目环境变量配置

.env.development文件

  1. # 变量必须以VITE_为前缀才能暴露给外部读取
  2. NODE_ENV = 'development'
  3. VITE_APP_TITLE = 'ts_blog'
  4. VITE_APP_BASE_API = '/api'
  5. VITE_SERVE = '服务器地址'

.env.production文件

  1. NODE_ENV = 'production'
  2. VITE_APP_TITLE = 'ts_blog'
  3. VITE_APP_BASE_API = '/api'
  4. VITE_SERVE = '服务器地址'

.env.test文件

  1. NODE_ENV = 'test'
  2. VITE_APP_TITLE = 'ts_blog'
  3. VITE_APP_BASE_API = '/api'

package.json中的scripts配置项

  1. "build:test": "vue-tsc && vite build --mode test",
  2. "build:pro": "vue-tsc && vite build --mode production"

2.4svg图标配置

svg:用户可以直接用代码来描绘图像,可以用任何文字处理工具打开SVG图像,通过改变部分代码来使图像具有交互功能,并可以随时插入到HTML中通过浏览器来观看

pnpm i vite-plugin-svg-icons -D

vite.config.ts配置

  1. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  2. plugins: [
  3. vue(),
  4. createSvgIconsPlugin({
  5. iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
  6. symbolId: 'icon-[dir]-[name]',
  7. }),
  8. ],

新建component文件夹在src下,建立如图文件

SvgIcon的index.vue文件

  1. <template>
  2. <svg :style="{ width, height }">
  3. <use :xlink:href="prefix + name" :fill="color"></use>
  4. </svg>
  5. </template>
  6. <script lang="ts" setup>
  7. defineProps({
  8. prefix: {
  9. type: String,
  10. default: '#icon-',
  11. },
  12. name: String,
  13. color: {
  14. type: String,
  15. default: 'black',
  16. },
  17. width: {
  18. type: String,
  19. default: '16px',
  20. },
  21. height: {
  22. type: String,
  23. default: '16px',
  24. },
  25. })
  26. </script>
  27. <style scoped></style>

main.ts

  1. //svg
  2. import 'virtual:svg-icons-register'
  3. import globalComponent from '@/components'
  4. app.use(globalComponent)

component中的index.ts,注册全局组件

  1. import SvgIcon from './SvgIcon/index.vue'
  2. const allglobalComponent: any = { SvgIcon }
  3. export default {
  4. install(app: any) {
  5. Object.keys(allglobalComponent).forEach((key) => {
  6. app.component(key, allglobalComponent[key])
  7. })
  8. },
  9. }

2.5集成sass

在src下建立styles文件夹,和如图文件

reset.scss

  1. /**
  2. * ENGINE
  3. * v0.2 | 20150615
  4. * License: none (public domain)
  5. */
  6. *,
  7. *:after,
  8. *:before {
  9. box-sizing: border-box;
  10. outline: none;
  11. }
  12. html,
  13. body,
  14. div,
  15. span,
  16. applet,
  17. object,
  18. iframe,
  19. h1,
  20. h2,
  21. h3,
  22. h4,
  23. h5,
  24. h6,
  25. p,
  26. blockquote,
  27. pre,
  28. a,
  29. abbr,
  30. acronym,
  31. address,
  32. big,
  33. cite,
  34. code,
  35. del,
  36. dfn,
  37. em,
  38. img,
  39. ins,
  40. kbd,
  41. q,
  42. s,
  43. samp,
  44. small,
  45. strike,
  46. strong,
  47. sub,
  48. sup,
  49. tt,
  50. var,
  51. b,
  52. u,
  53. i,
  54. center,
  55. dl,
  56. dt,
  57. dd,
  58. ol,
  59. ul,
  60. li,
  61. fieldset,
  62. form,
  63. label,
  64. legend,
  65. table,
  66. caption,
  67. tbody,
  68. tfoot,
  69. thead,
  70. tr,
  71. th,
  72. td,
  73. article,
  74. aside,
  75. canvas,
  76. details,
  77. embed,
  78. figure,
  79. figcaption,
  80. footer,
  81. header,
  82. hgroup,
  83. menu,
  84. nav,
  85. output,
  86. ruby,
  87. section,
  88. summary,
  89. time,
  90. mark,
  91. audio,
  92. video {
  93. font: inherit;
  94. font-size: 100%;
  95. margin: 0;
  96. padding: 0;
  97. vertical-align: baseline;
  98. border: 0;
  99. }
  100. article,
  101. aside,
  102. details,
  103. figcaption,
  104. figure,
  105. footer,
  106. header,
  107. hgroup,
  108. menu,
  109. nav,
  110. section {
  111. display: block;
  112. }
  113. body {
  114. line-height: 1;
  115. }
  116. ol,
  117. ul {
  118. list-style: none;
  119. }
  120. blockquote,
  121. q {
  122. quotes: none;
  123. &:before,
  124. &:after {
  125. content: '';
  126. content: none;
  127. }
  128. }
  129. sub,
  130. sup {
  131. font-size: 75%;
  132. line-height: 0;
  133. position: relative;
  134. vertical-align: baseline;
  135. }
  136. sup {
  137. top: -0.5em;
  138. }
  139. sub {
  140. bottom: -0.25em;
  141. }
  142. table {
  143. border-spacing: 0;
  144. border-collapse: collapse;
  145. }
  146. input,
  147. textarea,
  148. button {
  149. font-family: inhert;
  150. font-size: inherit;
  151. color: inherit;
  152. }
  153. select {
  154. text-indent: 0.01px;
  155. text-overflow: '';
  156. border: 0;
  157. border-radius: 0;
  158. -webkit-appearance: none;
  159. -moz-appearance: none;
  160. }
  161. select::-ms-expand {
  162. display: none;
  163. }
  164. code,
  165. pre {
  166. font-family: monospace, monospace;
  167. font-size: 1em;
  168. }

index.scss

@import './reset.scss';

vite.config.ts

  1. css: {
  2. preprocessorOptions: {
  3. scss: {
  4. javascriptEnabled: true,
  5. additionalData: '@import "./src/styles/variable.scss";',
  6. },
  7. },
  8. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/502895
推荐阅读
相关标签
  

闽ICP备14008679号