这是代码中出现的错误 属性'totalVuePackages' 类型上不存在



我不知道该怎么做,请帮助我。我正在学习v3js。我必须带着数据去核实。但是在控制台中写了一个错误:属性"totalvueppackages"在类型"{name: string;data(): {totalVuePackages:字符串;};创建():空白;}"。


<script lang='ts'>

import axios from 'axios';
export default {
name: 'componentHome',
data(){
return{
totalVuePackages:'',
}
},
created() {
// GET request using axios with error handling
axios.get("https://jsonplaceholder.typicode.com/posts")
.then(response => this.totalVuePackages = response.data.total)
.catch(error => {

this.errorMessage = error.message;
console.error("There was an error!", error);
});
}
}
' ' '

I tried changing and renaming but it doesn't work.
I do not understand anything 

您得到的错误是一个类型错误。您将totalvueppackages定义为字符串,但来自api调用的响应不是字符串值,这就是为什么您会得到此错误。您可以通过替换数据方法来解决这个问题,如下面的代码-

data(){
return{
totalVuePackages:'' as any
}
} 

,你的API调用为:

axios.get("https://jsonplaceholder.typicode.com/posts")
.then(response : any => this.totalVuePackages = response.data.total)
.catch(error => {

this.errorMessage = error.message;
console.error("There was an error!", error);
});

希望能成功!

最新更新