【发布时间】:2017-12-28 17:47:27
【问题描述】:
简单地说,我有两个组件:
- 父组件,它传递一个名为“profile”的 prop 对象
- 子组件接收配置文件属性
配置文件值是这样的对象:
{
name: "Something",
email: "some@thing.com"
}
会发生什么?
子组件完美接收模板中的配置文件值,但似乎无法检索并将其设置为组件数据。
目标是什么?
我想用配置文件电子邮件属性初始化值“电子邮件”。
我期待什么?
export default {
props: ["profile"],
data() {
return {
email: this.profile.email
}
}
}
更新
我没有指定电子邮件是用作模型的数据值。 我刚刚尝试删除它并简单地在模板中打印电子邮件的值,它也不起作用。
<!-- PARENT COMPONENT -->
<template>
<dialog-settings ref="dialogSettings" :profile="profile"></dialog-settings>
</template>
<script>
import Auth from "../services/apis/auth";
import DialogSettings from "../components/dialog-settings";
export default {
name: "app",
components: {
"dialog-settings": DialogSettings
},
beforeCreate() {
Auth.checkToken()
.then(profile => {
this.profile = profile;
})
.catch(err => {
});
},
data() {
return {
title: "App",
drawer: true,
profile: {},
navItems: []
};
}
}
</script>
<!-- CHILD COMPONENT -->
<template>
{{profile}} <!-- All the fields are passed and available (e.g. profile.email)-->
{{email}} <!-- Email is not defined -->
</template>
<script>
import Auth from "../services/apis/auth";
import DialogSettings from "../components/dialog-settings";
export default {
name: "dialog-settings",
props: ["profile"],
data() {
return {
email: this.profile.email
}
}
}
</script>
更新 2
我已经尝试了几件事,我认为问题在于 beforeCreate() 中对 API 的异步调用。
【问题讨论】:
-
这必须工作。我在您的代码中看不到任何错误。发布您的父子组件的完整代码。
标签: javascript vuejs2