我有一个名为BusinessDetails
. 我在另一个组件中使用这个组件,并且我已经将ref
属性附加到它以访问它的属性和方法。如果我不给出ref
a 类型,则在访问它的方法时会出错"Object is possibly null"
。我尝试将类型设置为BusinessComponent
、typeof BusinessComponent
、Vue.extend(BusinessComponent)
等,但它们都会引发不同的错误。我最终决定将类型设置为any
,并接受eslint
警告。虽然这可以解决问题,但是否有合适的方法来做到这一点?
<q-layout view="hHh LpR lfr">
<q-header>
<q-toolbar>
<q-btn flat round @click="$router.go(-1)" icon="arrow_back" aria-label="Back"/>
<q-toolbar-title>Create Account</q-toolbar-title>
</q-toolbar>
</q-header>
<q-page-container>
<q-page>
<business-details ref="businessDetails" />
</q-page>
</q-page-container>
</q-layout>
export default Vue.extend({
name: "CreateAccount",
components: {
BusinessDetails
},
setup () {
const businessDetails: Ref<any> = ref(null);
const isCurrentPageValid = (page: number): Promise<boolean> => {
switch (page) {
case AccountGroup.BUSINESS:
return businessDetails.value.isValid();
default:
return Promise.resolve(true);
}
};
return {
businessDetails,
isCurrentPageValid
};
}
});