debug日记:模板渲染和父向子传值时机

51 阅读1分钟

在渲染父向子传值,传递userConfig对象,对象结构如下:

userConfig:{
    xxx:{
        multiMode: true
    },
}

如果直接渲染

<span v-if=" userConfig[type].hasOwnProperty('multiMode')">

会报如下错误:

"TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')"

这是因为模板渲染时候,父向子传值不一定成功,因此改为:

<span v-if="userConfig[type] && userConfig[type].hasOwnProperty('multiMode')"">

即可