VueJS中如果组件位于v-if/v-show内部,则该组件不会显示。

huangapple go评论62阅读模式
英文:

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 &lt;template&gt;&lt;/template&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-info&quot; @click=&quot;showPatientForm&quot;&gt;Add a patient&lt;/button&gt;
...
&lt;div class=&quot;d-flex flex-column w-75 contentOrder&quot;&gt;
    &lt;div class=&quot;&quot; v-if=&quot;showArticle&quot; ref=&quot;patientOk&quot;&gt; &lt;!-- if the patient exists, show the articles --&gt;
        &lt;ArticleCmp/&gt;
    &lt;/div&gt;
    &lt;div class=&quot;hidden&quot; ref=&quot;patientNotOk&quot; v-else&gt; &lt;!-- Else show a form to create a patient --&gt;
        &lt;PatientForm :addPatient=&quot;addPatient&quot;/&gt;
    &lt;/div&gt;
&lt;/div&gt;

// between &lt;script&gt;&lt;/script&gt;, export default(){ methods:... }

showPatientForm(){
        try{
                const createPatient = this.$refs.patientNotOk // Find the &lt;div&gt; of the form
                createPatient.classList.remove(&#39;hidden&#39;) // 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 :

&lt;script setup&gt;
import { ref } from &quot;vue&quot;;
const patientNotOk = ref();
const patientOk = ref();
const showPatientForm = () =&gt; {
  try {
    const createPatient = patientNotOk.value; // Find the &lt;div&gt; of the form
    createPatient.classList.remove(&quot;hidden&quot;); // Get rid of the hidden class
  } catch (err) {
    console.error(err);
  }
};
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年5月10日 20:14:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定