zoukankan      html  css  js  c++  java
  • vue 3 Teleport

    我们在做vue2项目的时候,往往弹窗设置都挺负责的需要各种z-indx,而vue3推荐的做法是使用Teleport

    <!--    index.html--> 
     <body>
        <div id="app"></div>
        <div id="teleport-target"></div>
        <script type="module" src="/src/main.ts"></script>
      </body>

    在组件内

    <template>
      <p>我是设置页面a</p>
      <button @click="showToast" class="btn">打开 toast</button>
      <!-- to 属性就是目标位置 -->
      <teleport to="#teleport-target">
        <div v-if="visible" class="toast-wrap">
          <div class="toast-msg">我是一个 Toast 文案</div>
        </div>
      </teleport>
    </template>
    
    <script setup lang="ts">
      import { getCurrentInstance, inject, watch, ref } from 'vue'; // 子组件
      const instance = getCurrentInstance()
      const _this = instance.appContext.config.globalProperties // 获取全局对象\
      const name = inject('name')
      watch(name, (newValue, oldValue) => {
        console.log(name.value)
      })
      // toast 的封装
      const visible = ref(false);
      let timer;
      const showToast = () => {
        visible.value = true;
        // clearTimeout(timer);
        // timer = setTimeout(() => {
        //   visible.value = false;
        // }, 2000);
      }
    </script>
    
    <style>
    
    </style>

    然后引入这个组件,设置一下样式就好了,父组件传递的props也可以使用

    一辈子说长不长,说短不短,努力做好两件事:第一件事爱生活,爱身边的人,爱自己;第二件事是好好学习,好好工作,实现自己的人生价值观,而不仅仅是为了赚钱
  • 相关阅读:
    python简介
    计算机基础
    C# 验证数字
    在字符串指定的索引下添加字符,输出换行
    js中实现子页面向父页面中赋值
    js搜索相同类型的控件全选、取值(Checkbox)
    Nhibernate中多Or条件的查询,很多Or的查询
    js遍历checkbox获取数据
    Jquery获取web窗体关闭事件,排除刷新页面
    两年多的工作感悟
  • 原文地址:https://www.cnblogs.com/dcyd/p/15732537.html
Copyright © 2011-2022 走看看