试图弄清楚如何使用Axios在Vue.js上发出GET请求,但失败了
我的Vue项目是使用webpack简单模板创建的,所以现在我的src文件夹中只有main.js和App.Vue。Main.js如下所示:试图弄清楚如何使用Axios在Vue.js上发出GET请求,但失败了,vue.js,vuejs2,axios,Vue.js,Vuejs2,Axios,我的Vue项目是使用webpack简单模板创建的,所以现在我的src文件夹中只有main.js和App.Vue。Main.js如下所示: import Vue from 'vue' import App from './App.vue' new Vue({ el: '#app', render: h => h(App) }) <template> <div id="app"> </div> </template> <scr
import Vue from 'vue'
import App from './App.vue'
new Vue({
el: '#app',
render: h => h(App)
})
<template>
<div id="app">
</div>
</template>
<script>
import axios from 'axios';
axios.defaults.baseURL = 'https://www.bungie.net/Platform';
axios.defaults.headers.common = {
'X-API-Key': 'ecab73fd6c714d02b64f0c75503671d1'
};
export default {
axios.get('/User/GetBungieNetUserById/1/')
.then(function(response) {
console.log(response.data);
console.log(response.status);
});
}
</script>
<style lang="scss">
</style>
App.vue如下所示:
import Vue from 'vue'
import App from './App.vue'
new Vue({
el: '#app',
render: h => h(App)
})
<template>
<div id="app">
</div>
</template>
<script>
import axios from 'axios';
axios.defaults.baseURL = 'https://www.bungie.net/Platform';
axios.defaults.headers.common = {
'X-API-Key': 'ecab73fd6c714d02b64f0c75503671d1'
};
export default {
axios.get('/User/GetBungieNetUserById/1/')
.then(function(response) {
console.log(response.data);
console.log(response.status);
});
}
</script>
<style lang="scss">
</style>
这只有在我有一个X-API-Key头的情况下才能完成
因为这个原因,我决定
axios.defaults.baseURL = 'https://www.bungie.net/Platform';
axios.defaults.headers.common = {
'X-API-Key': 'ecab73fd6c714d02b64f0c75503671d1'
};
然后我提出了获取请求。很遗憾,我遇到了一个编译失败的错误:
./node_modules/babel loader/lib/node_modules/vue loader/lib/selector.js?type=script&index=0/src/App.vue
模块生成失败:语法错误:C:/MAMP/htdocs/Destiny/src/App.vue:意外标记,应为,14:7
我是Vue的新手,所以我犯的错误可能真的很愚蠢。如果您想在安装Vue组件时运行此代码,我将不胜感激
export default {
mounted() {
axios.get('/User/GetBungieNetUserById/1/')
.then(function(response) {
console.log(response.data);
console.log(response.status);
});
},
}
不能只在导出默认块中使用原始JS,Vue需要使用各种属性,其中一些属性可能是/包含函数。哦,是的,确实如此。