<!-- 小贴士: 🎉 恭喜你成功解决了挑战,很高兴看到你愿意分享你的答案! 由于用户数量的增加,Issue 池可能会很快被答案填满。为了保证 Issue 讨论的效率,在提交 Issue 前,请利用搜索查看是否有其他人分享过类似的档案。 你可以为其点赞,或者在 Issue 下追加你的想法和评论。如果您认为自己有不同的解法,欢迎新开 Issue 进行讨论并分享你的解题思路! 谢谢! --> ```vue // 你的答案 ``` <script setup lang='ts'> import { h, ref } from "vue" /** * Implement a functional component : * 1. Render the list elements (ul/li) with the list data * 2. Change the list item text color to red when clicked. */ const ListComponent = (p, { emit }) => { return h( 'ul', {}, p.list.map((x, i) => { return h('li', { style: { color: (p['active-index'] == i) ? 'red' : null }, onClick: () => { emit('toggle', i) } }, x.name) }) ) } const list = [{ name: "John", }, { name: "Doe", }, { name: "Smith", }] const activeIndex = ref(0) function toggle(index: number) { activeIndex.value = index } </script> <template> <list-component :list="list" :active-index="activeIndex" @toggle="toggle" /> </template> <!-- 或者 Vue SFC Playground 在线链接 (https://sfc.vuejs.org) -->