熬夜总结vue3中setUp函数的2个参数详解

简介: 熬夜总结vue3中setUp函数的2个参数详解

1.setUp函数的第1个参数props


setup(props,context){}


第一个参数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>


<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){
    console.log('props==>',props.mytitle);//输出的值是 undefined
    function sonHander(){
        context.emit('sonclick','子组件传递给父组件')
    }
    return {sonHander}
  }
});
</script>


1425695-20210629223237837-940608319.png


为什么通过props.mytitle输出的值是undefined呢?


因为我们没有使用props进行接收配置。即


props:{
    mytitle:{
        type:Object
    }
},

如果我们添加上接受配置


1425695-20210629223248056-928475558.png


2.参数context的讲解


第2个参数:context,是一个对象。


里面有attrs(获取当前标签上的所有属性的对象)


但是该属性是props中没有声明接收的所有的对象。


如果你使用props去获取值,同时props中你声明了你要获取的值


则:获取的值是undefined


注意点:


attrs获取值是不需要props中没有声明接收。


第1个参数props获取值是需要props中声明接收的


有emit事件分发,(传递给父组件需要使用该事件)


有slots插槽


<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>

1425695-20210629223258098-1810112332.png


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>

1425695-20210629223307004-794772859.gif


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>

1425695-20210629223316919-759308503.png


相关文章
|
6天前
|
JavaScript 前端开发 API
什么是响应式❓Vue2/Vue3中响应式的原理
什么是响应式❓Vue2/Vue3中响应式的原理
18 2
|
6天前
|
资源调度 JavaScript 前端开发
vite+vue3+ts从0到1搭建企业级项目(2)
vite+vue3+ts从0到1搭建企业级项目
20 1
|
3天前
|
JavaScript 前端开发
Vue躬行记(7)——渲染函数和JSX
Vue躬行记(7)——渲染函数和JSX
8 0
|
6天前
|
开发工具 git
vite+vue3+ts从0到1搭建企业级项目(4)
vite+vue3+ts从0到1搭建企业级项目
23 0
|
6天前
|
存储 JavaScript API
vite+vue3+ts从0到1搭建企业级项目(3)
vite+vue3+ts从0到1搭建企业级项目
19 0
|
6天前
|
资源调度 JavaScript 测试技术
vite+vue3+ts从0到1搭建企业级项目(1)
vite+vue3+ts从0到1搭建企业级项目
36 0
|
6天前
|
JavaScript 前端开发 编译器
跨越时代的框架对决:深度剖析Vue 2与Vue 3核心差异
跨越时代的框架对决:深度剖析Vue 2与Vue 3核心差异
15 0
|
4天前
|
前端开发 JavaScript
Vue底层实现原理总结
Vue底层实现原理总结
8 0
|
8天前
|
JavaScript 前端开发 测试技术
使用 Vue CLI 脚手架生成 Vue 项目
通过 Vue CLI 创建 Vue 项目可以极大地提高开发效率。它不仅提供了一整套标准化的项目结构,还集成了常用的开发工具和配置,使得开发者可以专注于业务逻辑的实现,而不需要花费大量时间在项目配置上。
67 7
使用 Vue CLI 脚手架生成 Vue 项目
|
6天前
|
JavaScript