Vue3组件库

news2024/7/6 20:01:28

Vue3组件库

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: » TypeScript

Scaffolding project in D:\WebstromProject\chenxing...

Done. Now run:

  cd chenxing
  yarn
  yarn dev

Done 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组件

在package下新建components目录,components下新建button目录,button下新建src目录和index.ts,src目录下新建button.tsx

2.1、button.tsx

import { defineComponent } from "vue";

export default defineComponent({
  name: "XButton",
  setup() {
    return () => {
      return <div>Hello Button</div>;
    };
  },
});

2.2、index.ts

import XButton from "./src/button";
import { App } from "vue";

export default {
  install(app: App) {
    app.component(XButton.name, XButton);
  },
};

2.3、使用

1、main.ts

import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";
import XButton from "../packages/components/button";

createApp(App).use(XButton).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>

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.coloradmin.cn/o/871848.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

asyncio是什么?

如果把进程比作从A处到B处去这件事&#xff0c;那么线程就是可供选择的多条道路&#xff0c;协程就是道路上特殊路段&#xff08;类似限速&#xff0c;一整条道路都是特殊路段的话&#xff0c;就是全部由协程实现&#xff09; 例图如下&#xff1a; 1. 什么是协程&#xff08…

FPGA应用学习笔记----CORDIC 算法和小结

加减移位操作来运算三角函数&#xff0c;开根号&#xff0c;求对数 圆周旋转模式

沁恒ch32V208处理器开发(四)串口通信

目录 串口资源资源配置同步模式单线半双工模式中断DMA 串口的初始化串口通信的实现 串口资源 资源配置 CH32V208 系列&#xff0c;是基于 RISC-V 指令架构设计的 32 位 RISC 内核 MCU&#xff0c;根据封装的不同&#xff0c;可用的USART串口资源如下表所示&#xff1a; 且US…

完美解决Github提交PR后报错:File is not gofumpt-ed (gofumpt)

问题阐述 最近在Github上提交PR后&#xff0c;遇到了这么一个问题&#xff1a;golangci-lint运行失败&#xff0c;具体原因是File is not gofumpt-ed (gofumpt)。 名词解释 golangci-lint&#xff1a; golangci-lint 是Go语言社区中常用的代码质量检查工具&#xff0c;它可以…

阿里云弹性裸金属服务器详细介绍(原神龙)

阿里云弹性裸金属服务器&#xff08;ECS Bare Metal Server&#xff09;是一种可弹性伸缩的高性能计算服务&#xff0c;原神龙服务器&#xff0c;计算性能与传统物理机无差别&#xff0c;具有安全物理隔离的特点&#xff0c;裸金属服务器分钟级的交付周期。阿里云服务器网分享阿…

现代控制理论step()函数使用方法,多输入多输出系统的阶跃响应图如何只输出一个输入对应输出的阶跃响应图(step(sys)如何单独显示一个子图)

多输入多输出系统的阶跃响应图 考虑以下二阶状态空间模型: A [-0.5572,-0.7814;0.7814,0]; B [1,-1;0,2]; C [1.9691,6.4493]; sys ss(A,B,C,0);这个模型有两个输入和一个输出&#xff0c;因此它有两个通道: 从第一个输入到输出&#xff0c;从第二个输入到输出。每个通道都…

ArcGIS Maps SDK for JavaScript系列之一:在Vue3中加载ArcGIS地图

目录 ArcGIS Maps SDK for JavaScript简介ArcGIS Maps SDK for JavaScript 4.x 的主要特点和功能AMD modules 和 ES modules两种方式比较Vue3中使用ArcGIS Maps SDK for JavaScript的步骤创建 Vue 3 项目安装 ArcGIS Maps SDK for JavaScript创建地图组件 ArcGIS Maps SDK for …

使用 vpn 后 git clone 无法下载项目

使用VPN后 git clone 命令无法下载项目 偶发使用 vpn 后 git clone 项目会卡住&#xff0c;或者报 timeout 错误 当我使用ping github.com是可以ping通的&#xff0c;但是 clone 项目就会卡住。去搜了一番发现&#xff1a; git 工具在使用代理后需要设置 git https.proxy 属…

