VueJs单击按钮后,bootstrap 4 modal不显示

时间:2018-08-11 09:39:55

标签: laravel vue.js bootstrap-4 bootstrap-modal

我刚刚学习了Bootstrap 4的vuejs,并尝试显示模式,前提是当未单击创建按钮时,HTML模态标签未显示在inspect元素中,并且在创建按钮之后,单击了显示的引导模态。当第一次创建按钮时,单击inspect元素上的HTML模态标签显示,但是引导模态不能显示在浏览器页面上,而第二次可以显示引导模态。这是我使用Laravel编写的源代码,谢谢您。

  

User.vue

HTML

<template>
    <div class="container">
        <div class="row">
            <div class="col-sm">
                <div class="card-deck mb-3">
                    <div class="card mb-4 box-shadow">
                        <div class="card-header bg-success text-white">
                            <h4 class="my-0 font-weight-bold">
                                <!-- Button create user -->
                                <button @click="initAddUser()" class="btn btn-danger btn-sm"><i class="fa fa-plus"></i> Create New User</button>
                                <span class="float-right">User</span>
                            </h4>
                        </div>
                        <div class="card-body">

                            <!-- Bootstrap modal -->
                            <div v-if="showModal" class="modal fade" id="add_user_model" tabindex="-1" role="dialog" aria-labelledby="add_user_model_label" aria-hidden="true">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header bg-info text-white">
                                            <h5 class="modal-title" id="add_user_model_label"><i class="fa fa-plus"></i> Add New User</h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        </div>
                                        <div class="modal-body">
                                            <!-- Message errors create user -->
                                            <div class="alert alert-danger" v-if="errors.length > 0">
                                                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h5>{{ messages }}</h5>
                                                <ul>
                                                    <li v-for="error in errors">{{ error }}</li>
                                                </ul>
                                            </div>

                                            <div class="form-group">
                                                <label class="font-weight-bold" for="name">Name :</label>
                                                <input type="text" class="form-control" id="name" name="name" placeholder="Name" v-model="user.name">
                                            </div>

                                            <div class="form-group">
                                                <label class="font-weight-bold" for="name">Email :</label>
                                                <input type="email" class="form-control" id="email" name="email" placeholder="E-mail" v-model="user.email">
                                            </div>

                                            <div class="form-group">
                                                <label class="font-weight-bold" for="password">Password:</label>
                                                <input type="password" class="form-control" id="password" name="password" placeholder="Password" v-model="user.password">
                                            </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">Close</button>
                                            <button type="button" class="btn btn-sm btn-primary" @click="createUser()">Submit</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

Js

<script>
    export default {
        data() {
            return {
                user: {
                    name: '',
                    email: '',
                    password: ''
                },
                errors: [],
                users: [],
                showModal: false,

            }
        },
        mounted() {
            this.readUsers();
        },
        methods: {
            initAddUser() {
                this.errors = [];
                $("#add_user_model").modal("show");
                this.showModal = true;
            },
            reset() {
                this.user.name = '';
                this.user.email = '';
                this.user.password = '';
            },
            readUsers() {
                axios.get('/user/show')
                    .then(response => {
                        this.users = response.data.users;
                    });
            },
            createUser() {
                axios.post('/user', {
                        name: this.user.name,
                        email: this.user.email,
                        password: this.user.password,
                    })
                    .then(response => {
                        this.reset();
                        this.users.push(response.data.user);
                        this.readUsers();
                        $("#add_user_model").modal("hide");
                    })
                    .catch(error => {
                        this.errors = [];
                        this.messages = error.response.data.message;

                        if (error.response.data.errors.name) {
                            this.errors.push(error.response.data.errors.name[0]);
                        }

                        if (error.response.data.errors.email) {
                            this.errors.push(error.response.data.errors.email[0]);
                        }

                        if (error.response.data.errors.password) {
                            this.errors.push(error.response.data.errors.password[0]);
                        }
                    });
            }
        }
    }
</script>
  

结果图像检查元素

  1. 此图像按钮首次单击https://ibb.co/jKENbU
  2. 此图像按钮第二次单击https://ibb.co/nRy4qp

0 个答案:

没有答案