如何从 Vue 中的 onSubmit 函数调用方法?
How can I call a method from onSubmit functon in Vue?
我有一个按钮,用于在填写表单时通过 onSubmit 函数登录用户。但是,我需要调用另一个方法来获取有关用户的一些额外数据,例如权限。但是我无法让它工作。我试图将所需方法的代码直接放在 onSubmit() 函数中,但它没有用。这是我的代码:
这是按钮所在的窗体。
<form @submit.prevent="onSubmit">
Some code
<button class="btn btn-success" type="submit">Log in</button>
</form>
这是我的脚本。
<script lang="ts">
import {defineComponent, reactive} from 'vue'
import userStore from '../store/user'
import { useStore } from "vuex";
export default defineComponent({
setup() {
const form = reactive({
username: "",
password: "",
});
//Calls the login function in user.ts
const onSubmit = () => {
userStore.login(form.username, form.password);
form.username = "";
form.password = "";
};
//Returns the store in the main so that the form can have a template that displays the error message.
return {form, userStore, onSubmit}
},
methods:{
//This did not work. useStore could not be accessed from "methods" but could be accessed from setup/mounted.
//The thing is, it must be updated only after clickig on the submit button.
setUserState(){
const store = useStore();
store.dispatch("setActiveUser");
},
}
})
</script>
我现在找到了解决问题的方法。我将 setUserState() 作为函数移到 setup() 下并删除了这些方法。安装程序现在正在设置商店和 returns setUserState。以下代码有效:
<script lang="ts">
import {defineComponent, reactive} from 'vue'
import userStore from '../store/user'
import { useStore } from "vuex";
export default defineComponent({
setup() {
const store = useStore();
const form = reactive({
username: "",
password: "",
});
//Calls the login function in user.ts
function onSubmit() {
userStore.login(form.username, form.password);
form.username = "";
form.password = "";
setUserState()
}
function setUserState(){
store.dispatch("setActiveUser");
}
//Returns the store in the main so that the form can have a template that displays the error message.
return {form, userStore, onSubmit, setUserState}
},
})
</script>
我有一个按钮,用于在填写表单时通过 onSubmit 函数登录用户。但是,我需要调用另一个方法来获取有关用户的一些额外数据,例如权限。但是我无法让它工作。我试图将所需方法的代码直接放在 onSubmit() 函数中,但它没有用。这是我的代码:
这是按钮所在的窗体。
<form @submit.prevent="onSubmit">
Some code
<button class="btn btn-success" type="submit">Log in</button>
</form>
这是我的脚本。
<script lang="ts">
import {defineComponent, reactive} from 'vue'
import userStore from '../store/user'
import { useStore } from "vuex";
export default defineComponent({
setup() {
const form = reactive({
username: "",
password: "",
});
//Calls the login function in user.ts
const onSubmit = () => {
userStore.login(form.username, form.password);
form.username = "";
form.password = "";
};
//Returns the store in the main so that the form can have a template that displays the error message.
return {form, userStore, onSubmit}
},
methods:{
//This did not work. useStore could not be accessed from "methods" but could be accessed from setup/mounted.
//The thing is, it must be updated only after clickig on the submit button.
setUserState(){
const store = useStore();
store.dispatch("setActiveUser");
},
}
})
</script>
我现在找到了解决问题的方法。我将 setUserState() 作为函数移到 setup() 下并删除了这些方法。安装程序现在正在设置商店和 returns setUserState。以下代码有效:
<script lang="ts">
import {defineComponent, reactive} from 'vue'
import userStore from '../store/user'
import { useStore } from "vuex";
export default defineComponent({
setup() {
const store = useStore();
const form = reactive({
username: "",
password: "",
});
//Calls the login function in user.ts
function onSubmit() {
userStore.login(form.username, form.password);
form.username = "";
form.password = "";
setUserState()
}
function setUserState(){
store.dispatch("setActiveUser");
}
//Returns the store in the main so that the form can have a template that displays the error message.
return {form, userStore, onSubmit, setUserState}
},
})
</script>