【问题标题】:call a component from another component in vue.js从 vue.js 中的另一个组件调用一个组件
【发布时间】:2016-07-14 00:57:24
【问题描述】:

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

【问题讨论】:

  • 注意添加一些代码...我可以告诉你有不止一种方法可以做到这一点。您的解决方案将取决于您的代码。无论你拥有什么......都会奏效。

标签: javascript vue.js vue-resource


【解决方案1】:

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

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

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

 &lt;alert :showalert.sync="showalert" type="default" :important="true"&gt;A book was saved successfully&lt;/alert&gt;

按下按钮,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 的新手,所以有更多知识的人可能会加入。

【讨论】:

    【解决方案2】:

    添加数据属性

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

    【讨论】:

    • 那么如何在book组件中调用alert组件呢?
    • 我正在使用 veuify 并且已经在 main.js 中导入了 Alert。但是我不能在 Book.vue 中调用 alert。它只是显示文本:一本书已成功创建!!!
    • 您的警报组件是什么样的?邮政编码。
    • 因此,在您的图书组件中,您需要添加一个组件属性,然后在脚本标签中导入警报组件文件。
    • 你能给我看一个在另一个组件中调用一个组件的例子吗?我是使用 vue.js 的新手。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2016-05-22
    • 2018-06-14
    • 1970-01-01
    • 2017-06-12
    • 2017-08-16
    • 2020-10-25
    • 2017-07-13
    相关资源
    最近更新 更多