2 回答

TA贡献1794条经验 获得超8个赞
默认情况下是道具,null但您可以给它们提供默认值来克服此问题。
例子 :
Vue.component('result', {
props: {
stuff: {
type: Object,
default: {
type: ''
}
}
},
data: () => ({}),
template: "<img :src='tag' class='result'></img>",
computed: {
tag: function tag() {
return `pages/search/img/${this.stuff.type.toLowerCase()}_tag.png`;
}
}
});

TA贡献1813条经验 获得超2个赞
所述stuff的支柱时未定义result被创建部件。
有两种方法可以解决此问题:
使用v-if父组件模板中的指令来确保stuff在result创建组件时具有一个值:
<template>
<result v-if="stuff" :stuff="stuff" />
</template>
或处理stuff道具是undefined在result组件。
Vue.component('result', {
props: {
// Object with a default value
stuff: {
type: Object,
// Object or array defaults must be returned from
// a factory function
default: () => ({ type: 'someType'})
},
},
data: () => ({}),
template: "<img :src='tag' class='result' >",
computed: {
tag: function tag() {
return `pages/search/img/${this.stuff.type.toLowerCase()}_tag.png`;
}
}
})
注意:img元素是一个void元素,它不需要结束标签。
添加回答
举报