【发布时间】:2019-11-22 01:19:33
【问题描述】:
我是 vuejs 新手,请帮我解决这个问题-
this.$http.get() is not working.
我试过了-
npm install vue-resource
和
npm install vue-resource --save
然后我写了这段代码
<template>
<v-container>
{{ questionData }}
</v-container>
</template>
data () {
return {
questionData: [],
mounted () {
this.$http.get('http://api.iqube.org.in/questions').then(response => {
this.questionData = response.body;
console.log(response.body)
}, error => {
});
}
控制台日志显示未定义。
这就是我的 main.js 的样子
import Vue from 'vue';
import router from './router';
import store from './store';
import './plugins/vuetify';
import './plugins/axios';
import './registerServiceWorker';
import App from './App.vue';
import VueResource from 'vue-resource';
Vue.use(VueResource);
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
我是在导入不需要的东西吗?
所有问题数据都应显示在页面中。但我得到一个空白页。我也在控制台中收到此错误
Cannot redefine property: $http
【问题讨论】:
-
axios 是一个通常用于 GET、POST 和其他操作的库。你可能想看看它。 ofc 在更简单的服务请求中可能没有必要,但由于其响应解析功能,它将大大简化您的代码。
-
“控制台日志显示未定义。” console.log 显示 什么 未定义?
-
@ceejayoz 最有可能
response.body -
@mcy 很有可能,我敢打赌 OP 需要在那里使用
response.data。 -
我仍然收到此错误:vue-resource.esm.js?28dd:1520 Uncaught TypeError: Cannot redefine property: $http at Function.defineProperties (
) at plugin (vue-resource .esm.js?28dd:1520) 在 Function.Vue.use (vue.runtime.esm.js?2b0e:5103) 在 eval (main.js?56d7:14) 在 Module../src/main.js ( app.js:7574) 在 webpack_require (app.js:724) 在 fn (app.js:101) 在 Object.1 (app.js:7948) 在 webpack_require (app.js:724) 在 app.js:791
标签: vue.js vue-resource