【发布时间】:2019-12-26 18:38:56
【问题描述】:
我有一个组件调用我的后端 API。然后,这为我提供了用于组件的数据。我现在想创建另一个也使用该数据的组件。虽然我可以再做一个看起来很浪费的 api 调用。
所以,在 Profile.vue 中,我在 created() 函数中有这个。
<script>
import axios from 'axios';
import { bus } from '../main';
export default {
name: 'Profile',
data() {
return {
loading: false,
error: null,
profileData: null,
getImageUrl: function(id) {
return `http://ddragon.leagueoflegends.com/cdn/9.16.1/img/profileicon/` + id + `.png`;
}
}
},
beforeCreate() {
//Add OR Remove classes and images etc..
},
async created() {
//Once page is loaded do this
this.loading = true;
try {
const response = await axios.get(`/api/profile/${this.$route.params.platform}/${this.$route.params.name}`);
this.profileData = response.data;
this.loading = false;
bus.$emit('profileData', this.profileData)
} catch (error) {
this.loading = false;
this.error = error.response.data.message;
}
}
};
</script>
然后我有另一个使用 Vue 路由器连接的子组件,这是为了显示更多信息。
MatchHistory 组件
<template>
<section>
<h1>{{profileDatas.profileDatas}}</h1>
</section>
</template>
<script>
import { bus } from '../main';
export default {
name: 'MatchHistory',
data() {
return {
profileDatas: null
}
},
beforeCreate() {
//Add OR Remove classes and images etc..
},
async created() {
bus.$on('profileData', obj => {
this.profileDatas = obj;
});
}
};
</script>
所以,我想获取信息并显示我传输的数据。
【问题讨论】:
标签: vue.js components vue-component vue-router