使用从父级传递的 prop 对象初始化数据属性对象
Initialize data attribute object with prop object passed from parent
我试图从父组件传递一个对象作为道具,然后用该值初始化子组件。
这样做的目的是打开一个对话框,其中包含一个子组件,该子组件是一个具有多个页面的表单。填写其中一个页面后,更改将发送给父级,并出现对话框的下一页。如果用户想要导航到上一个对话框屏幕,则需要使用已更新的父级值对其进行初始化。
/* PARENT */
<template>
<CompanyInfo :editedClient="editedClient" />
</template>
<script>
import CompanyInfo from './CompanyInfo.vue'
export default {
name: 'Client',
components: {
'CompanyInfo': CompanyInfo
},
data: () => ({
editedClient: {
name: 'aaaa',
website: '',
locations: [],
contacts: [
{
firstName: '',
lastName: '',
emails: [{ email: '' }],
phoneNumbers: [{ phoneNumber: ''}],
notes: []
}
]
},
})
}
</script>
/* CHILD */
<template></template>
<script>
export default {
name: 'CompanyInfo',
data: () => {
props: [ 'editedClient' ],
companyInfo: {
name: this.editedClient.name, //this throws an error when directly initialized like this
website: this.editedClient.email, //the error says that 'this' is undefined
locations: this.editedClient.locations //tried initializing this with mounted() hook
}
},
beforeCreate() {
console.log(this.editedClient.name) // this prints undefined to the console
}
}
</script>
您可以为此使用 vue 中的计算 属性。并且props是收到外数据属性
/* CHILD */
<template></template>
<script>
export default {
name: 'CompanyInfo',
props: {
editedClient: {
type: Object,
default: () => {
name: '',
website: '',
locations: ''
}
}
},
computed: {
companyInfo: {
get() {
return {
name: this.editedClient.name,
website: this.editedClient.email,
locations: this.editedClient.locations
}
}
}
},
data: () => {
},
mounted() {
console.log(this.companyInfo.name)
}
}
</script>
我试图从父组件传递一个对象作为道具,然后用该值初始化子组件。
这样做的目的是打开一个对话框,其中包含一个子组件,该子组件是一个具有多个页面的表单。填写其中一个页面后,更改将发送给父级,并出现对话框的下一页。如果用户想要导航到上一个对话框屏幕,则需要使用已更新的父级值对其进行初始化。
/* PARENT */
<template>
<CompanyInfo :editedClient="editedClient" />
</template>
<script>
import CompanyInfo from './CompanyInfo.vue'
export default {
name: 'Client',
components: {
'CompanyInfo': CompanyInfo
},
data: () => ({
editedClient: {
name: 'aaaa',
website: '',
locations: [],
contacts: [
{
firstName: '',
lastName: '',
emails: [{ email: '' }],
phoneNumbers: [{ phoneNumber: ''}],
notes: []
}
]
},
})
}
</script>
/* CHILD */
<template></template>
<script>
export default {
name: 'CompanyInfo',
data: () => {
props: [ 'editedClient' ],
companyInfo: {
name: this.editedClient.name, //this throws an error when directly initialized like this
website: this.editedClient.email, //the error says that 'this' is undefined
locations: this.editedClient.locations //tried initializing this with mounted() hook
}
},
beforeCreate() {
console.log(this.editedClient.name) // this prints undefined to the console
}
}
</script>
您可以为此使用 vue 中的计算 属性。并且props是收到外数据属性
/* CHILD */
<template></template>
<script>
export default {
name: 'CompanyInfo',
props: {
editedClient: {
type: Object,
default: () => {
name: '',
website: '',
locations: ''
}
}
},
computed: {
companyInfo: {
get() {
return {
name: this.editedClient.name,
website: this.editedClient.email,
locations: this.editedClient.locations
}
}
}
},
data: () => {
},
mounted() {
console.log(this.companyInfo.name)
}
}
</script>