1. 程式人生 > >Vue 子元件中觸發父元件方法

Vue 子元件中觸發父元件方法

父元件中:

<子元件名 @close="close"></子元件名>
methods: {
    close(id) {
        this.$confirm('此操作將永久刪除該檔案, 是否繼續?', '提示', {
            confirmButtonText: '確定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.$message({
                type
: 'success', message: '刪除成功!' }); }).catch(() => { this.$message({ type: 'info', message: '已取消刪除' }); }); }

子元件中:

<el-button size="mini" type="primary" @click="close('5')">關閉</el-button
>
methods: {
close(id){
    this.$emit('close',id);
    console.log(id);
}
}子元件中通過this.$emit()觸發父元件中方法