Javascript “中的意外异步操作”;";Vue.js中的axios
作为Vue.js的新手,我有一个组件需要从远程服务器获取配置文件数据:Javascript “中的意外异步操作”;";Vue.js中的axios,javascript,vue.js,axios,Javascript,Vue.js,Axios,作为Vue.js的新手,我有一个组件需要从远程服务器获取配置文件数据: <template> <div v-if="token"> {{profile}} </div> </template> <script> import axios from 'axios'; export default { name: 'Profile', data () { return { pr
<template>
<div v-if="token">
{{profile}}
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'Profile',
data () {
return {
profile: {},
}
},
computed: {
token () {
return this.$store.getters.getToken;
},
mounted () {
return axios
.get( this.BASE_URL + '/profile')
.then( res => {
this.profile = res.data;
console.log('profile is:', res.data);
})
},
},
}
</script>
而且
error: Unexpected side effect in "mounted" computed property
86 | .get( this.BASE_URL + '/profile')
87 | .then( res => {
> 88 | this.profile = res.data;
| ^
如果我从mounted中omitreturn
,我也会得到以下错误:
error: Expected to return a value in "mounted" computed property
我真的很困惑。感谢您的提示以解决此问题。装载的级别应与计算的级别相同
<script>
import axios from 'axios';
export default {
name: 'Profile',
data() {
return {
profile: {},
};
},
computed: {
token() {
return this.$store.getters.getToken;
},
},
mounted() {
axios.get(this.BASE_URL + '/profile').then(res => {
this.profile = res.data;
console.log('profile is:', res.data);
});
},
};
</script>
从“axios”导入axios;
导出默认值{
名称:'Profile',
数据(){
返回{
档案:{},
};
},
计算:{
代币(){
返回此。$store.getters.getToken;
},
},
安装的(){
get(this.BASE_URL+'/profile')。然后(res=>{
this.profile=res.data;
log('profile is:',res.data);
});
},
};
<script>
import axios from 'axios';
export default {
name: 'Profile',
data() {
return {
profile: {},
};
},
computed: {
token() {
return this.$store.getters.getToken;
},
},
mounted() {
axios.get(this.BASE_URL + '/profile').then(res => {
this.profile = res.data;
console.log('profile is:', res.data);
});
},
};
</script>