setup语法糖

setup语法糖

<script setup>
	//这里的代码会被编译成组件setup()函数中的内容,这里的script跟普通的script只有组件首次引入的时候执行一次不同,<script setup>中的代码会在每次组件实例被创建的时候执行
</script>

基本使用

<script setup>
    //这里跟setup函数一样的写法,只是不需要return,组件不需要挂在compones上,可以直接在模板上使用
	import { reactive, ref } from '@vue/reactivity'
    import { computed, watch, watchEffect } from '@vue/runtime-core'
    import { getCurrentInstance } from 'vue'
    const { proxy } = getCurrentInstance()
    import HelloWorld from './components/HelloWorld.vue'
    import headerModule from './components/js/headerModule'
	const data = ({
        
    })
</script>
<template>
  <HelloWorld msg="Hello Vue 3 + Vite" />
</template>

defineProps,defineEmits,defineExpose它们都是<script setup>中才能使用的编译器宏,不需要引入直接可以使用,会随着 <script setup> 处理过程一同被编译掉

父传子

<template>
<!--父组件-->
  <HelloWorld msg="Hello" />
</template>


<script setup>
//子组件通过defineProps代替props
    const porps = definePorps({
        fol:{
        	type:String,
            default:'默认值',
            require:true//设置之后,props必须要接收到这个数据,默认为false
    	}
    })
</script>
<template>
  <HelloWorld msg="Hello Vue 3 + Vite" />
</template>

子传父

<script setup>
    //使用defineEmits代替$emit
    const emit = defineEmits(['change'])//这里最好写出来
	const change = val=>{
    	emit("change",val)
	}
</script>
<template>
	<!--<div @click="emit('biu',123)">子组件</div> 这样写前面可以加$用之前的写法也行-->
	<div @click="change(111)">子组件</div>
</template>

暴露组件

<script>
	//使用<script setup>的组件默认是关闭的,它的公开实例不会暴露在里面声明的绑定
    //使用defineExpose暴露数据
    const a = ref(1)
    const b = 2
    defineExpose({
        a,
        b
    })
</script>

父组件通过模板绑定ref,获取到这个对象
<script setup>
    //得先声明个变量,来保存它,或者放在onMounted里
    const hello = ref(null)
	const get = ()=>{
    	console.log(hello)
  	}
</script>
<template>
	<Header @click="get" ref="hello">子组件</Header>
</template>

自定义指令

<script setup>
	//全局自定义指令一样用,本地指令可以直接在模板上使用,但是必须以 vNameOfDirective 的形式来命名本地自定义指令
	const vMyDirective = {
		beforeMount: (el) => {
			console.log(el)
		}
	}
</script>
<template>
	<div v-my-directive></div>
</template>

ref获取dom节点

<script setup>
import { ref ,getCurrentInstance,nextTick} from 'vue'
import Header from './Header.vue'

const {proxy} = getCurrentInstance()
const rename = ref(null)//声明一个和ref一样的名字,直接获取它
nextTick(()=>{
  console.log(rename.value)
  //console.log(proxy.$refs.rename) 或者直接这样获取
    //要获取子组件的dom节点,通过绑定ref在子组件标签上,子组件获取自己的dom节点,通过defineExpose暴露给父组件
})
</script>

<template>
  <div ref="rename"></div>
  <Header/>
  <div></div>
</template>
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue的setUp语法糖是一种简化Vue组件代码的语法形式。它使用了单文件组件的方式,将模板、脚本和样式放在同一个文件中,使得组件更加易读和易维护。 在setUp语法糖中,使用<script setup>标签来定义组件的逻辑部分。在这个标签内,我们可以使用defineProps函数来声明props,并且具备完整的类型推断。比如可以这样声明props:const props = defineProps({ foo: String })。我们也可以使用withDefaults函数来为props设置默认值,比如const props = withDefaults(defineProps<Props>(), { msg: 'hello', labels: () => ['one', 'two'] })。 同时,在<script setup>标签中,我们可以直接使用emit函数来触发父组件的事件监听器,而不需要使用this.$emit。这样可以简化代码的书写。 在模板中,我们可以直接使用组件的标签名来引入自定义组件,而不需要在component对象里进行声明。比如<template> <MyComponent /> </template>,然后在<script setup>中import MyComponent from './MyComponent.vue'。 总的来说,Vue的setUp语法糖能够简化组件的编写和维护,使得代码更加清晰和易读。它提供了更加简洁的语法形式来定义props、触发事件等功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue3中setup语法糖学习](https://blog.csdn.net/pdd11997110103/article/details/121438090)[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: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值