674 字
3 分钟
setup语法糖的使用
2022-02-26
无标签

前言#

在setup()中手动暴露状态和方法会很冗长。幸运的是,只有在不使用构建步骤时不是必要的.当使用单文件中,我们可以通过<script setup>大大简化使用

参考链接

基本使用#

<script setup>
// imported components are also directly usable in template
import Foo from './Foo.vue'
import { ref } from 'vue'
// write Composition API code just like in a normal setup()
// but no need to manually return everything
const count = ref(0)
const inc = () => {
count.value++
}
</script>
<template>
<Foo :count="count" @click="inc" />
</template>

编译结果

import Foo from './Foo.vue'
import { ref } from 'vue'
export default {
setup() {
const count = ref(1)
const inc = () => {
count.value++
}
return function render() {
return h(Foo, {
count,
onClick: inc
})
}
}
}

定义 props 和 emit#

<script setup>
// expects props options
const props = defineProps({
foo: String
})
console.log(props.foo)
// expects emits options
const emit = defineEmits(['update', 'delete'])
emit('update')
</script>

编译结果

export default {
props: {
foo: String
},
emits: ['change', 'delete'],
setup(props, { emit }) {
// setup code
}
}

导入组件#

<script setup> 内导入的组件,可以直接在模版中使用。

<script setup>
import Foo from './Foo.vue'
import MyComponent from './MyComponent.vue'
</script>
<template>
<Foo />
<my-component />
</template>

编译结果

import Foo from './Foo.vue'
import MyComponent from './MyComponent.vue'
export default {
setup(){
return function render(){
return [h(Foo),h(MyComponent)]
}
}
}

使用指令#

指令以类似的方式工作—除了名为 v-my-dir 的指令名 映射到 setup作用域中 名为 vMyDir 的变量

<script setup>
import { directive as vClickOutside } from 'v-click-outside'
</script>
<template>
<div v-click-outside />
</template>

编译结果

import { directive as vClickOutside } from 'v-click-outside'
export default {
setup() {
return function render() {
return withDirectives(h('div'), [[vClickOutside]])
}
}
}

使用 slots 和 attrs#

<script setup>
import { useSlots, useAttrs } from 'vue'
const slots = useSlots()
const attrs = useAttrs()
</script>

暴露组件公用接口#

<script setup>
const a = 1
const b = ref(2)
defineExpose({
a,
b
})
</script>

与普通的script标签一起使用#

有些情况下,代码必须在模块范围内执行,例如:

  • 申报命名出口
  • 应该只执行一次的全局副作用。

在这种情况下,正常的<script>块可以与<script setup>一起使用

<script>
performGlobalSideEffect()
// this can be imported as `import { named } from './*.vue'`
export const named = 1
</script>
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>

编译结果

import {ref} from 'vue'
performGlobalSideEffect()
export const named = 1
export default {
setup(){
const count = ref(0)
return { count }
}
}

声明其他选项#

<script setup>语法提供了表达大多数现有选项API选项的等效功能的能力,但以下几个选项除外:

  • name
  • inheritAttrs
  • 插件或库所需的自定义选项

如果需要声明这些选项,请使用带有导出默认值的单独<script>

<script>
export default {
name: 'CustomName',
inheritAttrs: false,
customOptions: {}
}
</script>
<script setup>
// script setup logic
</script>

provide & inject#

<script setup>
import { ref, provide } from 'vue'
const name = ref('xiaoyu')
provide('name',name)
</script>
<!-- 子组件 -->
<script setup>
import { inject } from 'vue'
const name:string = inject('name')
</script>
setup语法糖的使用
https://blog.cxyxiaoyu.top/posts/setup语法糖的使用/
作者
小宇
发布于
2022-02-26
许可协议
CC BY-NC-SA 4.0