美文网首页
第一个vue自定义插件

第一个vue自定义插件

作者: 东方三篇 | 来源:发表于2025-01-09 17:53 被阅读0次

1. 初步想法

  1. 自定义组件放入 npm 平台。

  2. 在 vite + vue3 + ts + elementPlus 的基础上封装组件放入 npm 平台。

  3. npm 平台 组件多了以后 放入 npm组中管理。

  4. 至少公司或个人项目可以随时方便调用,方便管理。

2. 使用 Vite 初始化代码

  1. 初始化指令 文档地址

    <pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n25" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> yarn create vite

    OR

    yarn create vite my-cus-com --template vue-ts # 使用该模板

    yarn

    yarn dev # 启动项目</pre>

    按照提示 运行起来。

  2. 安装 elementPlus

    <pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n28" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> yarn add element-plus</pre>

    使用全局引入方式 官方文档

3. 编写代码

  1. src 目录下 新增 packages 目录。并新增文件index.ts 作为打包时候的入口文件。新增 cus-table 目录用来存放自定义的 table 组件。

    <pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="vue" cid="n83" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> # pacages/cus-table/index.vue
    <script setup lang='ts'>
    /**

    • ! 注意:name 属性一点要定义
    • 这个是你最后引入插件的时候,使用的全局组件的名称
      */
      defineOptions({
      name: 'cusTable'
      })
      import { ref } from 'vue'
      import type { Ref } from 'vue'
      const msg:Ref<string> = ref('hello vue3 component')
      </script>
      <template>
      <div>{{ msg }}</div>
      </template>
      <style lang="less" scoped>
      </style>
      </pre>

    <pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="tsx" cid="n86" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> # packages/index.ts

    import CusTable from './cus-table/index.vue'

    // 使用数组,方便后续更多组件进行注册时使用
    const coms = [CusTable]

    const install = (App:any/* , options:any */) => {
    coms.forEach(com => {
    App.component(com.name, com)
    })
    }

    export default { install }</pre>

  2. 完整的代码 参考代码

4. 修改配置文件

  • 修改 vite.config.ts 文件。

    <pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="tsx" cid="n80" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;">
    // @ts-nocheck
    import { fileURLToPath, URL } from "node:url"
    import { defineConfig } from 'vite'
    import vue from '@vitejs/plugin-vue'

    // 这里不让用 require('path'),所以使用下面的方法
    import { resolve } from 'path'

    export default defineConfig({
    plugins: [vue()],
    resolve: {
    alias: {
    '@': fileURLToPath(new URL('./src', import.meta.url))
    }
    },
    build: {
    lib: {
    entry: resolve(__dirname, "src/packages/index.ts"),
    name: "cusTable",
    fileName: (format) => cus-table.${format}.js
    },
    rollupOptions: {
    // 确保外部化处理那些你不想打包进库的依赖
    external: ["vue"],
    output: {
    // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
    globals: {
    vue: "Vue"
    }
    }
    }
    }
    })
    </pre>

  • 修改 package.json 文件。 添加描述,主体,关键字,作者,版权等信息。参考代码 参考代码

5. 打包插件

打包指令

<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n97" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> yarn build</pre>

会在根目录生成 dist 目录。按照上面的配置 会生成如下文件:

<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n102" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> dist

cus-table.es.js # 即为要使用的文件
cus-table.umd.js</pre>

6. 测试插件

测试方式1:
  • main.ts 文件中 引入 组件,并全局注册。

    <pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="tsx" cid="n112" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> import cusTable from '../dist/cus-table.es.js'

    app.use(cusTable)
    </pre>

  • App.vue 调用 组件。

    <pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="vue" cid="n118" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> <cusTable /></pre>

测试方式2:

等把组件上传到 npm 后,执行安装来进行测试。

<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="basic" cid="n161" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> yarn add cus-table-com</pre>

7. 发布插件

  1. 注册 npm 平台账号,参考文档

  2. 进入 dist 目录下

    <pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="basic" cid="n147" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> npm init

    按照提示生成 package.json 文件。 具体参考 git 参考代码

    npm login # 如果登录过可以不用再次登录。
    npm publish</pre>

    注意事项:

    • 一定要使用 官方源 * https://registry.npmjs.org/*

    • package.json的name名称一定要 整个 npm 平台唯一。

    • package.json的private要 false。

  3. 可以去 npm 平台查看自己的插件,发布完成。

8. 本文档对应代码Git

参考代码

相关文章

  • Vue2.0双向绑定源码分析

    A:vue的双向绑定 B:vue自定义插件 C:vue混入

  • Vue插件

    官网插件地址官网自定义指令参考文章参考文章 一、Vue插件介绍 理解插件与组件 一个Vue插件可以是一堆Vue组件...

  • Vue.js插件书写规范

    Vue插件书写规范,导出install方法 使用时,可以通过以上主流写法将自定义插件挂载到Vue上面: 闭包里ex...

  • Vue(一、插件开发)

    一、插件开发 开发插件 Vue.js 的插件应当有一个公开方法 install 。这个方法的第一个参数是 Vue ...

  • Vuex 原理

    1、Vue.use(Vuex):将Vuex 应用到Vue中 use是一个自定义插件,这个插件里有一个固定方法ins...

  • Vue3.x

    一. 项目搭建及自定义脚手架模板 二. 添加插件 vue add eg:vue add vuetify 2.x版...

  • vue全局API和使用方法

    Vue.use(plugins) 注册一个插件 例子: Vue.directive()创建或者获取自定义指令 一个...

  • VSCODE配置vue模板

    VSCode -vue项目,自定义.vue模版 1.安装vscode vscode官网地址: 2.安装一个插件,识...

  • Capacitor实现WebView中访问的自定义Android

    前言 Capacitor 自定义插件 实现WebView中访问的自定义Android代码 最近在工作中使用Vue+...

  • Vue.use()在new Vue() 之前使用的原因浅析

    先看Vue.use做了什么 do: 检查插件是否已经注册,相同的插件只注册一次 将Vue构造函数作为第一个参数,作...

网友评论

      本文标题:第一个vue自定义插件

      本文链接:https://www.haomeiwen.com/subject/bakyyjtx.html