【发布时间】:2021-07-03 20:51:52
【问题描述】:
我正在开发 vue 应用程序。我在这里面临的问题是挂载的子组件是在创建父子之前执行的。
我正在从父组件向子组件发送道具,并且我想在子组件的挂载中使用这些道具,但我没有在挂载中获取这些道具,因为它们是在父组件中创建之前执行的。请帮我解决这个问题,以便子组件将 this.userCopy 设置为 this.user,这是作为道具出现的。
父组件
<template>
<div>
<Info :user="user" />
</div>
</template>
<script>
import Info from 'views/info';
export default {
components: {
Info
},
data () {
return {
user: {
first_name: '',
last_name: '',
},
errors:[]
}
}
created() {
this.fetchUsers();
},
methods: {
fetchUsers() {
this.$axios.get('/user.json')
.then(response => {
this.user = response.data;
}).catch(error => {
this.errors = error.response.data.error;
});
},
}
}
</script>
子组件
<template>
<div>
{{userCopy}}
</div>
</template>
<script>
export default {
props: ['user'],
data () {
return {
userCopy: {}
}
}
mounted: function() {
var that = this;
this.userCopy = this.user
}
}
</script>
【问题讨论】:
标签: javascript vue.js components