vue3:setup语法糖(1)

一、父组件向子组件传值

<template>
  <div class="hello">
  我是父组件
  <!-- 父组件通过:变量(这里是info)绑定值 -->
   <Child :info="parentMsg"></Child>
  </div>
</template>
 
<script setup>
import Child from './Child'
import {ref} from 'vue'
const parentMsg=ref('父组件传递值是a')
 
</script>
 
<style scoped>
 
</style>

2、子组件接收方式和使用

<template>
<!-- info是父组件传递过了的值 -->
  <div>我是子组件拿到了父组件的值是{{info}}</div>
</template>
 
<script setup>
import { toRefs, defineProps } from 'vue'
const props = defineProps({
  //子组件接收父组件传递过来的值
  info: String,
})
//使用父组件传递过来的值
const {info} =toRefs(props)
 
</script>

 

二、子组件向父组件传值

<Test-Com @clickChild="clickEven"></Test-Com>

const clickEven = (val: string) => {

console.log(val);

}

<template>

<button @click="clickChild">点击子组件</button>

</template>

<script lang="ts" setup>

import { defineEmits } from 'vue'

const emit = defineEmits(['clickChild'])

const clickChild = () => {

emit('clickChild', '傻逼龙成')

}

</script>

三、父组件获取子组件中的属性值

当时用语法糖时,需要将组建的属性及方法通过defineExpose导出,父组件才能访问到数据,否则拿不到子组件的数据

<Test-Com ref="testcomRef"></Test-Com>

<button @click="getSonHander">获取子组件中的数据</button>

<script lang="ts" setup>

import TestCom from '../components/TestCom.vue'

import { ref } from 'vue'

const testcomRef = ref()

const getSonHander = () => {

console.log('获取子组件中的性别', testcomRef.value.change());

}

</script>

<template>

<h2> 我是子组件</h2>

<p>性别:{{ sex }}</p>

<button @click="change">change</button>

</template>

<script lang="ts" setup>

import { reactive, ref, defineExpose } from "vue";

let sex = ref('男')

let info = reactive({

like: '王者荣耀',

age: 18

})

const change=()=>{

return 'lcsb'

}

defineExpose({ sex, info ,change})


 

</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3中,可以使用`<script setup>`语法糖来编写组件的逻辑部分。使用这个语法糖可以简化组件的编写,并且不再需要使用`return`来导出变量,而是在`setup`函数中直接声明变量。这样,在模板中就可以直接使用这些变量了。 要使用`<script setup>`语法糖,只需要在`<script>`标签中添加`setup`属性就可以了。例如: ```html <script setup> // 在这里编写组件的逻辑部分 // 可以声明变量、导入其他模块等 </script> ``` 通过使用`<script setup>`语法糖,我们可以更简洁地编写Vue 3组件的逻辑部分,并且让代码更加清晰易读。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [第九章、Vue3中<script setup语法糖](https://blog.csdn.net/weixin_42321326/article/details/128901124)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue3 setup写法(语法糖版本)](https://blog.csdn.net/weixin_44064067/article/details/126312646)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值