javascript - v-for 列表中的 Vue2 模态

标签 javascript laravel vue.js vuejs2

我正在尝试实现一个 vue2 modal如 vue 文档中所述 https://v2.vuejs.org/v2/examples/modal.html .

它看起来像这样:

<tbody v-if="fields.length">
    <tr v-for="(item, index) in fields">
        <td>@{{ item.name }}</td>
        <td><input type="checkbox" id="checkbox" v-model="item.active"></td>
        <td>
            <button id="show-modal" @click="showModal = true">Show Modal</button>
        </td>
    </tr>
    <modal :item="item" v-if="showModal" @close="showModal = false">
        <h3 slot="header">Hello World</h3>
    </modal>
</tbody>


Vue.component('modal', {
    template: '#modal-template',
    data: function() {
        return {
            item: ''
        }
    }
});

当按钮显示并弹出模式时,我收到一条警告:Property or method "item" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. ,当我检查modal时Vue 开发工具中的组件,item仍然是'' ,它没有由 :item 填充绑定(bind)。

通过item的正确方法是什么?对象放入模态中以便我们可以在窗口中使用它?

最佳答案

如果您将 item 作为值从父组件传递给子组件,则不能使用 data - 必须使用 props 相反!

Vue.component('modal', {
    template: '#modal-template',
    props: ['item'],
    data: function() {
        return {

        }
    }
});

关于javascript - v-for 列表中的 Vue2 模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49413427/

相关文章:

javascript - 防止在密码字段中触发按键事件

php - Laravel:如何打开多个域(不是子域)以显示来自同一服务器的页面(无重定向)?

jquery - 在 Laravel 中显示 JQuery 模态?

javascript - Vue : How to build bundle for Nuxt with vue-cli-service?

Vue.JS - nextTick 错误 : “TypeError: Cannot read property ‘badInput’ of undefined”

javascript - 如何为覆盖全屏宽度且不溢出的元素设置动画

javascript - Highcharts - 获取点击事件的图表

javascript - Vue.js 组件层次关系

javascript - 使用 Protractor 检测 Iframe 是否有 Angular

php - Braintree 无法执行充电 : Custom field is invalid: description