Javascript 单击Vue.js中的按钮时,在我最喜欢的页面中显示我的API
我最近一直在使用Vue.js框架,所以它对我来说非常新 当我尝试单击我的按钮时,我想检索API的ID并将其发送到我的收藏夹页面Javascript 单击Vue.js中的按钮时,在我最喜欢的页面中显示我的API,javascript,vue.js,vuex,store,Javascript,Vue.js,Vuex,Store,我最近一直在使用Vue.js框架,所以它对我来说非常新 当我尝试单击我的按钮时,我想检索API的ID并将其发送到我的收藏夹页面 <template> <button v-model="newCharacter" class="new-character" autofocus autocomplete="off" placeholder="Ajouter en fav
<template>
<button
v-model="newCharacter"
class="new-character"
autofocus
autocomplete="off"
placeholder="Ajouter en favoris ?"
@keypress.enter="addCharacter"
>
</template>
<script>
import axios from 'axios';
export default {
name: 'FavoriteButton',
data() {
return {
characters: [],
page: 1,
pages: 1,
currentCharacter: {},
};
},
created() {
this.fetch();
},
methods: {
addCharacter() {
this.$store.commit('addCharacter', this.newCharacter);
this.newCharacter = '';
},
fetch() {
const params = {
page: this.page,
};
const url = 'https://rickandmortyapi.com/api/character';
return axios
.get(url, { params })
.then((res) => {
this.characters = res.data.results;
this.pages = res.data.info.pages;
console.log(res.data.info);
})
.catch(console.log);
},
async fetchOne(id) {
const result = await axios.get(
`https://rickandmortyapi.com/api/character/${id}/`,
);
this.currentCharacter = result.data;
console.log(this.currentCharacter);
},
},
};
</script>
从“axios”导入axios;
导出默认值{
名称:“FavoriteButton”,
数据(){
返回{
字符:[],
页码:1,
页码:1,
当前字符:{},
};
},
创建(){
this.fetch();
},
方法:{
addCharacter(){
this.store.commit('addCharacter',this.newCharacter);
this.newCharacter='';
},
fetch(){
常量参数={
page:this.page,
};
常量url=https://rickandmortyapi.com/api/character';
返回轴
.get(url,{params})
。然后((res)=>{
this.characters=res.data.results;
this.pages=res.data.info.pages;
console.log(res.data.info);
})
.catch(console.log);
},
异步fetchOne(id){
const result=等待axios.get(
`https://rickandmortyapi.com/api/character/${id}/`,
);
this.currentCharacter=result.data;
console.log(此.currentCharacter);
},
},
};
我认为这不是正确的方法,但我找不到解决问题的办法
谢谢你的帮助。点击事件在哪里?@BoussadjraBrahim在它是buton之前它是一个输入,这就是为什么有@keypress.enter你有一个输入,你想转换成一个按钮,然后使用
click
事件和addCharacter
处理程序来查找IDv model=“newCharacter”这个数据属性,即newCharacter,在哪里