Error: "Argument of type 'string | string' is not assignable to argument of type 'string'"
P粉232409069
P粉232409069 2023-08-25 11:44:28
0
1
511
<p>I am developing a Vue project using both TypeScript and axios to make API calls and develop a reset password component, the resetPassword function in my auth.ts file looks like this</p> <pre class="brush:php;toolbar:false;">resetPassword(password1: string, password2: string, uid: string, token: string): Promise<any> { return new Promise<any>((resolve, reject) => { axios .post("API", { password1, password2, uid, token }) .then((res : any) => { resolve(//RESOLVE); }) .catch((error) => { //REJECT }) }) }</pre> <p>In my ResetPassword.vue file I use newPassword like this</p> <pre class="brush:php;toolbar:false;">this.resetPassword(password1.value, password2.value, this.$route.params.id, this.$route.params.resetID).then( (res) => { if(password1.value == password2.value){ Notification{ "SUCCESS" }); }</pre> <p>In my ResetPassword.vue file, I get an error on the third parameter saying <strong>"Type 'string[]' is not assignable to type 'string'."</strong></p> ; <p>I'm a little new to ts and I need some help. </p> <p>I'm confused between the two solutions here, should I have <strong>this.$route.params.id as a string</strong> <em>or is this better</ em> <strong>this.$ Route.params.id.toString()</strong></p>
P粉232409069
P粉232409069

reply all(1)
P粉322106755

Route parameters can contain many values ​​with the same key. That's why it's probably an array of strings and not a single string. If you are sure there is only one parameter, you can use as string to tell the typescript compiler that you know 100% that this variable is a string and not a string[]代码>

this.resetPassword(password1.value, password2.value, this.$route.params.id as string, this.$route.params.resetID as string)

If you would use .toString() and would have an array like ["foo", "bar"] you would get "foo , bar" as the result of .toString()

If you are not sure whether it is an array, you can check it. If it is an array, take the first value:

let id: string;
if (Array.isArray(this.$route.params.id)) {
    id = this.$route.params.id[0];
} else {
    id = this.$route.params.id;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!