【问题标题】:Vuejs Accessing Data in Modal Component Using PropsVuejs 使用 Props 访问模态组件中的数据
【发布时间】:2019-05-15 13:15:11
【问题描述】:

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

这是父模板。

    <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>

如何让父级中的单个用户显示在模态中?

【问题讨论】:

    标签: javascript html node.js vue.js vuejs2


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 谢谢。这正是我一直在寻找的。它有效。
    【解决方案2】:

    首先,您必须将 euser 属性传递给 &lt;edit-user/&gt; 组件,而不是调用编辑的按钮。

    其次,initEdit() 函数应该看起来更像这样

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

    第三,如果您计划在模式中编辑用户,您可能需要在子组件中创建用户的副本。

    watch: {
      showEditUser() {
        this.editableUser = JSON.parse(JSON.stringify(this.euser))
      }
    }
    

    那么孩子身上的所有v-models 都应该指向this.editableUser。 当用户去保存编辑时,您可以发出一个新函数,可以像这样将新版本传回给父级

    saveEdit() {
       this.$emit('save', this.editableUser)
    }
    

    您只需要像这样在&lt;edit-user /&gt; 组件中保存文件

    <edit-user v-show="showEditUser" :showEditUSer.sync="showEdit" :euser="user" @save="saveUser" />
    
    //script...data...methods
    saveUser(user) {
      let ind = this.users.map(u => u.id).indexOf(user.id)
      if(ind != -1) {
        Object.assign(this.users[ind], user) //put user back where it belongs with new data
      }
    }
    

    【讨论】:

    • 将它与@luigi 结合使用使我能够在两个组件之间传递数据。谢谢。
    【解决方案3】:

    像这样传递用户:

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

    并更改 prop 属性以接收对象而不是数组

      euser: {
        type: Object,
      },
    

    尝试将您的主要组件数据添加到用户只是为了测试“未定义”问题是否来自那里。并在:euser="user[0]"

    user: [{
       id: 1
    }]
    

    【讨论】:

    • 感谢您的回复。我已经更改了道具属性。我也尝试按照您的建议传递用户。用户仍未显示。该对象是“未定义的”。
    • 我刚刚添加了一些代码来测试对象“未定义”
    • 我进行了修改,但在我的子组件上,用户没有显示。
    猜你喜欢
    • 2019-03-30
    • 2016-06-03
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2020-10-16
    • 2021-01-16
    • 1970-01-01
    • 2018-03-02
    相关资源
    最近更新 更多