英文:
Component isn't display by VueJS if it is inside a v-if/v-show
问题
I understand that you want a translation of the provided code and error message. Here's the translation:
我正在尝试在点击“Add Patient”按钮时显示一个组件,使用了 v-if:
// 在 <template> 中
<button type="button" class="btn btn-info" @click="showPatientForm">添加患者</button>
...
<div class="d-flex flex-column w-75 contentOrder">
<div v-if="showArticle" ref="patientOk"> <!-- 如果患者存在,显示文章 -->
<ArticleCmp/>
</div>
<div class="hidden" ref="patientNotOk" v-else> <!-- 否则显示创建患者的表单 -->
<PatientForm :addPatient="addPatient"/>
</div>
</div>
// 在 <script> 中,export default(){ methods:... }
showPatientForm(){
try{
const createPatient = this.$refs.patientNotOk // 找到表单的 <div>
createPatient.classList.remove('hidden') // 移除隐藏类
}
catch(err){
console.error(err)
}
},
我知道这不是一种清晰的解决方法,但如果我将 v-else 更改为 v-if,当我显示患者表单,然后点击现有患者时,患者表单不会消失。
但问题是,如果我想点击现有患者,然后创建一个客户端,this.$refs.PatientNotOk 被视为 null。这是因为 VueJS(Vite 3)在开始时不渲染 v-else,因为它不是 true。那么,如何使其渲染 v-else?
错误日志:
TypeError: createPatient is null
showPatientForm CreateOrderView.vue:127
2 CreateOrderView.vue:33
callWithErrorHandling runtime-core.esm-bundler.js:173
callWithAsyncErrorHandling runtime-core.esm-bundler.js:182
invoker runtime-dom.esm-bundler.js:345
CreateOrderView.vue:131:25
showPatientForm CreateOrderView.vue:131
2 CreateOrderView.vue:33
callWithErrorHandling runtime-core.esm-bundler.js:173
callWithAsyncErrorHandling runtime-core.esm-bundler.js:182
invoker runtime-dom.esm-bundler.js:345
(第131行是 catch 中的 console.error,第127行是 createPatient.classList.remove,第33行是模板中的按钮 "Add a patient")
英文:
I'm trying to display a component when I hit a button 'Add Patient', by using a v-if :
// between <template></template>
<button type="button" class="btn btn-info" @click="showPatientForm">Add a patient</button>
...
<div class="d-flex flex-column w-75 contentOrder">
<div class="" v-if="showArticle" ref="patientOk"> <!-- if the patient exists, show the articles -->
<ArticleCmp/>
</div>
<div class="hidden" ref="patientNotOk" v-else> <!-- Else show a form to create a patient -->
<PatientForm :addPatient="addPatient"/>
</div>
</div>
// between <script></script>, export default(){ methods:... }
showPatientForm(){
try{
const createPatient = this.$refs.patientNotOk // Find the <div> of the form
createPatient.classList.remove('hidden') // Get rid of the hidden class
}
catch(err){
console.error(err)
}
},
I know it's not a clean way to make it works, but if I make the v-else to a v-if, when I show the patient form, and then click on an existing patient, the patient form doesn't disappears.
But the problem is, that if I want to click on an existing patient and then create a client, this.$refs.PatientNotOk is considered null. That is because VueJS (Vite 3) doesn't render at the beginning the v-else, as it is not true. So How can I make it render the v-else ?
The error log :
TypeError: createPatient is null
showPatientForm CreateOrderView.vue:127
2 CreateOrderView.vue:33
callWithErrorHandling runtime-core.esm-bundler.js:173
callWithAsyncErrorHandling runtime-core.esm-bundler.js:182
invoker runtime-dom.esm-bundler.js:345
CreateOrderView.vue:131:25
showPatientForm CreateOrderView.vue:131
2 CreateOrderView.vue:33
callWithErrorHandling runtime-core.esm-bundler.js:173
callWithAsyncErrorHandling runtime-core.esm-bundler.js:182
invoker runtime-dom.esm-bundler.js:345
(line 131 is the console.error of the catch, line 127 is the createPatient.classList.remove and line 33 is the button "Add a patient" in the template)
答案1
得分: 1
When using ref, you need to add variables ref with the same names.
const patientNotOk = ref();
const patientOk = ref();
Here is a working script setup:
<script setup>
import { ref } from "vue";
const patientNotOk = ref();
const patientOk = ref();
const showPatientForm = () => {
try {
const createPatient = patientNotOk.value; // Find the <div> of the form
createPatient.classList.remove("hidden"); // Get rid of the hidden class
} catch (err) {
console.error(err);
}
};
</script>
英文:
When using ref, you need to add variables ref with the same names.
const patientNotOk = ref();
const patientOk = ref();
Here is a working script setup :
<script setup>
import { ref } from "vue";
const patientNotOk = ref();
const patientOk = ref();
const showPatientForm = () => {
try {
const createPatient = patientNotOk.value; // Find the <div> of the form
createPatient.classList.remove("hidden"); // Get rid of the hidden class
} catch (err) {
console.error(err);
}
};
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论