vue3 + ts 组件全局挂载

vue3 + ts 组件全局挂载

1. 生成组件

<template>
  <button class="m-button" :class="type">
    <slot></slot>
  </button>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  name: 'mButton',
  components: {},
  props: {
    // 按钮类型
    type: {
      type: String,
      default: 'default'
    },
    // 是否朴素
    plain: {
      type: Boolean,
      default: false
    },
    // 是否边框 圆
    round: {
      type: Boolean,
      default: false
    },
    // 是否圆形
    circle: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    return {}
  }
})
</script>

<style scope lang="scss">
.default {
  color: $--button-default-font-color;
  background-color: $--button-default-background-color;
  border-color: $--button-default-border-color;
  font-size: $--button-font-size;
  font-weight: $--button-font-weight;
  border-radius: $--button-border-radius;
  padding: $--button-padding-vertical $--button-padding-horizontal;
}
</style>

2. install

新建一个index.ts文件,引入上面的组件,并且install

import mButton from './src/button.vue'
import { App } from 'vue'
const components = {
  install: (Vue: App): void => {
    Vue.component(mButton.name, mButton)
  }
}

3. 导出

在 index.ts 中将定义的 components 导出

export default components

4. createApp.use

main.ts 中引入上面的index.ts文件,并且createApp.use在vue中使用

import { createApp } from 'vue'
import App from './App.vue'
import mButton from '@/components/button/index'

const app = createApp(App)
app.use(mButton)
app.mount('#app')

Tips:
组件的命名是在index.ts 中的install中的mButton.name 中定义。也就是在 button.vue文件中的name属性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值