Skip to content

$inject

本文详解事件回调中 $inject 参数的数据结构及各项属性说明。

request.png

数据结构

ts
type Inject = {
    api: API,//表单的 API
    self: Rule,//当前组件的生成规则
    option: Object,//表单全局配置
    args: any[],//函数的原始参数
}

示例 1: 调用 API 方法

js
const api = $inject.api;
const formData = api.formData();

示例 2: 获取事件的原始参数

例如组件触发 change 事件时,会传递出当前的 value 值。

js
emit('change', value);
//or
//props.change(value);

获取 value 值

js
const value = $inject.args[0];

如果事件存在多个参数时

js
emit('beforeUpload', file, fileList);
//or
//props.beforeUpload(file, fileList);

获取参数

js
const file = $inject.args[0];
const fileList = $inject.args[1];

示例3: 修改当前组件规则

例如当 value 修改后通过接口修改组件状态

js
const api = $inject.api;
const value = $inject.args[0];
api.fetch({
    action: '/api/getdata',
    query:{
        value
    }
}).then(res=>{
    //修改自己
    $inject.self.options = res.data;
    //修改其他组件
    $inject.api.getRule('name').value = res.name;
})