引用 el-date-picker 内部元素

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

referencing el-date-picker inner elements

问题

  1. <ElDatePicker ref="datePickerRef" />
  2. mounted() {
  3. console.log(this.$refs.datePickerRef.$el.querySelector('.el-input__inner'));
  4. }

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

英文:
  1. &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;
  2. mounted() {
  3. console.log(this.$refs.datePickerRef.$el.querySelector(&#39;.el-input__inner&#39;);
  4. }

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

答案1

得分: 1

  1. 你是指在Composition API中吧,因为你已经在Options API中了,这里假设你正在使用TypeScript
  2. &lt;script setup lang=&quot;ts&quot;&gt;
  3. import { onMounted, ref } from &#39;vue&#39;;
  4. import { ElDatePicker } from &#39;./你的/组件路径/component.vue&#39;;
  5. const datePickerRef = ref&lt;typeof ElDatePicker | null&gt;(null);
  6. onMounted(() =&gt; {
  7. if (datePickerRef.value) {
  8. console.log(datePickerRef.value.$el.querySelector(&#39;.el-input__inner&#39;));
  9. }
  10. });
  11. &lt;/script&gt;
  12. &lt;template&gt;
  13. &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;
  14. &lt;/template&gt;
  15. 如果你没有使用TypeScript
  16. &lt;script setup&gt;
  17. import { onMounted, ref } from &#39;vue&#39;;
  18. const datePickerRef = ref(null);
  19. onMounted(() =&gt; {
  20. if (datePickerRef.value) {
  21. console.log(datePickerRef.value.$el.querySelector(&#39;.el-input__inner&#39;));
  22. }
  23. });
  24. &lt;/script&gt;
  25. &lt;template&gt;
  26. &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;
  27. &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 :

  1. &lt;script setup lang=&quot;ts&quot;&gt;
  2. import { onMounted, ref } from &#39;vue&#39;;
  3. import { ElDatePicker } from &#39;./your/path/to/component.vue&#39;;
  4. const datePickerRef = ref&lt;typeof ElDatePicker | null&gt;(null);
  5. onMounted(() =&gt; {
  6. if (datePickerRef.value) {
  7. console.log(datePickerRef.value.$el.querySelector(&#39;.el-input__inner&#39;));
  8. }
  9. });
  10. &lt;/script&gt;
  11. &lt;template&gt;
  12. &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;
  13. &lt;/template&gt;

if you are not in typescript:

  1. &lt;script setup&gt;
  2. import { onMounted, ref } from &#39;vue&#39;;
  3. const datePickerRef = ref(null);
  4. onMounted(() =&gt; {
  5. if (datePickerRef.value) {
  6. console.log(datePickerRef.value.$el.querySelector(&#39;.el-input__inner&#39;));
  7. }
  8. });
  9. &lt;/script&gt;
  10. &lt;template&gt;
  11. &lt;ElDatePicker ref=&quot;datePickerRef&quot; /&gt;
  12. &lt;/template&gt;

答案2

得分: 0

  1. &lt;script setup&gt;
  2. import { ref, onMounted } from &quot;vue&quot;;
  3. const myReference = ref(null);
  4. onMounted(() =&gt; {
  5. console.log(myReference.value); &lt;-- 这将输出一个 DOM 节点
  6. })
  7. &lt;/script&gt;
  8. &lt;template&gt;
  9. &lt;h1 ref=&quot;myReference&quot;&gt;我现在可以访问 DOM 引用了&lt;/h1&gt;
  10. &lt;/template&gt;
英文:
  1. &lt;script setup&gt;
  2. import { ref, onMounted } from &quot;vue&quot;;
  3. const myReference = ref(null);
  4. onMounted(() =&gt; {
  5. console.log(myReference.value); &lt;-- This will output a DOM node
  6. })
  7. &lt;/script&gt;
  8. &lt;template&gt;
  9. &lt;h1 ref=&quot;myReference&quot;&gt;I have access to the DOM reference now&lt;/h1&gt;
  10. &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:

确定