从vue.js中的另一个组件调用组件

时间:2016-03-26 10:04:11

标签: javascript vue.js vue-resource

我有一个类似于此视频的提醒组件: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>

2 个答案:

答案 0 :(得分: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'传递给警报作为道具,警报显示为v-show条件现在为真,

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道具上的监视器会看到更改并触发一个方法,在超时属性中设置的任何秒数之后将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',
            }

因为此prop已同步回父级,因此按钮状态也会更新。

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

答案 1 :(得分:0)

添加数据属性

alertShow: false

接下来,在回调中:

this.alertshow = true;

如果要删除它,请将其设置为false。

在组件中添加一个指令:

v-show="alertshow"

更新

将一个组件属性添加到块组件。

components: {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) {
            });
}