在vue3 watch回调中,使用form的setValue、setFeedback等方法会报错 #1881
-
版本 代码 操作 问题 期望 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
尝试过用nextTick将setFeedback setValues事件放渲染后处理,也还是有问题。 |
Beta Was this translation helpful? Give feedback.
-
可能是 @formily/reactive-vue 与 vue3 的响应体系结合产生的问题,待排查。这种情况可以考虑利用 @formily/reactive 提供的响应系统来替代,比如上面的例子可以改写为下面的代码,不过需要多写一个dispose: <template>
<FormProvider :form="form">
<SchemaField :schema="schema"> </SchemaField>
<FormConsumer>
<template #default="{ form }">
{{ form.pattern }}
<div style="white-space: pre; margin-bottom: 16px">
{{ JSON.stringify(form.values, null, 2) }}
</div>
</template>
</FormConsumer>
<button @click="tryTest">设置校验</button>
<button @click="tryTest2">设置值</button>
<!-- <button @click="submitPattern">保存</button> -->
</FormProvider>
</template>
<script>
import {
Form,
DatePicker,
Upload as AUpload,
Select,
Cascader,
Input as AInput,
} from "ant-design-vue";
import { createForm } from "@formily/core";
import { observable, Tracker } from "@formily/reactive";
import {
FormProvider,
FormConsumer,
createSchemaField,
connect,
mapProps,
mapReadPretty,
} from "@formily/vue";
import "ant-design-vue/dist/antd.css";
import { onUnmounted } from 'vue';
const FormItem = connect(
Form.Item,
mapProps(
{
title: "label",
description: "extra",
required: true,
validateStatus: true,
},
(props) => {
return {
...props,
// help: field.errors?.length ? field.errors : undefined,
};
}
)
);
const Upload = connect(
AUpload,
mapProps(function () {
return {
onChange: function () {
return [];
},
};
})
);
const Input = connect(
AInput,
mapReadPretty((props) => <div>{props.value}</div>)
);
const { SchemaField } = createSchemaField({
components: {
FormItem,
DatePicker,
Upload,
Select,
Cascader,
Input,
},
});
let schema = {
type: "object",
properties: {
input: {
type: "string",
title: "文本",
"x-component": "Input",
"x-decorator": "FormItem",
},
},
};
export default {
components: { FormProvider, FormConsumer, SchemaField },
setup () {
const form = createForm({
pattern: "editable",
})
const setFeedBack = () => {
form.query("input").forEach((field) => {
field.setFeedback({
code: "ValidateError",
messages: ["该字段不能重复"],
triggerType: "onInput",
type: "error",
});
});
}
const setValue = () => {
form.setValues({ input: "test2" }, "overwrite");
}
const obs = observable({
test: false,
test2: false,
})
const tryTest = () => {
obs.test = true;
}
const tryTest2 = () => {
obs.test2 = true;
}
const tracker = new Tracker(() => setFeedBack())
tracker.track(() => obs.test)
const tracker2 = new Tracker(() => setValue())
tracker2.track(() => obs.test2)
onUnmounted(() => {
tracker.dispose()
tracker2.dispose()
})
return {
form,
schema,
tryTest,
tryTest2
};
},
methods: {
submitPattern() {
this.form.submit((res) => {
console.log(res);
alert("提交成功");
});
},
}
};
</script> |
Beta Was this translation helpful? Give feedback.
可能是 @formily/reactive-vue 与 vue3 的响应体系结合产生的问题,待排查。这种情况可以考虑利用 @formily/reactive 提供的响应系统来替代,比如上面的例子可以改写为下面的代码,不过需要多写一个dispose: