1

我在这个视频中有一个警报组件:https ://laracasts.com/series/learning-vue-step-by-step/episodes/21我还有另一个组件(书)。创建一本书时,如何在成功回调函数中调用 Alert 组件,如下所示:

<alert>A book was created successfully !!!</alert>

我是使用 vue.js 的新手。谢谢您的帮助。

更新:这是我的代码

submit: function () {
    this.$http.post('/api/books/add', {
        data: this.data,
    }).then(function (response) {
        // I want to use Alert component right here to notice to users.
    }, function (response) {
    });
}

更新 2: 警报组件

<template>
    <div class="Alert Alert--{{ type | capitalize }}"
         v-show="show"
         transition="fade"
    >
        <slot></slot>

        <span class="Alert__close"
              v-show="important"
              @click="show = false"
        >
            x
        </span>
    </div>
</template>

<script>

    export default {
        props: {
            type: { default: 'info' },
            timeout: { default: 3000 },
            important: {
                type: Boolean,
                default: false
            }
        },

        data() {
            return {show: true};
        },

        ready() {
            if (!this.important)
            {
                setTimeout(
                    () => this.show = false,
                        this.timeout
                    )
            }

        }
    }

</script>

<style>
    .Alert {
        padding: 10px;
        position: relative;
    }

    .Alert__close {
        position: absolute;
        top: 10px;
        right: 10px;
        cursor: pointer;
    }

    .Alert--Info {
        background: #e3e3e3;
    }

    .fade-transition {
        transition: opacity 1s ease;
    }

    .fade-leave {
        opacity: 0;
    }
</style>

在 Book.vue 我想这样做:

// resources/assets/js/components/Book.vue
<template>
    .....
    <alert>A book was created successfully !!!</alert>

    //Create book form
    ....
</template>

<script>
    export default {
        methods: {
            submit: function () {
                    this.$http.post('/api/books/add', {
                    data: this.data,
                }).then(function (response) {
                     this.$refs.alert
                }, function (response) {
                });
    }
</script>
4

2 回答 2

1

这个 JSfiddle 可以满足您的需求:https ://jsfiddle.net/mikeemisme/s0f5xjxu/

我使用按钮按下而不是服务器响应来触发警报,并更改了一些方法名称,但原理是相同的。

警报组件嵌套在按钮组件内。Button 将 showalert 属性传递给带有同步修饰符集的警报组件。

 <alert :showalert.sync="showalert" type="default" :important="true">A book was saved successfully</alert>

按下按钮,showalert 设置为 'true','true' 作为 prop 传递给 alert,alert 显示为 v-show 条件现在为 true,

data() {
          //by default we're not showing alert.
          //will pass to alert as a prop when button pressed
          //or when response from server in your case
          return {
            showalert: false
          };
        },

在警报组件中的 showalert 属性上的手表会看到更改并触发一个方法,该方法在 timeout 属性中设置的任何秒数之后将 showalert 设置回“false”。

            //this method is triggered by 'watch', below
            //when 'showalert' value changes it sets the timeout
            methods: {
              triggerTimeout: function() {
                //don't run when detect change to false
                if (this.showalert === true) {
                  setTimeout(
                    () => this.showalert = false,
                    this.timeout
                  )
                }
              },
            },

            watch: {
              // detect showalert being set to true and run method
              'showalert': 'triggerTimeout',
            }

因为这个道具被同步回父级,所以按钮状态也更新了。

它有效,但使用手表等感觉有些夸张。Vue 可能有更好的方法来处理这个问题。我是 Vue 的新手,所以有更多知识的人可能会加入。

于 2016-03-27T17:09:19.960 回答
0

添加数据属性

alertShow: false

接下来,在回调中:

this.alertshow = true;

当您要删除它时,请将其设置为 false。

在组件中添加指令:

v-show="alertshow"

更新:

为块组件添加组件属性。

components: {Alert},

接下来在组件之外,导入 Alert 组件文件:

import Alert from './directory/Alert.vue'

以上是如果你使用的是 vueify。否则,使用添加组件

Vue.component

查看文档。

更新 2:

您的代码进行了更改:

<script>
import Alert from './directory/alert.vue';
export default {

    components: {
        Alert
    },

    methods: {
        submit: function () {
                this.$http.post('/api/books/add', {
                data: this.data,
            }).then(function (response) {
                 this.$refs.alert
            }, function (response) {
            });
}

于 2016-03-27T06:48:53.673 回答