problem description
I want to emulate cnode"s function of giving likes to comments, using the vue framework, but I made a mistake when I clicked.
the environmental background of the problems and what methods you have tried
I write the post request as a plug-in in server/index.js, call it in comments.vue, and there will be an error. It"s no problem if you write the post request directly in the component.
related codes
/ / Please paste the code text below (do not replace the code with pictures)
comments.vue
methods: {
async up (id) {
let accesstoken = Cookies.get("accesstoken");
if(accesstoken){
//
//
let {data} = await this.$api.up(id, {accesstoken});
console.log(data);
//
axios.post(`https://cnodejs.org/api/v1/reply/${id}/ups`, {accesstoken}).then((res) => {
console.log(res.data);
});
}else{
//
alert("");
}
}
}
server/index.js
let instance = axios.create ({
baseURL: "https://cnodejs.org/api/v1"
});
/ / like the comments
export function up (id ="", params = {}) {
let defaults = {
accesstoken: ""
};
Object.assign(defaults, params);
return instance.post(`/reply/${id}/ups`, { params: defaults });
};
export default {
install(vue) {
vue.prototype.$api = {
up
};
}
};
what result do you expect? What is the error message actually seen?
should have printed a return value like this:
{"success": true, "action": "down"}
, but now it reports an error:
401 Unauthorized