TypeError:window.intlTelInput 不是 reactjs 中的函数
Posted
技术标签:
【中文标题】TypeError:window.intlTelInput 不是 reactjs 中的函数【英文标题】:TypeError: window.intlTelInput is not a function in reactjs 【发布时间】:2022-01-06 02:33:05 【问题描述】:我是 ReactJS 的新手,在将 jQuery 与 React JS 一起用于 intlTelInput 时遇到此错误我已安装 npm jQuery 并导入所需的所有代码。我还在 index.html 中包含了所有 CSS 和 jQuery 链接,但代码仍然无法正常工作,我收到此错误
TypeError: window.intlTelInput 不是函数
如果有人对此问题有任何想法或解决方案,请帮助我找到方法
这是我的Index.html
页面,其中包含所有 CDN 链接:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="build/css/intlTelInput.css">
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/css/intlTelInput.min.css" />
<!-- JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/js/utils.min.js"></script>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!-- ---------------------------------------------------------------------------- -->
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable javascript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
这是我的登录页面代码。
import React from 'react'
import firebase from './firebase'
import "./App.css";
import getDatabase, ref, child, get from "firebase/database";
// import PhoneInput from 'react-phone-number-input'
import $ from 'jquery';
import intlTelInputUtils from 'jquery';
class Login extends React.Component
// <-------------------------------------------------------------------------------------->
// jQuery code
componentWillMount()
var phoneNumber = window.intlTelInput(document.querySelector("#phoneNumber"),
separateDialCode: true,
preferredCountries: ["in"],
hiddenInput: "full",
utilsScript: "//cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/js/utils.js"
);
$("#getCode").click(function ()
var full_num = phoneNumber.getNumber(intlTelInputUtils.numberFormat.E164);
$("input[name='phoneNumber[full]'").val(full_num);
localStorage.setItem("Phone_No", full_num)
);
// // <--------------------------------------------------------------------------------------------------------->
handleChange = (e) =>
const name, value = e.target
this.setState(
[name]: value
)
this.setState( phoneNumber: value , () =>
console.log(this.state.phoneNumber);
);
configureCaptcha = () =>
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('sign-in-button',
'size': 'invisible',
'callback': (response) =>
// reCAPTCHA solved, allow signInWithPhoneNumber.
this.onSignInSubmit();
// console.log("Recaptca varified")
,
// defaultCountry: "IN"
);
onSignInSubmit = (e) =>
e.preventDefault()
this.configureCaptcha()
const phoneNumber = this.state.mobile
const appVerifier = window.recaptchaVerifier;
const dbRef = ref(getDatabase());
get(child(dbRef, `Users/$phoneNumber`)).then((snapshot) =>
if (snapshot.exists())
firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
.then((confirmationResult) =>
// SMS sent. Prompt user to type the code from the message, then sign the
// user in with confirmationResult.confirm(code).
window.confirmationResult = confirmationResult;
alert('An OTP has been sent to your registered mobile number')
localStorage.setItem("Phone_No", phoneNumber)
console.log(localStorage.getItem('Phone_No'));
).catch((error) =>
console.error(error);
alert("Oops! Some error occured. Please try again.")
);
else
alert('Sorry, this mobile number is not registered with us. Please use your registered mobile number.');
)
onSubmitOTP = (e) =>
e.preventDefault()
const code = this.state.otp
console.log(code)
window.confirmationResult.confirm(code).then((result) =>
// User signed in successfully.
const Users = result.user;
console.log(JSON.stringify(Users))
this.props.history.push("/home");
).catch((error) =>
alert("You have entered wrong code")
);
render()
return (
<div className="Main-header">
<img src="./55k-logo.png" style= height: "80px", width: "200px" />
<br />
<div>
<h2>Login Form</h2>
<p>Limtless Water. From Unlimited Air.</p>
<form onSubmit=this.onSignInSubmit>
<div id="sign-in-button"></div>
/* <PhoneInput */
<label>Mobile Number</label> <br />
/* for="phoneNumber" */
<input type="tel" id="phone" name="mobile" placeholder="Enter Your Number" required onChange=this.handleChange />
<div className="buttons">
<button type="submit">Submit</button>
</div>
</form>
</div>
<div>
<form onSubmit=this.onSubmitOTP>
<label >Code</label> <br />
/* for="code" */
<input type="number" name="otp" placeholder="Enter The 6 Digit OTP" required onChange=this.handleChange />
<div className="buttons" >
<button type="submit">Submit</button>
</div>
</form>
</div>
</div>
)
export default Login;
【问题讨论】:
【参考方案1】:你应该避免在 React 项目中使用 jQuery。
对于 intl-tel-input,有 react-intl-tel-input 和 react-intl-tel-input-v2 包可供使用。
【讨论】:
以上是关于TypeError:window.intlTelInput 不是 reactjs 中的函数的主要内容,如果未能解决你的问题,请参考以下文章
反应本机获取多标记[未处理的承诺拒绝:TypeError:TypeError:未定义不是对象(评估'this.state.markers.map
Django TypeError - TypeError: issubclass() arg 1 必须是一个类
pyspark:TypeError:'float'对象不可迭代
Python 3.8 TypeError: can't concat str to bytes - TypeError: a bytes-like object is required, not 's