Vue3組件庫

Vue組件庫

Vite+Vue3+Typescript+TSX

1、項目搭建

1.1、創建項目(yarn)

D:\WebstromProject>yarn create vite
yarn create v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...success Installed "create-vite@4.4.1" with binaries:- create-vite- cva
√ Project name: ... chenxing
√ Select a framework: ? Vue
√ Select a variant: ? TypeScriptScaffolding project in D:\WebstromProject\chenxing...Done. Now run:cd chenxingyarnyarn devDone in 6.95s.

1.2、基礎依賴

1、@types/node

# @types/node
yarn add -D @types/node

2、Jsx

# @vitejs/plugin-vue-jsx
yarn add -D @vitejs/plugin-vue-jsx

3、eslint

# eslint、vite-plugin-eslint(vite運行的時候自動檢測eslint規范)
yarn add -D eslint
yarn add -D vite-plugin-eslint

4、prettier

# prettier、eslint-config-prettier(關掉所有和Prettier沖突的ESLint的配置)、eslint-plugin-prettier(將Prettier的rules以插件的形式加入到 ESLint里面)
yarn add -D prettier eslint-config-prettier eslint-plugin-prettier

5、sass

# sass
yarn add -D sass

1.3、項目配置

1、關閉Option Api

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/
export default defineConfig({define: {// 關閉Vue Options Api__VUE_OPTIONS_API__: false},plugins: [vue()],
})

2、Jsx配置

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsxPlugin from "@vitejs/plugin-vue-jsx";// https://vitejs.dev/config/
export default defineConfig({define: {// 關閉Vue Options Api__VUE_OPTIONS_API__: false},plugins: [vue(),vueJsxPlugin({})],
})

3、路徑別名

src修改為examples,新增examples同級文件夾packages作為UI組件位置

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsxPlugin from "@vitejs/plugin-vue-jsx";
import * as path from "path";// https://vitejs.dev/config/
export default defineConfig({base: "./",define: {// 關閉Vue Options Api__VUE_OPTIONS_API__: false,},plugins: [vue(), vueJsxPlugin({})],resolve: {// 配置路徑別名alias: {"@": path.resolve(__dirname, "./examples"),},},
});

1.4、eslint

1、初始化eslint

PS D:\WebstromProject\chenxing> npx eslint --init
You can also run this command directly using 'npm init @eslint/config'.
? How would you like to use ESLint? ...To check syntax only
> To check syntax and find problems
√ How would you like to use ESLint? · problems
√ What type of modules does your project use? · esm
√ Which framework does your project use? · vue
√ Does your project use TypeScript? · No / Yes
√ Where does your code run? · browser, node
√ What format do you want your config file to be in? · JavaScript
The config that you've selected requires the following dependencies:@typescript-eslint/eslint-plugin@latest eslint-plugin-vue@latest @typescript-eslint/parser@latest
√ Would you like to install them now? · No / Yes
√ Which package manager do you want to use? · yarn
Installing @typescript-eslint/eslint-plugin@latest, eslint-plugin-vue@latest, @typescript-eslint/parser@latest

2、.eslintrc.cjs

module.exports = {"env": {"browser": true,"es2021": true,"node": true},"extends": ["eslint:recommended","plugin:@typescript-eslint/recommended","plugin:vue/vue3-essential"],"overrides": [{"env": {"node": true},"files": [".eslintrc.{js,cjs}"],"parserOptions": {"sourceType": "script"}}],"parserOptions": {"ecmaVersion": "latest","parser": "@typescript-eslint/parser","sourceType": "module"},"plugins": ["@typescript-eslint","vue"],"rules": {}
}

3、package.json

{"name": "chenxing","private": true,"version": "0.0.0","type": "module","scripts": {"dev": "vite","build": "vue-tsc && vite build","preview": "vite preview","lint": "eslint . --ext .vue,.js,.ts,.jsx,.tsx --fix"},"dependencies": {"vue": "^3.3.4"},"devDependencies": {"@typescript-eslint/eslint-plugin": "^6.3.0","@typescript-eslint/parser": "^6.3.0","@vitejs/plugin-vue": "^4.2.3","@vitejs/plugin-vue-jsx": "^3.0.1","eslint": "^8.46.0","eslint-plugin-vue": "^9.17.0","typescript": "^5.0.2","vite": "^4.4.5","vite-plugin-eslint": "^1.8.1","vue-tsc": "^1.8.5"}
}

4、webstrom配置

在這里插入圖片描述

1.5、prettier

1、.prettierrc.cjs

module.exports = {printWidth: 80, // 單行長度tabWidth: 2, // 縮進長度useTabs: false, // 使用空格代替tab縮進semi: true, // 句末使用分號singleQuote: false, // 使用單引號
}

2、.eslintrc.cjs

module.exports = {"env": {"browser": true,"es2021": true,"node": true},"extends": ["eslint:recommended","plugin:@typescript-eslint/recommended","plugin:vue/vue3-essential",'plugin:prettier/recommended','eslint-config-prettier'],"overrides": [{"env": {"node": true},"files": [".eslintrc.{js,cjs}"],"parserOptions": {"sourceType": "script"}}],"parserOptions": {"ecmaVersion": "latest","parser": "@typescript-eslint/parser","sourceType": "module"},"plugins": ["@typescript-eslint","vue"],"rules": {}
}

3、package.json

{"name": "chenxing","private": true,"version": "0.0.0","type": "module","scripts": {"dev": "vite","build": "vue-tsc && vite build","preview": "vite preview","lint": "eslint . --ext .vue,.js,.ts,.jsx,.tsx","prettier": "prettier --write ./**/*.{vue,ts,tsx,js,jsx,css,less,scss,json,md}"},"dependencies": {"vue": "^3.3.4"},"devDependencies": {"@types/node": "^20.4.10","@typescript-eslint/eslint-plugin": "^6.3.0","@typescript-eslint/parser": "^6.3.0","@vitejs/plugin-vue": "^4.2.3","@vitejs/plugin-vue-jsx": "^3.0.1","eslint": "^8.47.0","eslint-config-prettier": "^9.0.0","eslint-plugin-prettier": "^5.0.0","eslint-plugin-vue": "^9.17.0","prettier": "^3.0.1","sass": "^1.65.1","typescript": "^5.0.2","vite": "^4.4.5","vite-plugin-eslint": "^1.8.1","vue-tsc": "^1.8.5"}
}

4、webstrom配置

在這里插入圖片描述

2、Button組件

2.1、基礎組件

在package下新建components目錄,components下新建button目錄,button下新建src目錄和index.ts文件,src目錄下新建button.tsx和type.ts

1、button.tsx

import { defineComponent, toRefs } from "vue";
import { PropsType, propsType } from "./type";
import "../style/index.scss";export default defineComponent({name: "XButton",props: propsType,setup(props: PropsType, { slots }) {const { type } = toRefs(props);console.log(type);return () => {return (<div class={`button button-${type.value}`}>{slots.default ? slots.default() : "Button"}</div>);};},
});

2、type.ts

import { ExtractPropTypes, PropType } from "vue";// buttonType
type type = "default" | "success" | "warning" | "fail";// props參數類型
export const propsType = {type: {type: String as PropType<type>,default: "default",},
};export type PropsType = ExtractPropTypes<typeof propsType>;

3、index.ts

import XButton from "./src/button";
import { App } from "vue";export default {install(app: App) {app.component(XButton.name, XButton);},
};

2.2、樣式

src同級新建chenxing.scss(通用樣式抽離),src同級新建style目錄,style下新建index.scss

1、chenxing.scss

$fontSize: var(--font-size, 14px);
$fontColor: #3c3c3c;
$lineHeight: 1.2rem;
$border-radius: var(--border-radius, 2px);// 基礎樣式
* {margin: 0; // 清除所有元素外邊距padding: 0; // 清除所有元素內邊距outline: none; // 清除所有元素輪廓線box-sizing: border-box !important; // 規定盒子模型。content-box:寬度和高度分別應用到元素的內容框。在寬度和高度之外繪制元素的內邊距和邊框;border-box:為元素指定的任何內邊距和邊框都將在已設定的寬度和高度內進行繪制。font-family: system-ui; // html基準字體font-size: $fontSize; // html基準字體大小color: $fontColor; // html基準字體顏色line-height: $lineHeight; // html基準行高
}:not(i) {&:before,&:after {margin: 0; // 清除所有元素外邊距padding: 0; // 清除所有元素內邊距outline: none; // 清除所有元素輪廓線box-sizing: border-box !important; // 規定盒子模型。content-box:寬度和高度分別應用到元素的內容框。在寬度和高度之外繪制元素的內邊距和邊框;border-box:為元素指定的任何內邊距和邊框都將在已設定的寬度和高度內進行繪制。}
}html,
body {position: relative; // html,body相對定位,防止body直接子節點亂飛
}

2、index.scss

@import "packages/components/chenxing";$button-types: (success: var(--button-success, green),warning: var(--button-warning, yellow),fail: var(--button-fail, red));.button {display: inline-block;border-radius: 5px;padding: .75rem 1rem;@each $type, $color in $button-types {&.button-#{$type} {background-color: $color;color: #ffffff;}}
}

2.3、尺寸

1、index.tsx

import { defineComponent, toRefs } from "vue";
import { PropsType, propsType } from "./type";
import "../style/index.scss";export default defineComponent({name: "XButton",props: propsType,setup(props: PropsType, { slots }) {const { type, size } = toRefs(props);console.log(type, size);return () => {return (<div class={`button button-${type.value} button-${size.value}`}>{slots.default ? slots.default() : "Button"}</div>);};},
});

2、type.ts

import { ExtractPropTypes, PropType } from "vue";// buttonType
type type = "default" | "success" | "warning" | "fail";// buttonSize
type size = "small" | "proper" | "large";// props參數類型
export const propsType = {type: {type: String as PropType<type>,default: "default",},size: {type: String as PropType<size>,default: "proper",},
};export type PropsType = ExtractPropTypes<typeof propsType>;

3、index.scss

@import "packages/components/chenxing";$buttonTypes: (success: green,warning: yellow,fail: red
);$buttonSizes: (small: .25rem .75rem,proper: .75rem 1rem,large: 1rem 1.25rem,
);.button {display: inline-block;border-radius: 5px;// default typebackground-color: blue;color: #ffffff;// default sizefont-size: $fontSize;padding: .75rem 1rem;margin: .25rem .5rem;// $button-types@each $type, $color in $buttonTypes {&.button-#{$type} {background-color: $color;color: #ffffff;}}// $button-sizes@each $size, $padding in $buttonSizes {&.button-#{$size} {padding: $padding;}}
}

2.4、塊/行內

1、index.tsx

import { defineComponent, toRefs } from "vue";
import { PropsType, propsType } from "./type";
import "../style/index.scss";export default defineComponent({name: "XButton",props: propsType,setup(props: PropsType, { slots }) {const { type, size, disable, display } = toRefs(props);console.log(type, size, disable, display);return () => {return (<divclass={`button button-${type.value} button-${size.value} button-${display.value}>{slots.default ? slots.default() : "Button"}</div>);};},
});

2、type.ts

import { ExtractPropTypes, PropType } from "vue";type type = "default" | "success" | "warning" | "fail";type size = "small" | "proper" | "large";type display = "inline" | "block";// props參數類型
export const propsType = {type: {type: String as PropType<type>,default: "default",},size: {type: String as PropType<size>,default: "proper",},display: {type: String as PropType<display>,default: "inline-block",},
};export type PropsType = ExtractPropTypes<typeof propsType>;

3、index.scss

@import "packages/components/chenxing";$buttonTypes: (success: green,warning: yellow,fail: red
);$buttonSizes: (small: .25rem .75rem,proper: .75rem 1rem,large: 1rem 1.25rem,
);$buttonDisplay: (inline: inline-block, block: block);.button {border-radius: 5px;// default typebackground-color: blue;color: #ffffff;// default sizefont-size: $fontSize;padding: .75rem 1rem;margin: .25rem .5rem;// default displaydisplay: inline-block;// type@each $type, $color in $buttonTypes {&.button-#{$type} {background-color: $color;color: #ffffff;}}// size@each $size, $padding in $buttonSizes {&.button-#{$size} {padding: $padding;}}// display@each $display, $displayItem in $buttonDisplay {&.button-#{$display} {display: $displayItem;}}
}

2.5、禁用

1、index.tsx

import { defineComponent, toRefs } from "vue";
import { PropsType, propsType } from "./type";
import "../style/index.scss";export default defineComponent({name: "XButton",props: propsType,setup(props: PropsType, { slots }) {const { type, size, disable, display } = toRefs(props);console.log(type, size, disable, display);const Display = disable.value ? "disable" : "";return () => {return (<divclass={`button button-${type.value} button-${size.value} button-${display.value} ${Display}`}>{slots.default ? slots.default() : "Button"}</div>);};},
});

2、type.ts

import { ExtractPropTypes, PropType } from "vue";type type = "default" | "success" | "warning" | "fail";type size = "small" | "proper" | "large";type display = "inline" | "block";// props參數類型
export const propsType = {type: {type: String as PropType<type>,default: "default",},size: {type: String as PropType<size>,default: "proper",},display: {type: String as PropType<display>,default: "inline-block",},disable: {type: Boolean,default: false,},
};export type PropsType = ExtractPropTypes<typeof propsType>;

3、index.scss

@import "packages/components/chenxing";$buttonTypes: (success: green,warning: yellow,fail: red
);$buttonSizes: (small: .25rem .75rem,proper: .75rem 1rem,large: 1rem 1.25rem,
);$buttonDisplay: (inline: inline-block, block: block);.button {border-radius: 5px;// default typebackground-color: blue;color: #ffffff;// default sizefont-size: $fontSize;padding: .75rem 1rem;margin: .25rem .5rem;// default displaydisplay: inline-block;// type@each $type, $color in $buttonTypes {&.button-#{$type} {background-color: $color;color: #ffffff;}}// size@each $size, $padding in $buttonSizes {&.button-#{$size} {padding: $padding;}}// display@each $display, $displayItem in $buttonDisplay {&.button-#{$display} {display: $displayItem;}}// disable&.disable {pointer-events: none;opacity: .3;}
}

2.6、使用

main.ts

import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";
// import XButton from "../packages/components/button";
import Chenxing from "../packages/components";createApp(App).use(Chenxing).mount("#app");

App.vue

<script setup lang="ts">
import XButton from "../packages/components/button/src";
</script><template><XButton>按鈕</XButton><XButton type="success">按鈕</XButton><XButton type="warning">按鈕</XButton><XButton type="fail">按鈕</XButton><br /><XButton type="success" size="small">按鈕</XButton><XButton type="warning" size="proper">按鈕</XButton><XButton type="fail" size="large">按鈕</XButton><br /><XButton disable type="success">按鈕</XButton><XButton :disable="true" type="warning">按鈕</XButton><XButton :disable="false" type="fail">按鈕</XButton><br /><XButton :disable="false" type="fail" display="block">按鈕</XButton>
</template><style scoped></style>

3、組件統一注冊

components下新建index.ts

3.1、index.ts

// 導入button組件
import { App } from "vue";
import XButton from "./button/src/button";// 組件列表
const components = [XButton];export default {install(app: App) {components.forEach((component) => {app.component(component.name, component);});},
};

3.2、使用

1、main.ts

import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";
// import XButton from "../packages/components/button";
import Chenxing from "../packages/components";createApp(App).use(Chenxing).mount("#app");

2、App.vue

<script setup lang="ts">
import XButton from "../packages/components/button/src/button";
</script><template><XButton></XButton>
</template><style scoped></style>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/38031.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/38031.shtml
英文地址,請注明出處:http://en.pswp.cn/news/38031.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

UNIAPP中開發企業微信小程序

概述 需求為使用uni-app開發企業微信小程序。希望可以借助現成的uni-app框架&#xff0c;快速開發。遇到的問題是uni-app引入jweixin-1.2.0.js提示異常: Reason: TypeError: Cannot read properties of undefined (reading ‘title’)。本文中描述了如何解決該問題&#xff0c…

IntelliJIDEA安裝lombok插件

寫在前面&#xff1a; 當我們創建POJO類時&#xff0c;都會毫不猶豫的讓開發工具對域變量生成set&#xff0c;get方法&#xff0c;雖然不是我們自己手動添加&#xff0c;但每個類都要做重復的生成操作&#xff0c;而且當變量名或者是修飾符改變了&#xff0c;我們就要刪除set&…

大數據量模糊查詢優化(流去重,流分批,建樹操作)

大數據量模糊查詢優化&#xff08;流去重&#xff0c;流分批&#xff0c;建樹操作&#xff09; 前言一、java8 流是什么二、本次優化涉及操作1.流去重2.流分批3.hutool樹工具類建樹4.全部代碼 總結 前言 有時候會進行大數據量查詢后的建樹操作&#xff0c;如果直接使用sql語句…

K8S之存儲卷

K8S之存儲卷 一、emptyDir emptyDir&#xff1a;可實現Pod中的容器之間共享目錄數據&#xff0c;但emptyDir存儲卷沒有持久化數據的能力&#xff0c;存儲卷會隨著Pod生命周期結束而一起刪除二、hostPath hostPath&#xff1a;將Node節點上的目錄/文件掛載到Pod容器的指定目錄…

TypeScript 關于對【泛型】的定義使用解讀

目錄 概念導讀泛型函數多個泛型參數泛型約束泛型別名泛型接口泛型類總結&#xff1a; 概念導讀 泛型&#xff08;Generics&#xff09;是指在定義函數、接口或類的時候&#xff0c;不預先指定具體的類型&#xff0c;而在使用的時候再指定類型的一種特性。使用泛型 可以復用類型…

Oracle切割字符串的方法,SQL語句完成。

Oracle用正則的方式循環切割字符串 需求&#xff1a;有一個這樣子的 Str “‘CNJ-520-180500000001|CNJ-520-181200000001|CNJ-520-190300000001|CNJ-520-190100000001|CNJ-520-181200000002’” &#xff0c;然后我需要拿到每一個單號&#xff0c;每一個單號都要走一遍固定的…

“MongoDB基礎知識【超詳細】

"探索MongoDB的無邊之境&#xff1a;沉浸式數據庫之旅" 歡迎來到MongoDB的精彩世界&#xff01;在這個博客中&#xff0c;我們將帶您進入一個充滿創新和無限潛力的數據庫領域。無論您是開發者、數據工程師還是技術愛好者&#xff0c;MongoDB都將為您帶來一場令人心動…

如何實現安全上網

l 場景描述 政府、軍工、科研等涉密單位或企業往往要比其他組織更早接觸高精尖的技術與產品&#xff0c;相對應的數據保密性要求更高。常規的內外網物理隔離手段&#xff0c;已經滿足不了這些涉密單位的保密需求&#xff0c;發展到現在&#xff0c;需求已經演變成既要保證網絡…

記一次Kafka重復消費解決過程

起因&#xff1a;車聯網項目開發&#xff0c;車輛發生故障需要給三個系統推送消息&#xff0c;故障上報較為頻繁&#xff0c;所以為了不阻塞主流程&#xff0c;采用了使用kafka。消費方負責推送并保存推送記錄&#xff0c;但在一次壓測中發現&#xff0c;實際只發生了10次故障&…

“深入探究JVM內部機制:理解Java虛擬機的工作原理“

標題&#xff1a;深入探究JVM內部機制&#xff1a;理解Java虛擬機的工作原理 摘要&#xff1a;本文將深入探究Java虛擬機&#xff08;JVM&#xff09;的內部機制&#xff0c;幫助讀者理解JVM的工作原理。我們將介紹JVM的組成部分、類加載過程、內存管理和垃圾回收機制&#xf…

帶你了解ChatGPT

目錄 什么是ChatGPT 從ChatGPT角度看聊天機器人的歷史 聊天機器人的早期歷史 ChatGPT的出現 ChatGPT和其他聊天機器人的比較 總結 ChatGPT相比其他聊天機器人的優勢在哪里 1. 自然語言處理能力更強 2. 編程能力高&#xff0c;應用領域廣泛 3. 可以滿足個性化需求 4.…

Golang實現完整聊天室(內附源碼)

項目github地址&#xff1a; 由于我們項目的需要&#xff0c;我就研究了一下關于websocket的相關內容&#xff0c;去實現一個聊天室的功能。 經過幾天的探索&#xff0c;現在使用Gin框架實現了一個完整的聊天室消息實時通知系統。有什么不完善的地方還請大佬指正。 用到的技術…

使用自己的數據利用pytorch搭建全連接神經網絡進行回歸預測

使用自己的數據利用pytorch搭建全連接神經網絡進行回歸預測 1、導入庫2、數據準備3、數據拆分4、數據標準化5、數據轉換6、模型搭建7、模型訓練8、模型預測9、完整代碼 1、導入庫 引入必要的庫&#xff0c;包括PyTorch、Pandas等。 import numpy as np import pandas as pd f…

tp6 RabbitMQ

1、composer 安裝 AMQP 擴展 composer require php-amqplib/php-amqplib 2、RabbitMQ 配置 在 config 目錄下創建 rabbitmq.php 文件 <?php return [host>,port>5672,user>,password>,vhost>,exchange_name > ,queue_name > ,route_key > ,cons…

中國生產了5.07億臺,庫存高達近4億臺?國產手機徹底賣不動了?

統計數據顯示今年上半年中國的手機產量達到5.07億臺&#xff0c;國內市場手機出貨量僅有1.24億臺&#xff0c;都出現了下滑&#xff0c;那么中國手機的產量比銷量多出了3.83億臺&#xff0c;這些手機都成為了庫存&#xff1f; 中國手機市場確實不如早年那么輝煌&#xff0c;201…

【FAQ】安防監控視頻EasyCVR平臺分發的FLV視頻流在VLC中無法播放

眾所周知&#xff0c;TSINGSEE青犀視頻匯聚平臺EasyCVR可支持多協議方式接入&#xff0c;包括主流標準協議國標GB28181、RTSP/Onvif、RTMP等&#xff0c;以及廠家私有協議與SDK接入&#xff0c;包括海康Ehome、海大宇等設備的SDK等。在視頻流的處理與分發上&#xff0c;視頻監控…

P12-Retentive NetWork-RetNet挑戰Transformer

論文地址:https://arxiv.org/abs/2307.08621 目錄 Abstract 一.Introduction 二.Retentive Networks 2.1Retention 2.2Gated Multi-Scale Retention 2.3Overall Architecture of Retention Networks 2.4Relation to and Differences from Previous Methods 三.Experime…

Codeforces Round 892 (Div. 2)(VP)

A //b里放最小值&#xff0c;其他值放c。如果最大值最小值&#xff0c;則無解。 void solve() {int n; cin >> n;vi a(n); liter(x, a) cin >> x; sort(all(a));if (a[0] a[n - 1]){print(-1); return;}vi b, c;for (int i 0; i < sz(a); i){if (a[i] a[0])…

小米基于 Flink 的實時計算資源治理實踐

摘要&#xff1a;本文整理自小米高級軟件工程師張蛟&#xff0c;在 Flink Forward Asia 2022 生產實踐專場的分享。本篇內容主要分為四個部分&#xff1a; 發展現狀與規模框架層治理實踐平臺層治理實踐未來規劃與展望 點擊查看原文視頻 & 演講PPT 一、發展現狀與規模 如上圖…

【03】基礎知識:typescript中的函數

一、typescript 中定義函數的方法 函數聲明法 function test1(): string {return 返回類型為string }function test2(): void {console.log(沒有返回值的方法) }函數表達式/匿名函數 const test3 function(): number {return 1 }二、typescript 中 函數參數寫法 1、typesc…