vue 自定义组件以及自定义全局组件

一般常用的组件,比如输入框,按钮,modal,drawer等等需要经常用到,那么我们就可以将其封装成全组件;

而一般的项目中比如说一个需求,里面有很多重复的功能点需要拆分,这个可能只有当前项目会用到,所以没必要写成一个全局的,那么我们就需要局部注册

自定义组件(局部注册)

在自定义组件中写出你要写的功能或样式
在ExampleComponent.vue组件中:

<template>
	<div>我是一个自定义组件</div>
</template>
<style>
	// 这里写样式
</style>
<script lang="ts">
	export default {
	  name: 'example-component',
	  data(){
	    return {}
	  }
	}
</script>

在父组件中使用:

<template>
	<ExampleComponent></ExampleComponent>
</template>
<script>
	import ExampleComponent from "../component/ExampleComponent.vue";
	export default {
		name: "index",
		data() {
			return {};
		},
		components: {
			ExampleComponent //ExampleComponent: ExampleComponent的简写
		},
	};
</script>

自定义组件(全局注册)

我的自定义全局组件目录结构如下:
在这里插入图片描述
index.js是一个入口文件,用来导出所有的公用组件,代码如下:

import dialog from './dialog';
const views = {
  dialog,
}

const install = function (Vue) {
  Object.keys(views).forEach((key) => {
    Vue.component('po-'+key, views[key])
  })
}

const API = {
  install
}

export default API;

在main.js(也有可能是index.js)入口文件中引入components下的index.js:

import Components from './components/';
Vue.use(Components);

使用:

<po-dialog></po-dialog>

对你有用的话点个赞吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值