Vue3中setup方法的用法详解
作者:Kyle 时间:2023-07-02 16:56:19
1.参数props
props是一个对象,包含父组件传递给子组件的所有数据。在子组件中使用props进行接收。包含配置声明并传入的所有的属性的对象。
也就是说,如果你想通过props的方式输出父组件传递给子组件的值。你需要使用props进行接收配置。即props:{......}。如果你未通过Props进行接受配置,则输出的值是undefined
<template>
<div class="box">
父组件
</div>
<no-cont :mytitle="msg"
othertitle="别人的标题"
@sonclick="sonclick">
</no-cont>
</template>
<script lang="ts">
import NoCont from "../components/NoCont.vue"
export default {
setup () {
let msg={
title:'父组件给子给子组件的数据'
}
function sonclick(msss:string){
console.log(msss)
}
return {msg,sonclick}
},
components:{
NoCont
}
}
</script>
为什么通过props.mytitle输出的值是undefined呢?
因为我们没有使用props进行接收配置。即
props:{
mytitle:{
type:Object
}
}
2.参数context
第2个参数:context,是一个对象。里面有attrs(获取当前标签上的所有属性的对象)。但是该属性是props中没有声明接收的所有的对象。如果你使用props去获取值,同时props中你声明了你要获取的值,则:获取的值是undefined
注意点:
attrs获取值是不需要props中没有声明接收。第1个参数props获取值是需要props中声明接收的。有emit事件分发(传递给父组件需要使用该事件)
<template>
<div @click="sonHander">
我是子组件中的数据
</div>
</template>
<script lang="ts">
import { defineComponent,setup } from 'vue';
export default defineComponent({
name: 'NoCont',
props:{
mytitle:{
type:Object
}
},
setup(props,context){
//输出{title:父组件传递的值}
console.log('props==>',props.mytitle);
// 输出别人的标题【使用context获取值,不需要使用props去接受】
console.log('context==> ',context.attrs.othertitle);
// 输出undefined,因为context不需要使用props去接受。
console.log('contextmytitle==> ',context.attrs.mytitle);
function sonHander(){
context.emit('sonclick','子组件传递给父组件')
}
return {sonHander}
}
});
</script>
3. 子组件向父组件派发事件
<template>
<div @click="sonHander">
我是子组件中的数据
</div>
</template>
<script lang="ts">
import { defineComponent,setup } from 'vue';
export default defineComponent({
name: 'NoCont',
props:{
mytitle:{
type:Object
}
},
setup(props,context){
function sonHander(){
context.emit('sonclick','子组件传递给父组件')
}
return {sonHander}
}
});
</script>
4.优化事件派发
我们知道第2个参数context是一个对象,并且对象中有三个属性attrs,slots,emit,在事件派发的时候,直接使用emit就ok了
<template>
<div @click="sonHander">
我是子组件中的数据
</div>
</template>
<script lang="ts">
import { defineComponent,setup } from 'vue';
export default defineComponent({
name: 'NoCont',
props:{
mytitle:{
type:Object
}
},
setup(props,{attrs,slots,emit}){
//直接使用emit进行事件派发
function sonHander(){
emit('sonclick','子组件传递给父组件')
}
return {sonHander}
}
});
</script>
5.获取父组件传递的值
我们将使用props参数获取值,以及使用attrs获取值
<template>
<hr/>
<h2>子组件</h2>
<div @click="sonHander">
我是子组件中的数据
</div>
<h2>使用了props声明接收==>{{ mytitle }}</h2>
<h2>使用参数attrs获取==>{{ attrs.othertitle }}</h2>
</template>
<script lang="ts">
import { defineComponent,setup } from 'vue';
export default defineComponent({
name: 'NoCont',
props:{
mytitle:{
type:Object
}
},
setup(props,{attrs,slots,emit}){
function sonHander(){
emit('sonclick','子组件传递给父组件')
}
return {sonHander,attrs}
}
});
</script>
附使用setup函数时需要注意几点:
setup函数的执行时机是在beforeCreate和created之间
由于setup执行时机是在created之间,所以组件才刚刚被创建,而data和methods还没初始化好,所以无法在setup中使用data和methods
setup中this指向undefined
setup只能是同步的,不能是异步的
来源:https://segmentfault.com/a/1190000042234229


猜你喜欢
简单了解什么是神经网络

PHP对象克隆clone用法示例
Python 中的range(),以及列表切片方法
matplotlib运行时配置(Runtime Configuration,rc)参数rcParams解析
如何用idea数据库编写快递e站

matplotlib共享坐标轴的实现(X或Y坐标轴)

Python面向对象之继承原理与用法案例分析

MySQL binlog中的事件类型详解
基于Python实现简单的定时器详解

Python的Flask框架中使用Flask-Migrate扩展迁移数据库的教程
使用Tensorflow将自己的数据分割成batch训练实例

Python中ini配置文件读写的实现
sqlserver 千万数量级分页存储过程代码
使用Vue自定义指令实现Select组件

基于Python实现音乐播放器的实现示例代码
Python实现删除文件中含“指定内容”的行示例
python实现Decorator模式实例代码
Python Pycurl的属性与方法案例详解
Pycharm学习教程(3) 代码运行调试

Python实现将Excel转换为json的方法示例
