[英]How to fix 'This expression is not constructable ' error in typescript?
I am begginer with ts and react,an I get error while creating a new class object of auth service.我是 ts 的初学者并做出反应,在创建身份验证服务的新 class object 时出现错误。
const Auth = new Authentication();
Error错误
This expression is not constructable.
此表达式不可构造。 Type 'typeof import("c:/react-js/portfolio/src/Services/IdentityServices/Authentication")' has no construct signatures.
类型 'typeof import("c:/react-js/portfolio/src/Services/IdentityServices/Authentication")' 没有构造签名。
my Authentication Service我的身份验证服务
import axios from 'axios';
class AuthenticationServices {
API_URL = 'https://localhost:44382/api/authenticate/';
login(username: string, password: string) {
return axios
.post(this.API_URL + 'login', {
username,
password,
})
.then((reponse) => {
if (reponse.data.token) {
localStorage.setItem('user', JSON.stringify(reponse.data));
}
return reponse.data;
});
}
logout() {
localStorage.removeItem('user');
}
register(username: string, password: string, email: string) {
return axios.post(this.API_URL + 'register', {
username,
password,
email,
});
}
getCurrenUser() {
let currentUser = localStorage.getItem('user');
if (currentUser) return JSON.parse(currentUser);
return 'Current user is empty';
}
}
My register component:我的注册组件:
import React from 'react';
import { Container, Row, Form, Button, Col } from 'react-bootstrap';
import { SubmitHandler, useForm } from 'react-hook-form';
import { RegisterPage } from '../Pages/RegisterPage';
import * as Authentication from '../Services/IdentityServices/Authentication';
type IFormInput = {
email: string;
username: string;
password: string;
};
export const Register = () => {
const {
register,
watch,
handleSubmit,
formState: { errors },
} = useForm<IFormInput>();
const [createUser, setCreateUser] = React.useState<IFormInput>();
const Auth = new Authentication();
const onSubmit: SubmitHandler<IFormInput> = (data) => {
console.log(data);
};
return (
<Container>
<Row></Row>
<RegisterPage title="Kayıt Ol">
<Container>
<Row className="d-flex justify-content-center ">
<Col xs={5} className="shadow-sm p-3 mb-5 bg-body rounded ">
<Form onSubmit={handleSubmit(onSubmit)}>
<Form.Group className="mb-3" controlId="formBasicEmail">
<Form.Label>Email adresi</Form.Label>
<Form.Control
type="email"
placeholder="Enter email"
{...register('email')}
/>
<Form.Text className="text-muted"></Form.Text>
</Form.Group>
<Form.Group className="mb-3" controlId="formBasicPassword">
<Form.Label>Kullanıcı Adı</Form.Label>
<Form.Control
type="text"
placeholder="Kullanı adı"
{...register('username')}
/>
</Form.Group>
<Form.Group className="mb-3" controlId="formBasicPassword">
<Form.Label>Şifre</Form.Label>
<Form.Control
type="password"
placeholder="Şifre"
{...register('password')}
/>
</Form.Group>
<Button variant="primary" type="submit">
Kayıt ol
</Button>
</Form>
</Col>
</Row>
</Container>
</RegisterPage>
</Container>
);
};
import * as Authentication
imports the module's module namespace object , which has properties for each of the module's exports; import * as Authentication
导入模块的模块命名空间 object ,它具有每个模块导出的属性; it's not constructible.它是不可构造的。
You haven't shown any export of AuthenticationServices
, so it's hard to tell you how to import it.您没有显示任何
AuthenticationServices
的导出,因此很难告诉您如何导入它。 If you exported it as a named export (generally preferred), like this:如果您将其导出为命名导出(通常首选),如下所示:
export AuthenticationServices;
...then you'd import it with {}
, like this: ...然后您将使用
{}
导入它,如下所示:
import { AuthenticationServices } from "...";
// or if you want to rename it
import { AuthenticationServices as Authentication } from "...";
If you export it as the default export, like this:如果将其导出为默认导出,如下所示:
export default AuthenticationServices;
...then you'd import it like this: ...然后你会像这样导入它:
import AuthenticationServices from "...";
// Or if you want to use a different name
import Authentication from "...";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.