【问题标题】:Info not displaying in modal, however no errors and Vue component works in console信息未以模式显示,但没有错误,并且 Vue 组件在控制台中工作
【发布时间】:2018-04-28 03:35:26
【问题描述】:

希望以模式显示数据。我的设置如下:

app.js

Vue.component('modal', require('./components/Modal.vue'));

const app = new Vue({
    el: '#vue',
    data() {
        return {
            id: '',
            booking_start: '',
            booking_end: '',
            car: [],
            user: []
        };
    },
});

Modal.vue 组件:

<template>
    <div id="modal" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <slot name="header"></slot>
                </div>
                <div class="modal-body">
                    <slot></slot>
                </div>
                <div class="modal-footer">
                    <slot name="footer"></slot>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
</template>

<script>
    export default {
        name: 'modal',
        mounted() {
            console.log('Modal mounted.')
        },
        data() {
            return {}
        },
        props: ['id', 'booking_start', 'car', 'user'],
        mounted() {

        }
    }
</script>

Laravel 刀片:

<div id="vue">
        <modal v-bind="{{json_encode($reservation)}}">
            <template slot="header">
                <strong>Vehicle Checkout</strong>
            </template>
            <p>Ready to check out this vehicle?</p>
            <table class="table table-sm">
                <tr>
                    <th>Vehicle Name</th>
                    <td><span id="reservation-car-name">@{{ car.name }}</span></td>
                </tr>
                <tr>
                    <th>Vehicle Make / Model</th>
                    <td><span id="reservation-car-make-model"></span></td>
                </tr>
                <tr>
                    <th>Vehicle Registration</th>
                    <td><span id="reservation-car-registration"></span></td>
                </tr>
                <tr>
                    <th>Odometer Start</th>
                    <td><span id="reservation-car-odometer"></span></td>
                </tr>
            </table>
            <template slot="footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                <button type="submit" class="btn btn-primary">Checkout</button>
            </template>
        </modal>
    </div>

此时,我只是试图让数据显示在模态中。

查看 Vue 开发工具:

控制台没有报错,可以在控制台输出我要的数据。

我可能缺少一些非常基本的东西,因为我是 Vue 的新手,但我终生无法弄清楚它是什么。

【问题讨论】:

    标签: laravel vue.js


    【解决方案1】:

    组件标签替换为模板内容,所以将组件标签&lt;modal&gt;.your content.&lt;/modal&gt;中的所有内容放入modal组件中

    Vue.component('modal',{
      props:['car'],
      template: `<div><template slot="header">
                    <strong>Vehicle Checkout</strong>
                </template>
                <p>Ready to check out this vehicle?</p>
                <table class="table table-sm">
                    <tr>
                        <th>Vehicle Name</th>
                        <td><span id="reservation-car-name">{{ car.name }}</span></td>
                    </tr>
                    <tr>
                        <th>Vehicle Make / Model</th>
                        <td><span id="reservation-car-make-model">{{ car.model }}</span></td>
                    </tr>
                    <tr>
                        <th>Vehicle Registration</th>
                        <td><span id="reservation-car-registration">{{ car.reg }}</span></td>
                    </tr>
                    <tr>
                        <th>Odometer Start</th>
                        <td><span id="reservation-car-odometer">{{ car.odo }}</span></td>
                    </tr>
                </table>
                <template slot="footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Checkout</button>
                </template>
                </div>`
    })
    
    
    const app = new Vue({
        el: '#vue',
        data() {
            return {
                id: '',
                booking_start: '',
                booking_end: '',
                car: {name:'test1',model:'gh201',reg:'201821542',odo:'2018-01-01'},
                user: []
            };
        },
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/js/bootstrap.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
    <div id="vue">
      <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
      <!-- Modal -->
      <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
          <!-- Modal content-->
          <div class="modal-content">
            <modal :car="car"></modal>
           </div>
        </div>
      </div>
    </div>

    我的其他答案与此问题有关:router viewtransition-group

    【讨论】:

    • 这是否会失去在其他地方重用模态组件的能力,因为它不再使用插槽?
    • 我如何访问其他非汽车道具?
    • 我没有使用 slot 但你可以,我只是想让你知道组件标签被模板内容替换。
    • @kerrin :您可以将所有对象放入一个 udata 中,然后使用 udata.car 来获取 car 的值
    • @kerrin :如果您仍有疑问,请像我一样分享您的现场 sn-p。
    猜你喜欢
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2022-08-15
    • 2020-09-15
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多