zoukankan      html  css  js  c++  java
  • Alert提示框的vue组件编写

    最近一直学习vue,跟着视频敲代码,敲了两三个组件后,终于对编写组件有一个大致的思路了,以下通过编写一个alert提示框组件大致梳理一下我编写组件的思路。
    主要分为三个部分:组件引入(注册路由器)、定义组件内容样式及实现组件间通信。
    (注册路由器使用路由组件也可放到最后一步,个人习惯先引入然后定义组件及样式便于观察组件显示效果)

    下面是本组件引入后显示效果,通过登匹配验证为false触发alert提示框显示,点击确认关闭提示框:

    组件引入
    一种是写入路由,要先在在router.js中配置路由:
    routes: [{path:'', component:''}]]
    通过路由组件标签 <router-link to="/" /> 注册路由器;
     
    另一种是通过import方式在父组件中引入,通过路由组件标签 <router-view /> 注册路由器,本文通过第二种方式注册路由器:
    1 import AlertTip from '@/components/AlertTip'
    2 
    3 export default {
    4   components: {
    5     AlertTip // 忘记会报错:'AlertTip' is defined but never used 
    6   }
    7 }

    定义组件内容及样式

    本文演示的alert提示框组件的定义内容如下:
     1 <div id="dialog" class="dialog_container">
     2 <!--提示框容器-->
     3     <section class="alert_container">
     4       <!--提示内容容器-->
     5       <div class="alert_content">
     6         <div class="tip_icon">
     7           <i class="el-icon-warning"></i>
     8           <span>提示信息</span>
     9         </div>
    10         <!--提示信息,接收父组件传来的参数-->
    11         <p class="tip_text">{{alertText}}</p>
    12       </div>
    13       <!--确认事件传递回父组件-->
    14       <button class="confirm" @click="closeTip">确认</button>
    15     </section>
    16   </div>
    提示框主要样式——实现提示框位置及显示,一般采用的样式书写方式如下:
    1 .dialog_container {
    2   // 提示框位置及显示一般样式
    3   position: fixed;
    4   z-index: 9999; // 最上层
    5   left:50%;
    6   top:50%;
    7 }
    提示框内提示文本及按钮样式根据类标签选择器自行编写。
    组件间通信
    引入组件并定义好组件内容样式之后,要考虑的就是组件间通过参数传递进行通信。主要是父组件向子组件传递提示文本,子组件向父组件传递按钮点击事件状态。本文所演示提示框组件主要是通过自定义事件进行父子组件间通信。
    1 props: {
    2     alertText: String // 初始化显示声明类型
    3   },
    4   methods: {
    5     closeTip () {
    6       // 分发自定义事件(事件名:closeTip)
    7       this.$emit('closeTip')
    8     }
    9   }
    在父组件中引入子组件时添加需要传递的参数及事件:
    <AlertTip :alertText="alertText" v-show="alertShow" @closeTip="closeTip"/>
    在父组件中通过调用showAlert()方法传入alertText参数值:
    1 showAlert (alertText) {
    2   this.alertShow = true, // 是否显示提示框组件
    3   this.alertText = alertText
    4 }
    至此,将代码整合到一起,就得到一个可以动态显示的提示框组件了。
    目前还是前端小白一枚,所述不足之处还希望大家多多指正呀!
  • 相关阅读:
    Codeforces 845E Fire in the City 线段树
    Codeforces 542D Superhero's Job dp (看题解)
    Codeforces 797F Mice and Holes dp
    Codeforces 408D Parcels dp (看题解)
    Codeforces 464D World of Darkraft
    Codeforces 215E Periodical Numbers 容斥原理
    Codeforces 285E Positions in Permutations dp + 容斥原理
    Codeforces 875E Delivery Club dp
    Codeforces 888F Connecting Vertices 区间dp (看题解)
    Codeforces 946F Fibonacci String Subsequences dp (看题解)
  • 原文地址:https://www.cnblogs.com/lynn-z/p/12870385.html
Copyright © 2011-2022 走看看