如何在Laravel Vue.js中单击按钮后隐藏Bootstrap-Vue模态

时间:2019-03-06 13:58:51

标签: laravel vue.js bootstrap-vue

我是laravel和vue.js的新手。

在刀片视图中,我有一个boots-vue模态,其中包括用于全文本搜索的vue.js组件,并在单击按钮时向后端发送发布请求。

因此,我想在执行请求后单击相同的按钮后隐藏此模式。

我尝试使用ref属性访问模态,然后按照文档here中所述调用hide()方法,但是它不起作用,我不知道自己缺少什么。

这是我的代码:

index.blade.php


@extends('layouts.app', ['navId' => 'teams'])
@section('title', 'Teams')
@section('content')
    <div class="container-fluid mt-5 pt-5 col-10">
        <div class="row">
            <div class="col-3 offset-6">
                <b-button v-b-modal="'myModal'">Add Users</b-button>
                <!-- The modal -->
                <b-modal id="myModal" class="" size="s" centered :hide-footer="true" :hide-header="true">
                    <div class="row justify-content-center">
                        <h4 class="">Add new Participant</h4>
                    </div>
                    <user-search />
                </b-modal>
            </div>
        </div>
    </div>

@endsection

UserSearch.vue:

<template>
    <div class="" ref="myModalRef">
        <div class="card">
            <input type="text" placeholder="Search ..." v-model="keywords" v-on:keyup="autoComplete" 
                    class="form-control bg-light border-0">
            <div class="panel-footer mt-2" v-if="results.length">
                <ul class="list-unstyled">
                    <div class="result-item my-3">
                        <li class="media mx-2 my-3" v-for="result in results" :key="result.id" @mouseover="hover = true"
                                                        @mouseleave="hover = false"
                                                        :class="{ active: hover }">
                            <img :src="result.avatar_path ? result.avatar_path : '/img/dashboard/user-placeholder.png'" 
                                    class="rounded-circle align-self-center mr-3" width="40" height="40"/>
                            <h5 v-text="result.name" class="mt-3"></h5>
                            <div class="media-body d-flex justify-content-end mt-3">
                                <b-form-checkbox :id="'selected-user' + result.id" v-model="usersToAdd" :value="result" class="" />
                            </div>
                        </li>
                    </div>
                </ul>
            </div>
        </div>

        <div v-if="usersToAdd.length" class="col-3 my-5 mx-auto">
            <b-button type="submit" @click="sendInviteToUsers">Done</b-button>
        </div>

    </div>

</template>
<script>
import bForm from 'bootstrap-vue/es/components/form/form';
import bFormGroup from 'bootstrap-vue/es/components/form-group/form-group';
import bFormInput from 'bootstrap-vue/es/components/form-input/form-input';

export default {
    name: 'UserSearch',
    data() {
        return {
            keywords: '',
            results: [],
            hover: false,
            usersToAdd: [],
        };
    },

    methods: {
        autoComplete() {
            this.results = [];
            if(this.keywords.length > 2 ){
                this.$axios.get('/api/users/search', { params: { keywords: this.keywords } })
                .then(response => {
                    this.results = response.data;
                })
                .catch(() => {
                        this.loading = false;
                        this.showErrorToast('An error occurred while Fetching Users. Please try again later');
                });

                // .catch(error => {});
            }

        },

        sendInviteToUsers() {
            if(this.usersToAdd.length) {
                this.$axios.post('/api/teams/invite-with-email/', this.usersToAdd);
            }
        }
    }
}
</script>
<style scoped>

</style>


1 个答案:

答案 0 :(得分:0)

您的代码存在问题,bootstrap-vue无法正确找到模式引用。尝试类似的方法

从您的 index.blade.php 中删除模态,并将其与 ref 一起包含在 UserSearch.vue 中。属性

index.blade.php

elementHTML

UserSearch.vue

@extends('layouts.app', ['navId' => 'teams'])
@section('title', 'Teams')
@section('content')
    <div class="container-fluid mt-5 pt-5 col-10">
        <div class="row">
            <div class="col-3 offset-6">
                <b-button v-b-modal="'myModal'">Add Users</b-button>

                <!--Removed modal tag from here and included it in UserSearch.vue-->
                <user-search />

            </div>
        </div>
    </div>

@endsection

在“ sendInviteToUsers”函数内部,按上述方法编辑该函数。在这里,从服务器收到api响应后,模式将关闭。