为了账号安全,请及时绑定邮箱和手机立即绑定

【金秋打卡】第9天 Vue3 + Typescript 从0到1开发通用基础组件(3-14~3-16)

标签:
活动

课程名称Vue3 + Typescript 从0到1开发通用基础组件

课程讲师:张轩

课程内容:

今天课程学习的主要知识点内容包括:
1、vue3 中的 defineComponent 函数;
2、vue3 中的 teleport 组件;

课程收获:

通过学习老师的视频课程和同学的公开笔记,主要收获如下:
一、vue3 中的 defineComponent 函数
defineComponent完全是为了服务ts所存在的,使用defineComponent进行组件定义。

<script lang="ts">
import { defineComponent } from 'vue'
const component = 
 defineComponent({
  name: 'HelloWorld',
  props: {
    msg: {
      required: true,
      type: String
    }
  }, 
  setup(props, context) {
    console.log(props)
    console.log(context)
  }
})
export default component;
</script>

二、vue3 中的 teleport 组件
1、解决的问题:
(1)vue开发中常常遇见组件嵌套很深的情况,可能会导致出现某些错误;
(2)dialog被包裹在其他组件之中,容易被干扰;
(3)样式也在其他组件中,容易变的混乱;

2、Teleport使用:
Teleport 传送 to="#modal" 渲染到modal 节点上;

3、案例举例

<template>
<teleport to="#modal">
  <div id="center" v-if="isOpen">
    <h2><slot>this is a modal</slot></h2>
    <button @click="buttonClick">Close</button>
  </div>
</teleport>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
  props: {
    isOpen: Boolean,
  },
  emits: {
    'close-modal': null
  },
  setup(props, context) {
    const buttonClick = () => {
      context.emit('close-modal')
    }
    return {
      buttonClick
    }
  }
})
</script>

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消