uni-app 组件注册

全局注册
uni-app 支持配置全局组件,需在 main.js 里进行全局注册,注册后就可在所有页面里使用该组件。
0.组件的创建
\components\pageHead.vue

<template>
	<view>
		<view class="">
			{{title}}
		</view>
		<view class="">
			{{detail}}
		</view>
	</view>
</template>

<script>
	export default {
		name: "pageHead",
		props: {
			title: '',
			detail: '',
		}
	}
</script>

1.main.js 里进行全局导入和注册

import Vue from 'vue'
import App from './App'

App.mpType = 'app'

// 全局注册
import pageHead from 'components/pageHead.vue'
Vue.component('pageHead', pageHead)

2.index.vue 里可直接使用组件

<template>
	<div>
		 <pageHead :title="title" :detail="detail"></pageHead>
	</div>
</template>

<script>
	 
	export default {
		onLoad() {
		 
		},
		data() {
			return {
				title: "hello",
				detail:"你好"
			}
		},
	}
</script>

局部注册
页面引入组件方式
1.传统vue规范: 在 index.vue 页面中,通过 import 方式引入组件 ,在 components 选项中定义你想要使用的组件。

<template>
	<div>
		<pageHead :title="title" :detail="detail"></pageHead>
	</div>
</template>

<script>
	import pageHead from '../../components/pageFooter.vue';

	export default {
		components: {
			pageHead
		},
		onLoad() {

		},
		data() {
			return {
				title: "hello",
				detail: "你好"
			}
		},
	}
</script>

总结步骤2步,先import 引入后components注册

2.通过uni-app的easycom: 将组件引入精简为一步。只要组件安装在项目的 components 目录下,并符合 components/组件名称/组件名称.vue 目录结构。就可以不用引用、注册,直接在页面中使用。

举例:主要是需要符合路径规范---------最方便
\components\pageHead\pageHead.vue

<template>
	<div>
		<pageHead :title="title" :detail="detail"></pageHead>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				title: "hello",
				detail: "你好"
			}
		},
	}
</script>

easycom是自动开启的,不需要手动开启,最方便
不管components目录下安装了多少组件,easycom打包后会自动剔除没有使用的组件,对组件库的使用尤为友好。
原文出处

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光明有我16620122910

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值