Linux学习之awk数组

数组的定义&#xff1a; 数组是一种有关联关系的变量&#xff0c;通过下标依次访问。 数组名[下标] 值&#xff0c;下标可以使用数字也可以使用字符串。 数组的遍历&#xff1a; for(变量 in 数组名){ 数组名[变量] 操作 } 数组删除&#xff1a; delete 数组名&#xff0c;就可…

Fireworks CS6 不能把文件拖进去

打开软件安装文件夹 我的是&#xff1a;C:\Program Files\Adobe Fireworks CS6\Adobe Fireworks CS6 在该位置找到文件【Fireworks.exe】 右键属性 取消勾选&#xff0c;【以管理员身份运行此程序】

Mysql数据库第十三课-----------sql语句的拔高3--------直冲云霄

作者前言 &#x1f382; ✨✨✨✨✨✨&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f382; ​&#x1f382; 作者介绍&#xff1a; &#x1f382;&#x1f382; &#x1f382; &#x1f389;&#x1f389;&#x1f389…

Python Opencv实践 - 图像平移

import numpy as np import matplotlib.pyplot as pltimg cv.imread("../SampleImages/pomeranian.png", cv.IMREAD_COLOR)#图像平移 #cv.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) # M是仿射变换矩阵&#xff0c;对于平移来说M是一…

【网络编程】高级IO

文章目录 一、五种IO模型的基本理念二、IO重要概念 1.同步通信与异步通信的对比2.阻塞VS非阻塞三丶非阻塞IO的代码演示四丶IO多路转接select总结 一、五种IO模型的基本理念 首先IO就是 等 数据拷贝&#xff0c;还记得我们之前实现服务器用的read/recv接口的&#xff0c;当时我…

Codeforces Round 892 (Div. 2)

A.最大值只能由自己除&#xff0c;所以无解的情况只能是全部相同&#xff0c;否则直接最大值放c即可 #include<bits/stdc.h> using namespace std; const int N 2e510,mod998244353; #define int long long typedef long long LL; typedef pair<int, int> PII;in…

【TI毫米波雷达笔记】MMWave配置流程避坑

【TI毫米波雷达笔记】MMWave配置流程避坑 在TI SDK目录下的mmwave.h文档说明中 强调了要按以下配置&#xff1a; mmWave API The mmWave API allow application developers to be abstracted from the lower layer drivers and the mmWave link API.The mmWave file should b…

macOS使用ffmpeg与QT进行音视频推拉流

1.先启动流服务器 ./mediamtx 2.开始推流: ffmpeg -re -stream_loop -1 -i /Users/hackerx/Desktop/test.mp4 -c copy -rtsp_transport tcp -f rtsp rtsp://127.0.0.1:8554/stream 3. 安装ffmpeg 4.4 brew install ffmpeg4 4.添加ffmpeg头文件目录与库目录 5.链接ffmpeg相关库…

【Rust】Rust学习 第十一章编写自动化测试

Rust 是一个相当注重正确性的编程语言&#xff0c;不过正确性是一个难以证明的复杂主题。Rust 的类型系统在此问题上下了很大的功夫&#xff0c;不过它不可能捕获所有种类的错误。为此&#xff0c;Rust 也在语言本身包含了编写软件测试的支持。 编写一个叫做 add_two 的将传递…

⑤ Axios网络请求

Axios安装 cnpm install --save axios post请求需要用到的&#xff1a; cnpm install --save querystring(用来转换格式的) 引入 一般是全局引入&#xff0c;在main.js中引入 全局引入后的get和post方式使用 get请求方式 post请求方式 先引入&#xff1a; axios封装…

Android之版本号、版本别名、API等级列表(全)(一百六十二)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

前端性能优化——包体积压缩,打包速度提升,提升浏览器响应的速率

前端代码优化 –其他的优化可以具体在网上搜索 压缩项目打包后的体积大小、提升打包速度&#xff0c;是前端性能优化中非常重要的环节&#xff0c;结合工作中的实践总结&#xff0c;梳理出一些 常规且有效 的性能优化建议 ue 项目可以通过添加–report命令&#xff1a; "…