Vue学习:$event使用

主宰稳场 提交于 2020-03-05 06:42:30

我的使用场景

子组件向父组件传值,父组件在接受子组件传过来的值时,还会附加一些其他参数

父组件的部分代码
$event代表从子组件中传递过来的值;requestData、record.identifier、remark是父组件自身想要给方法传递的参数,并非来自子组件

<template>
    <a-table :columns="columns" :dataSource="requestData" rowKey="identifier" bordered>
        <template slot="remark" slot-scope="text, record">
            <editable-cell :text="text" @change="onCellChange(requestData, record.identifier, 'remark', $event)"/>
        </template>
    </a-table>
</template>

子组件的代码 EditableCell.vue

<template>
    <div class="editable-cell">
        <div v-if="editable" class="editable-cell-input-wrapper">
            <a-input :value="value" @change="handleChange" @pressEnter="check" /><a-icon
                type="check"
                class="editable-cell-icon-check"
                @click="check"
        />
        </div>
        <div v-else class="editable-cell-text-wrapper">
            {{ value || ' ' }}
            <a-icon type="edit" class="editable-cell-icon" @click="edit" />
        </div>
    </div>
</template>
<script>
    export default {
        props: {
            text: String,
        },
        data() {
            return {
                value: this.text,
                editable: false,
            };
        },
        methods: {
            handleChange(e) {
                const value = e.target.value;
                this.value = value;
            },
            check() {
                this.editable = false;
                # 向父组件传值
                this.$emit('change', this.value);
            },
            edit() {
                this.editable = true;
            },
        },
    };
</script>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!