大家好,我是 Vue 3 和 Typescript 的新手,我很难在我的项目中解决 typescript 错误,下面是 typescript 错误。
它无法找到验证方法,但是当我尝试对其进行控制台时它会显示出来。
“字符串”类型不存在属性“验证”。
我正在使用带有 typescript 和 Vue Element Plus 的 Vue 3 composition api,下面是我的组件代码。
零件
<template>
<div class="w-full h-full absolute flex">
<el-form
class="w-4/5 sm:w-1/2 lg:w-1/3 xl:w-1/4 m-auto"
:ref="formRef"
:model="form"
:rules="rules"
>
<h2>Login</h2>
<el-form-item label="Email" prop="email">
<el-input
type="email"
autocomplete="off"
v-model="form.email"
/>
</el-form-item>
<el-form-item label="Password" prop="password">
<el-input
type="password"
autocomplete="off"
v-model="form.password"
/>
</el-form-item>
<el-form-item>
<el-button
type="success"
class="w-full"
@click="handleLogin"
>Submit</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script lang="ts">
import { defineComponent, onBeforeMount, onMounted, reactive, ref } from "vue";
export default defineComponent({
name: "Login",
setup() {
const form = reactive({
email: "",
password: ""
});
const rules = ref({
email: [{
required: true,
message: "Email is required",
trigger: "blur"
}],
password: [{
required: true,
message: "Password is required",
trigger: "blur"
}]
});
const formRef = ref("formRef");
const handleLogin = () => {
console.log(formRef)
// Property 'validate' does not exist on type 'string'.
formRef.value?.validate((valid: boolean) => {
console.log(valid);
});
};
onMounted(() => {
console.log("Login mounted");
});
onBeforeMount(() => {
console.log("Login onBeforeMount");
});
return {
formRef,
form,
rules,
handleLogin
};
}
});
</script>