gpt4 book ai didi

javascript - 如何正确重置 Vue Composition Api react 值

转载 作者:行者123 更新时间:2023-12-01 16:04:26 27 4
gpt4 key购买 nike

我想知道如何在 vuejs 设置中重置响应式(Reactive)? (我知道是否将其更改为 ref 并使用 view.value 将解决这个问题,但应该有一个使用响应式的答案)

setup(props, context){
// states
const DataTable = reactive((null as unknown) as DataTable);
const PolicyForm = reactive((null as unknown) as PolicyForm);
let view = reactive(resetViewState());
let config = reactive(
(resetPreRegisterConfig() as unknown) as PreRegisterConfig
);
// methods:
const fetchProfilelist = (
pagination: Pagination{ page:1, size:15},
sort_label: string = ""
) => {
DataTable.fetchTablelist(api_fetchProfilelist, pagination, sort_label);
};
const pageRefresh = () => {
view = resetViewState(); // 👈
config = resetPreRegisterConfig();
fetchProfilelist();

};
return {
DataTable,
PolicyForm,
view,
config,
fetchProfilelist,
pageRefresh
}

最佳答案

您可以使用 Object.assign :

  setup() {
const initialState = {
name: "",
lastName: "",
email: ""
};

const form = reactive({ ...initialState });

function resetForm() {
Object.assign(form, initialState);
}

function setForm() {
Object.assign(form, {
name: "John",
lastName: "Doe",
email: "john@doe.com"
});
}

return { form, setForm, resetForm };
}

See it live on codesandbox

学分: taken from here

关于javascript - 如何正确重置 Vue Composition Api react 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61184749/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com