Vue:子组件使用的细节,子组件中的data,ref的使用,

本文探讨了Vue中子组件的正确使用方法,包括如何解决DOM结构错误,确保子组件如table row正确渲染,以及data属性为何必须为函数。同时介绍了ref属性的使用,帮助开发者更好地操作DOM和组件。

我们创建一个table

<div id="app">
	<table>
		<tbody>
			<row></row>
			<row></row>
			<row></row>
		</tbody>
	</table>
</div>

我们希望每一行的数据是子组件

<script>
	Vue.component('row', {
		template:'<tr><td>this is a row</td></tr>'
	})
	var app = new Vue({
		el:"#app",
	})
</script>

在这里插入图片描述
正常来说3个tr会在tbody里面,这里出错了,这是为什么呢
在H5的规范中,tbody下是tr,我们在使用子组件的时候,这里的tr写成了row,所以会出现问题,这个时候我们可以使用Vue提供的is属性来解决这个问题

<div id="app">
			<table>
				<tbody>
					<tr is="row"></tr>
					<tr is="row"></tr>
					<tr is="row"></tr>
				</tbody>
			</table>
		</div>

在这里插入图片描述
这样就没问题了
其他标签比如ul下的li,ol下的li,select下的option也可以使用is属性解决模板出现的bug问题

2.在子组件中,data必须是个函数,不能为对象

<div id="app">
			<table>
				<tbody>
					<tr is="row"></tr>
					<tr is="row"></tr>
					<tr is="row"></tr>
				</tbody>
			</table>
		</div>
		<script>
			Vue.component('row', {
				data: function() {
					return {
						content:'this is content'
					}
				},
				template:'<tr><td>{{content}}</td></tr>'
			})
			var app = new Vue({
				el:"#app",
			})
		</script>

之所以这样设计,是因为子组件不像根组件,只会被调用一次,我们不希望子组件之间的数据产生冲突,或者说,每个子组件都应该有自己的数据

3.ref的使用

<div id="app">
			<div ref='hello' @click="hdclick">
				hello box
			</div>
		</div>
		<script>
			var app = new Vue({
				el:"#app",
				methods:{
					hdclick: function(){
						console.log(this.$refs.hello.innerHTML);
					}
				}
			})
		</script>

在这里插入图片描述
使用ref获取div的dom节点
当ref为组件上的ref时,获取的为组件的引用

Vue 3 中,**父组件可以通过 `ref` 获取子组件的实例**,从而直接访问子组件的方法、属性,甚至监听子组件触发的某些行为。这在需要 **直接调用子组件方法或监听子组件状态变化** 时非常有用。 --- ### ✅ 示例:父组件使用 `ref` 调用子组件方法(Composition API + `<script setup>`) #### 子组件 `ChildComponent.vue` ```vue <template> <div> <h3>我是子组件</h3> </div> </template> <script setup> const sayHello = (name) => { console.log(`Hello from child, ${name}`); }; defineExpose({ sayHello }); // 暴露方法给父组件调用 </script> ``` > 使用 `defineExpose` 来暴露子组件中你希望父组件能访问的方法或属性。 --- #### 父组件 `ParentComponent.vue` ```vue <template> <div> <h2>父组件</h2> <ChildComponent ref="childRef" /> <button @click="callChildMethod">调用子组件方法</button> </div> </template> <script setup> import { ref } from &#39;vue&#39;; import ChildComponent from &#39;./ChildComponent.vue&#39;; const childRef = ref(); const callChildMethod = () => { if (childRef.value) { childRef.value.sayHello(&#39;Vue 3&#39;); } }; </script> ``` --- ### ✅ 示例:Options API 中使用 `ref` #### 子组件 `ChildComponent.vue` ```vue <template> <div> <h3>我是子组件</h3> </div> </template> <script> export default { methods: { sayHello(name) { console.log(`Hello from child, ${name}`); } } }; </script> ``` #### 父组件 `ParentComponent.vue` ```vue <template> <div> <h2>父组件</h2> <ChildComponent ref="childRef" /> <button @click="callChildMethod">调用子组件方法</button> </div> </template> <script> import ChildComponent from &#39;./ChildComponent.vue&#39;; export default { components: { ChildComponent }, data() { return { childRef: null }; }, methods: { callChildMethod() { this.childRef.sayHello(&#39;Vue 3&#39;); } } }; </script> ``` --- ### 🔍 解释: - **`ref`** 是 Vue 中获取 DOM 元素或组件实例的机制。 - 在 Vue 3 Composition API 中,你需要使用 `defineExpose` 显地暴露子组件的方法或属性,否则 `ref.value` 会是 `undefined`。 - 这种方式适用于需要 **直接访问子组件 API** 的场景,比如表单验证、组件内部状态切换、动画控制等。 --- ### ⚠️ 注意事项: - 不要滥用 `ref`,它会破坏组件的封装性,应优先使用事件通信(`emit`)或状态管理(如 Pinia)。 - `ref` 更适合用于操作 DOM 或需要直接调用子组件方法的场景。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值