typescript - Vue 3 PropType 看不到界面
问题描述
我使用 onMonted 钩子创建父组件 Product vue:
setup () {
let data = reactive({
product: {},
productInfo: {},
loaded: false,
shippingDate: ''
})
onMounted(async () => {
const product = await findProduct()
data.productInfo = productInfo(product)
data.product = product
data.loaded = true
})
return { data }
},
和子组件 ProductAttributes
props: {
product: {
type: Object as PropType<Product>,
required: true
}
},
setup (props) {
const configurableOptions = ref([])
const configurableChildren = ref([])
onMounted(() => {
configurableChildren.value = props.product.configurable_children
//@ts-ignore
configurableOptions.value = props.product.configurable_options.map((option: ConfigurableOptions) => {
option.values = option.values.sort((a: ConfigurableOptionsValue, b: ConfigurableOptionsValue) => {
return parseInt(a.value_index) - parseInt(b.value_index)
})
return option
})
// resetAttrs()
})
return {
configurableChildren,
configurableOptions
}
}
但是在这种情况下,打字稿向我显示一个错误Property 'configurable_children' does not exist on type 'unknown'.
但是如果我将设置功能道具更改为setup (props: { product: Product }) {
并将道具更改为props: ['product']
打字稿停止显示错误。
为什么会这样?为什么 typescript 不从 PropType 中提取类型?
PS我使用defineComponent
解决方案
对于Property 'x' does not exist on type 'unknown'
在这种情况下遇到错误的其他人,我可以通过将我的打字稿版本从升级4.1.5
到4.5.5
.
推荐阅读
- php - 使用过滤器(块)给出错误
- junit5 - 查明 assertAll 中的故障
- docker - 如何将 OpenSSL 版本从 OpenSSL 1.0.1t 更新到 OpenSSL 到 1.1.1u
- java - 为什么这个 parseInt 抛出数组越界异常?
- ruby - 如何通过 Ruby PostgreSQL gem 获取自动生成的行主键?
- django - Django - serve() 得到了一个意外的关键字参数“documuent_root”
- function - 如何在没有短代码 Woocommerce 的情况下将所有产品属性放在描述下方
- angularjs - Angular 6中NGPrime数据表中的多个复选框过滤
- javascript - 从 C++ 调用 Node JS javascript
- javascript - 如何循环播放嵌入的 Facebook 视频?