Vue 2.0 入门系列(6)组件实例之消息框

news/2024/7/6 0:04:15

本节要实现一个消息框组件。效果:

clipboard.png

Bulma 消息框

我们使用的是 Bulma 的 消息框。Bulma 的消息框的基本界面如下:

<article class="message">
  <div class="message-header">
    <p>标题</p>
    <button class="delete"></button>
  </div>
  <div class="message-body">
    消息内容
  </div>

纯静态页面,删除功能需要我们去实现。

使用 slot

首先,我们考虑用之前学到过的 slot 来实现:

Vue.component('message',{
    template: `
        <article class="message" v-show="isVisible">
          <div class="message-header">
            <slot name="title">默认标题</slot>
            <button class="delete" @click="hideMessage"></button>
          </div>
          <div class="message-body">
            <slot></slot>
          </div>
        </article>
    `,
    data() {
        return {
            isVisible:true
        }
    },
    methods:{
        hideMessage() {
            this.isVisible = false
        }
    }
});


var vm = new Vue({
    el:"#root"
});

因为我们使用了多个 slot,为了能否区分,可以将标题的 slot 取名。这样,在父实例中就可以这样使用:

<div id="root">
    <message>
        <template slot="title">欢迎</template>
        你好,Vue
    </message>
</div>

使用 template 的好处是页面渲染时会保持子组件的样子。当然,也可以这样:

<message>
    <h1 slot="title">欢迎</h1>
    你好,Vue
</message>

那么就会被渲染成:

<h1>欢迎</h1>

使用 pros

我们在来看看另外一种实现方式:

<message title="欢迎" body="你好,Vue"></message>

应当如何实现呢?模板似乎可以这样写:

template: `
    <article class="message" v-show="isVisible">
      <div class="message-header">
        {{ title }}
        <button class="delete" @click="hideMessage"></button>
      </div>
      <div class="message-body">
        {{ body }}
      </div>
    </article>
`,

这种情景属于「组件的内部通信」,即 message 实例(parent)的数据要传递给 message 组件(child),这需要在组件的 props 属性中进行定义 :

Vue.component('message',{
    props: ['title','body'],
    template: `
        <article class="message" v-show="isVisible">
          <div class="message-header">
            {{ title }}
            <button class="delete" @click="hideMessage"></button>
          </div>
          <div class="message-body">
            {{ body }}
          </div>
        </article>
    `,
    data() {
        return {
            isVisible:true
        }
    },
    methods:{
        hideMessage() {
            this.isVisible = false
        }
    }
});


var vm = new Vue({
    el:"#root"
});

在组件的 props 属性中定义好之后,实例就可以直接通过属性的方式传递数据给组件了。


附录:

  • 源码

  • Bulma: a modern CSS framework based on Flexbox


http://www.niftyadmin.cn/n/1205924.html

相关文章

UiPath if条件

自动化流程中&#xff0c;我们经常会碰到根据不同的入参来实现不同的输入结果&#xff0c;比如常用的IF判断&#xff0c;当条件为TRUE时&#xff0c;设定性别为“男”&#xff0c;否则为“女”&#xff0c;那么如何来实现IF判断呢&#xff1f;下面我就分享一下通过UiPath实现IF…

HDU 1710 Binary Tree Traversals(二叉树)

题目地址&#xff1a;HDU 1710 已知二叉树先序和中序求后序。#include <stdio.h> #include <string.h> int a[1001], cnt; typedef struct node {int date ;node *lchild , *rchild ; }*tree; int getk(int ch,int ino[],int is,int n) {for(int i is ; i < is…

UiPath if布尔

If介绍 位置&#xff1a;System.Activities.Statements.If 计算使用条件生成器添加的条件&#xff0c;并在满足条件时执行在Then分支中指定的一个或多个活动。或者&#xff0c;当不满足条件时&#xff0c;执行Else分支中指定的另一组活动。 Properties&#xff08;属性&#…

C#知识点总结系列

C#知识点总结系列&#xff1a;2、C#中IDisposable和IEnumerable、IEnumerator C#知识点总结系列&#xff1a;1、C#中Hashtable、Dictionary详解以及写入和读取对比

UiPath if assign

If介绍 位置&#xff1a;System.Activities.Statements.If 计算使用条件生成器添加的条件&#xff0c;并在满足条件时执行在Then分支中指定的一个或多个活动。或者&#xff0c;当不满足条件时&#xff0c;执行Else分支中指定的另一组活动。 Properties&#xff08;属性&…

UiPath if多个

If介绍 位置&#xff1a;System.Activities.Statements.If 计算使用条件生成器添加的条件&#xff0c;并在满足条件时执行在Then分支中指定的一个或多个活动。或者&#xff0c;当不满足条件时&#xff0c;执行Else分支中指定的另一组活动。 Properties&#xff08;属性&…

POJ 3463 Sightseeing

最短路次短路&#xff08;Dijkstrapriority_queue&#xff09; 题意是要求你找出最短路的条数与最短路仅仅差1的次短路的条数。 開始仅仅会算最短路的条数&#xff0c;和次短路的长度。真是给次短路条数跪了。ORZ。其它人都用Dijkstra。我想试试SPFA。然后大神说要把这个最短&a…

UiPath if condition

If Condition介绍 位置&#xff1a;System.Activities.Statements.If 计算使用条件生成器添加的条件&#xff0c;并在满足条件时执行在Then分支中指定的一个或多个活动。或者&#xff0c;当不满足条件时&#xff0c;执行Else分支中指定的另一组活动。 Properties&#xff08;…