我已经使用 Axios 发出了一个 get 请求,它按预期返回了一些数据,但我无法在挂载的函数中访问应用程序的数据属性来分配请求的结果。控制台日志this.productList返回undefined. 任何人都可以指出我正确的方向吗?
new Vue({
el: '#products',
data: function(){
return{
test: 'Hello',
productList: null
}
},
mounted: function(){
axios.get('https://api.coindesk.com/v1/bpi/currentprice.json').then(function(response){
console.log(response.data);
console.log(this.productList)
}).catch(function(error){
console.log(error);
})
}
})
Run Code Online (Sandbox Code Playgroud) 我正在试验使用VueJS的Kentico Delivery Preview API,该API允许您通过提交用于授权的承载令牌来获取未发布的内容(https://developer.kenticocloud.com/reference#authentication)。但是,无论我做什么我都会得到401的响应。PROJECT_ID,ITEM_NAME和TOKEN都是正确的,是从项目中提取的,因此这不是拼写错误。我承认我对auth没有太多经验,但是可以提供任何帮助:
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
},
mounted () {
axios
.request({
url: '/items/ITEM_NAME',
method: 'get',
baseURL: 'https://preview-deliver.kenticocloud.com/PROJECT_ID',
headers: {
'Authorisation': 'Bearer TOKEN'
}
})
.then(response => {
console.log(response.data)
})
}
})
Run Code Online (Sandbox Code Playgroud)