如何在父元素的帮助下找到子元素?

How to find child element with the help of parent's ref?

我需要使用 vue 的 this.$refs 访问项目,但不确定如何实现。我不想为此使用 jQuery。

<template>
  <div>
    <svg ref="svgItem">
      <path d="M899.33,118.33h-81.7v90.2h-31.3V2.07h31.3v88.5h81.7V2.07h31.29V208.53H899.33Z" />
    </svg>
  </div>
</template>

<script>
import { TimelineLite } from 'gsap';

export default {
  mounted() {
    const { svgItem } = this.$refs;
    const timeline = new TimelineLite();
    timeline.to(svgItem, 5, { opacity: 0.3 });
    // timeline.to('${svgItem} > path', 5, { opacity: 0.3 }); // something like this :)
  },
};
</script>

我知道如何使用 jQuery:

const svgPath = $('#parent path');
// or may be with:
const svgPath = $('#parent').find('path');

我觉得我可能会得到 querySelector 但不确定如何混合和匹配变量和字符串:

const { svgItem } = this.$refs;
const selector = document.querySelector(svgItem > 'path')  // something like this :)

const timeline = new TimelineLite();
timeline.to(selector, 5, { opacity: 0.3 }

谢谢。

方法querySelector也在HTMLElement上。它不仅在 document.

上可用
const { svgItem } = this.$refs;
const svgPath = svgItem.querySelector('path');