引用 el-date-picker 内部元素

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

referencing el-date-picker inner elements

问题

<ElDatePicker ref="datePickerRef" />

mounted() {
  console.log(this.$refs.datePickerRef.$el.querySelector('.el-input__inner'));
}

如何将这段代码转换为Vue 3的选项API?已尝试过ChatGPT建议的方法,但都没有成功。

英文:
 &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;


 mounted() {
  console.log(this.$refs.datePickerRef.$el.querySelector(&#39;.el-input__inner&#39;);
 }

How do I convert this to vue3 options api? Tried ways suggested by chatgpt already, but nothing worked

答案1

得分: 1

你是指在Composition API中吧,因为你已经在Options API中了,这里假设你正在使用TypeScript:

    &lt;script setup lang=&quot;ts&quot;&gt;
      import { onMounted, ref } from &#39;vue&#39;;
      import { ElDatePicker } from &#39;./你的/组件路径/component.vue&#39;;

      const datePickerRef = ref&lt;typeof ElDatePicker | null&gt;(null);

      onMounted(() =&gt; {
        if (datePickerRef.value) {
            console.log(datePickerRef.value.$el.querySelector(&#39;.el-input__inner&#39;));
        }
      });
    &lt;/script&gt;

    &lt;template&gt;
      &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;
    &lt;/template&gt;

如果你没有使用TypeScript:

    &lt;script setup&gt;
      import { onMounted, ref } from &#39;vue&#39;;

      const datePickerRef = ref(null);

      onMounted(() =&gt; {
        if (datePickerRef.value) {
            console.log(datePickerRef.value.$el.querySelector(&#39;.el-input__inner&#39;));
        }
      });
    &lt;/script&gt;

    &lt;template&gt;
      &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;
    &lt;/template&gt;
英文:

You mean in composition API I suppose, because you are already in options API, here it is supposing you are on typescript :

&lt;script setup lang=&quot;ts&quot;&gt;
  import { onMounted, ref } from &#39;vue&#39;;
  import { ElDatePicker } from &#39;./your/path/to/component.vue&#39;;

  const datePickerRef = ref&lt;typeof ElDatePicker | null&gt;(null);

  onMounted(() =&gt; {
    if (datePickerRef.value) {
        console.log(datePickerRef.value.$el.querySelector(&#39;.el-input__inner&#39;));
    }
  });
&lt;/script&gt;

&lt;template&gt;
  &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;
&lt;/template&gt;

if you are not in typescript:

&lt;script setup&gt;
  import { onMounted, ref } from &#39;vue&#39;;

  const datePickerRef = ref(null);

  onMounted(() =&gt; {
    if (datePickerRef.value) {
        console.log(datePickerRef.value.$el.querySelector(&#39;.el-input__inner&#39;));
    }
  });
&lt;/script&gt;

&lt;template&gt;
  &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;
&lt;/template&gt;

答案2

得分: 0

    &lt;script setup&gt;
    import { ref, onMounted } from &quot;vue&quot;;

    const myReference = ref(null);

    onMounted(() =&gt; {
      console.log(myReference.value); &lt;-- 这将输出一个 DOM 节点
    })
    &lt;/script&gt;

    &lt;template&gt;
      &lt;h1 ref=&quot;myReference&quot;&gt;我现在可以访问 DOM 引用了&lt;/h1&gt;
    &lt;/template&gt;
英文:
&lt;script setup&gt;
import { ref, onMounted } from &quot;vue&quot;;

const myReference = ref(null);

onMounted(() =&gt; {
  console.log(myReference.value); &lt;-- This will output a DOM node
})
&lt;/script&gt;

&lt;template&gt;
  &lt;h1 ref=&quot;myReference&quot;&gt;I have access to the DOM reference now&lt;/h1&gt;
&lt;/template&gt;

Here is an example of using $refs in Vue 3 with Composition API and Script Setup
view more

huangapple
  • 本文由 发表于 2023年4月4日 16:28:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927138.html
匿名

发表评论

匿名网友

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

确定