vue2升级vue3:单文件组件概述 及 defineExpos/expose
像我这种react门徒被迫迁移到vue的,用管了TSX,地vue 单文件组件也不太感冒,但是vue3 单文件组件,造了蛮多api ,还不得去了解下
https://v3.cn.vuejs.org/api/sfc-script-setup.html#单文件组件-script-setup
defineProps、defineEmits没有什么好说的,就是setup中定义 props 与 emits。
useSlots、useAttrs 它会返回与 setupContext.slots 和 setupContext.attrs
1 2 3 4 5 6 7 8 9 10 11 | export default defineComponent({ name: 'RefreshInterval' , props: { //defineProps refreshFun: { type: Function, }, }, emits: [ 'change' ], // defineEmits setup(props, { slots,attrs }) { //useSlots、useAttrs } }) |
其中比较迷惑的地方就是 defineexpose:
defineExpose
首先看官方文档:
https://v3.cn.vuejs.org/api/sfc-script-setup.html#defineexpose
使用 <script setup> 的组件是默认关闭的,也即通过模板 ref 或者 $parent 链获取到的组件的公开实例,不会暴露任何在 <script setup> 中声明的绑定。
为了在 <script setup> 组件中明确要暴露出去的属性,使用 defineExpose 编译器宏:
1234567891011<script setup>
import
{ ref } from
'vue'
const a = 1
const b = ref(2)
defineExpose({
a,
b
})
</script>
当父组件通过模板 ref 的方式获取到当前组件的实例,获取到的实例会像这样 { a: number, b: number } (ref 会和在普通实例中一样被自动解包)
翻译成大白话就是:子组件是<script setup>声明时,父组件(非<script setup>)是不能直接访问子组件的方法,需要子组件手动的抛出才行。即:refChildren.value.children.props 是无效的。
expose
官方文档:https://staging-cn.vuejs.org/api/options-state.html#expose
默认情况下,当通过 root 或模板 refs 访问时,组件实例将向父组件暴露所有的实例 property。这可能不是我们希望看到的,因为组件很可能拥有一些应保持私有的内部状态或方法,以避免紧耦合。
expose 选项期望一个 property 名称字符串的列表。当使用 expose 时,只有显式列出的 property 将在组件实例上暴露。
expose 仅影响用户定义的 property——它不会过滤掉内置的组件实例 property。
123456789101112export
default
{
// 只有 `publicMethod` 在公共实例上可用
expose: [
'publicMethod'
],
methods: {
publicMethod() {
// ...
},
privateMethod() {
// ...
}
}
}
使用expose函数来控制组件被ref时向外暴露的对象内容,借此来维护组件的封装性。
其实把它理解为 React函数组件 中的 useImperativeHandle 就行!
子组件利用useImperativeHandle可以让父组件输出任意数据。
12345678910111213141516171819202122232425262728// FancyInput组件作为子组件
const FancyInput = React.forwardRef(
function
FancyInput(props, ref) {
const inputRef = useRef();
// 命令式的给`ref.current`赋值个对象
useImperativeHandle(ref, () => ({
focus: () => {
inputRef.current.focus()
}
}));
return
<input ref={inputRef} ... />
})
// Example组件作为父组件
function
Example() {
const fancyInputRef = useRef()
const focus = () => {
fancyInputRef.current.focus()
}
return
(
<>
<FancyInput ref={fancyInputRef} />
</>
)
}
更多的,可以查看:Vue3 源码解析(九):setup 揭秘与 expose 的妙用 https://segmentfault.com/a/1190000040179961
参考文章:
vue3-什么是expose,是如何使用的,以及defineExpose的用法 https://blog.csdn.net/vet_then_what/article/details/125515694
Vue3中的expose函数 https://juejin.cn/post/6943950109268770830
最陌生的hooks: useImperativeHandle https://segmentfault.com/a/1190000040758640
useRef、useImperativeHandle https://www.jianshu.com/p/20aa551e44e7
转载本站文章《vue2升级vue3:单文件组件概述 及 defineExpos/expose》,
请注明出处:https://www.zhoulujun.cn/html/webfront/ECMAScript/vue3/8872.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-07-25 web自动化测试(2):选择selenium优势?与PhantomJS/QTP/Monkey对比
2021-07-25 web自动化测试(1):再谈UI发展史与UI、功能自动化测试