Vue JS在单击按钮时添加bulma类is-loading

时间:2017-11-23 15:02:21

标签: javascript vuejs2 bulma

我正在尝试使用bulma。我想要的是点击它时show the loading icon over a button

这是头部:

<head>
    <meta charset="UTF-8">
    <title>Button loading in Bulma</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.0/css/bulma.min.css">
</head>

这是正文代码:

<div class="container">
        <!--Vue container-->
    <div id="app">
        <div class="tile notification is-info">
            <div class="column">
                <h1>Newsletter</h1>
                <form>
                    <!-- Email -->
                    <div class="field">
                        <label class="label" for="email">Email</label>
                        <div class="control">
                            <input class="input is-small" type="email" id="email" placeholder="you@somewhere.com" autofocus>
                        </div>
                    </div>
                    <!-- Submit button -->
                    <div class="field">
                        <div class="control">
                            <button class="button is-primary" @click="loadingClass = 'is-loading'" :class="{ loadingClass }">Submit</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

这是vue.js脚本

<script src="https://unpkg.com/vue"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            loadingClass: ''
        }
    })
</script>

我无法让它发挥作用。我错过了什么?

解决方案:

好吧,我找到了一个解决方法here in the docs,所以我修复了如下:

形式:

<!-- Submit button -->
                        <div class="field">
                            <div class="control">
                                <button class="button is-primary" v-bind:class="{ 'is-loading' : loading }">Submit</button>
                            </div>
                        </div>

脚本:

<script>
    var app = new Vue({
        el: '#app',
        data: {
            loading: false
        }
    })
</script>

当我在控制台app.loading=true;中更改时,按钮确实更改为“正在加载”,反之为app.loading=false; 就是这样。

1 个答案:

答案 0 :(得分:1)

好的data:您希望对页面状态进行纯粹描述,因此请clicked : false

然后在您的处理程序中,修改您的状态:@click="clicked = true"

然后在动态类列表中,放入bulma加载类。

:class="{'button':true, 'is-primary':true, 'is-active':clicked}"

这涵盖了吗?