javascript - Vuejs 使用 Props 访问模态组件中的数据

标签 javascript html node.js vue.js vuejs2

有与我提出的问题非常相似的问题,但提出的解决方案不符合我的情况。 我正在尝试使用 vue 中的模态组件从不同组件中的父列表访问数据。 我尝试在循环中传递 prop 值以及在父 View 中传递使用的组件,但没有收到任何数据。

这是父模板。

    <template>
    <table class="table table-bordered table-stripped" v-if="users.length>0">
        <caption>List of Contacts</caption>
        <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Name</th>
                <th scope="col">Action</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="(user, index) in users" :key="user.id">
                <td>{{index+1}}</td>
                <td>{{user.name}}</td>
                <td>
                    <button type="button" class="btn btn-success btn-sm" @click="initEdit(user)" :euser="user">Edit</button>
                </td>
            </tr>
        </tbody>
    </table>
    <edit-user v-if="showEditUser" :showEditUser.sync="showEdit"></edit-user>
</template>

<script>
import editUser from '@/components/editUser.vue';
export default {
  name: 'listusers',
  components: {
        'edit-user': editUser,
    },
    data() {
      return {
          user: [],
          users: [],
          euser: {},
          showEdit: false,
        };
  },
    methods: {
        initEdit() {
            this.showEditUser = true;
        },
    },
};
</script>

这是模态组件。

    <template>
        <transition name="modal" role="dialog">
            <div class="modal" style="display: block">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Edit Contact</h5>
                    </div>
                    <div class="modal-body">
                        <p>{{euser}}</p>
                        <p>{{euser.id}}</p>
                    </div>
                    <div class="modal-footer">
                            <button type="button" class="btn btn-default" @click="closeModal">Close</button>
                    </div>
                    </div>
                </div>
            </div>
        </transition>
    </template>
<script>

export default {
    name: 'editUser',
    props: {
      euser: {
        type: Object,
      },
      showEdit: {
          'default' : false,
      }
    },
    data() {
        return {
            edit_user: [],
        };
    },
  methods: {
        closeModal(){
            this.$emit('update:showEdit');
        },
  },
};
</script>

我尝试在如上所示的循环以及如下所示的组件中传递 prop 值。

<edit-user v-if="showEditUser" :showEditUser.sync="showEdit" :euser="user"></edit-user>

如何从父级获取单个用户以在模式中显示?

最佳答案

在父组件中,您可以创建一个名为“currUser:null”的数据属性,并在“initUser”方法上执行以下操作:

initEdit(user){
    this.currUser=user;
    this.showEditUser = true;
}

那么你的模态组件定义将如下所示:

<edit-user v-if="showEditUser" :showEditUser.sync="showEdit" :euser="currUser"> 
</edit-user>

关于javascript - Vuejs 使用 Props 访问模态组件中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56150323/

相关文章:

javascript - 无法在 AngularJS 中将数据从指令传递到 html

html - Bootstrap 4 中的 Flexbox 表现得很奇怪

javascript - Node.js 和 Request - 限制下载文件的文件大小

node.js - 处理嵌套事务的简便方法

javascript - 将对象插入数组失败

javascript - iOS Web 应用程序 - 仅在添加到主屏幕时显示内容?

javascript - Angular 工厂和过滤器 $http

html - ASP.NET Gridview 固定 header 可滚动不起作用

html 导航菜单下拉菜单只显示最后一个菜单项

node.js - 检查 req.files 对象是否为空