在父组件传给子组件的方法中添加额外的参数

2020/3/14 vue

# 子组件使用props

# 子组件:

<script>
export default {
    name: 'child',
    props: {
        http-request: {
            type: Function,
            required: true
        }
    },
    created: {
        this.http-request(param1, param2);
    }
};
</scrip>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 父组件:

<template>
    <child :http-request="(param1, param2)=>httpRequest(param1, param2, customParam)"></child>
</template>
<script>
export default {
    name: 'parent',
    methods: {
        httpRequest (param1, param2, customParam) {}
    }
};
</script>
1
2
3
4
5
6
7
8
9
10
11

# 子组件使用$emit

# 子组件:

<script>
export default {
    name: 'child',
    created: {
        this.$emit('http-request', 'param1', 'param2');
    }
};
</script>
1
2
3
4
5
6
7
8

# 父组件:

方法一: 只能获取第一个参数

<template>
    <child @http-request="httpRequest(...arguments, customParam)"></child>
    <!-- 等同于 <child @http-request="httpRequest($event, customParam)"></child> -->
</template>
<script>
export default {
    name: 'parent',
    methods: {
        httpRequest (param, customParam) {
            console.log(param); // param1
        }
    }
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

方法二: 子组件的参数以数组展示

<template>
    <child @http-request="httpRequest(arguments)"></child>
</template>
<script>
export default {
    name: 'parent',
    methods: {
        httpRequest (params, customParam) {
            console.log(params); // ['param1', 'param2']
        }
    }
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13