语法糖: 语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会
1, 在语法糖中使用组件传值
import { defineProps, defineEmits, ref } from "vue"
// 使用defineProps自定义属性,接收父组件传值, 新版本无需导入,直接用
const props = defineProps(['msg'])
console.log(props, props.msg)
const info = ref("")
// 使用defineEmits自定义事件, 向父组件传值
const emit = defineEmits(["getinfo"])
function change(){
emit("getinfo", info.value)
}
2, 在语法糖中使用路由和状态管理
// 从路由模块中导入路由组合式API函数, 调用函数即可获取路由数据
import { useRoute, useRouter } from "vue-router"
const route = useRoute()
const router = useRouter()
console.log(route, router)
// 从vuex模板中导入状态仓库组合式API(hook)函数, 调用即可获取状态仓库