Vue3.x的expose是什么?

本文通过实例解析Vue3.x中的expose选项,该选项用于指定父组件能访问到的子组件实例上的属性。在默认情况下,父组件可以直接访问子组件的所有属性,但使用expose可以限制这一行为,确保数据安全和组件封装。文中展示了如何在子组件中使用expose暴露特定方法,以便父组件在需要时调用,例如改变子组件的count值。

1.Vue3.x的expose是什么?

  1. 一个将暴露在公共组件实例上的 property 列表
  2. 默认情况下,通过 refs、refs、refsparent 或 $root 访问到的公共实例与模板使用的组件内部实例是一样的。expose 选项将限制公共实例可以访问的 property。

好上面是vue官方的解释,现在我举个例子。

父组件

<template>
	<div>我是一个父组件</div>
	<!--HelloWorldVue 是一个子组件或者一个公共组件-->
	<HelloWorldVue ref="dmo" />
	<button @click>更改子组件的值</button>
</template>
<script>
import HelloWorldVue from './components/HelloWorld.vue';
export default {
	components:{
		HelloWorldVue
	},
	setup(){
		const dmo = ref(null);
		const change = () => {
			demo.value.changeCount();
		}
		return {
			demo,
			change
		}
	}
}
</script>

子组件 / 公共组件中的代码

<template>
	<div>在子组件中显示的数据---{{count}}</div>
</template>
<script>
	export default {
		setup(props, { expose }){
			const count = ref(10);
			const changeCount = () => {
					count.value = 5555;
			}
			return {
				count,
			}
		}
	}
</script>

当点击button按钮,就可以改变子组件的count值

:8081/#/user:1 Access to XMLHttpRequest at &#39;http://localhost:8080/user/list&#39; from origin &#39;http://localhost:8081&#39; has been blocked by CORS policy: Response to preflight request doesn&#39;t pass access control check: No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource. xhr.js:175 POST http://localhost:8080/user/list net::ERR_FAILED dispatchXhrRequest @ xhr.js:175 xhrAdapter @ xhr.js:12 dispatchRequest @ dispatchRequest.js:52 Promise.then request @ Axios.js:57 Axios.<computed> @ Axios.js:77 wrap @ bind.js:9 getTableData @ User.vue:141 mounted @ User.vue:225 invokeWithErrorHandling @ vue.esm.js:1872 callHook @ vue.esm.js:4244 insert @ vue.esm.js:3167 invokeWithErrorHandling @ vue.esm.js:1872 invoker @ vue.esm.js:2193 invokeInsertHook @ vue.esm.js:6401 patch @ vue.esm.js:6620 Vue._update @ vue.esm.js:3972 updateComponent @ vue.esm.js:4090 get @ vue.esm.js:4504 run @ vue.esm.js:4579 flushSchedulerQueue @ vue.esm.js:4335 eval @ vue.esm.js:1998 flushCallbacks @ vue.esm.js:1924 Promise.then timerFunc @ vue.esm.js:1951 nextTick @ vue.esm.js:2008 queueWatcher @ vue.esm.js:4427 update @ vue.esm.js:4569 notify @ vue.esm.js:739 reactiveSetter @ vue.esm.js:1064 proxySetter @ vue.esm.js:4653 handleItemClick @ element-ui.common.js:3358 invokeWithErrorHandling @ vue.esm.js:1872 Vue.$emit @ vue.esm.js:3912 dispatch @ emitter.js:29 handleClick @ element-ui.common.js:4090 invokeWithErrorHandling @ vue.esm.js:1872 invoker @ vue.esm.js:2197 original._wrapper @ vue.esm.js:7609 createError.js:15 Uncaught (in promise) Error: Network Error at createError (createError.js:15:1) at XMLHttpRequest.handleError (xhr.js:87:1)
05-14
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值