vue3 setup语法糖

相对于以前的语法,使用语法糖之后语法变得相当简单和简短

<script setup>
</script>

注:vue3.2以上的版本才可使用,或者说比较合适

demo

无需setup函数,并且会自动return定义的变量等
<template>
	{{msg}}
</template>

<script setup>
	let msg = 'aaa'
</script>
注册组件
<template>
	<login></login>
</template>

<script setup>
	import login from 'login.vue'
</script>

传值

父 → 子
// 父
<template>
	<subject-card :title="msg"></subject-card>
</template>

<script setup>
	import subjectCard from '../components/subjectCard.vue'
	const msg = 'aaa'
</script>
// 子
// subjectCard.vue
<template>
	<div class="test">
		{{title}}
	</div>
</template>
<script setup>
	const props = defineProps({
		title:{
			type: String
		}
	})

子 → 父

// 子
// subjectCard.vue
<script setup>
	const emit = defineEmits(['clickThis'])
	const clickThis = () => {
		emit('clickThis', '这是子组件向父组件传的值')
	}
</script>
// 父
<template>
	<subject-card @clickThis="clickThis"></subject-card>
</template>

<script setup>
	import subjectCard from '../components/subjectCard.vue'
	const clickThis = e => {
		console.log(e + '1')
	}
</script>

对外暴露属性(defineExpose)

// 子
// subjectCard.vue
<script setup>
	const color = 'pink'
	defineExpose({
		color
	})
</script>
// 父
<template>
	<subject-card ref="color"></subject-card>
</template>

<script setup>
	import { ref } from "vue"
	import subjectCard from '../components/subjectCard.vue'
	let color = ref(null)
	onMounted(() => {
		console.log(color.value.color)
		})
</script>

CSS使用变量

<script setup>
	const color = 'pink'
</script>

<style lang="less" scoped>
	background-color: v-bind(color);
</style>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值