使用View Design框架Image组件实现图片预览时遇到template无法绑定key的问题求助
Hey there, let's fix that "template cannot be keyed" error you're running into with your View Design Image component setup!
The root cause here is straightforward: Vue doesn't allow you to attach the :key attribute directly to a <template> tag. Keys need to be placed on the actual DOM elements (or components) that are being looped over. Right now, you've got your v-for and :key on the <template> wrapper, which is triggering this error.
The Corrected Code
You can eliminate the <template> wrapper entirely and apply the loop directly to the <Image> component. This works perfectly for your use case since you're only rendering one element per loop iteration:
<template> <Space wrap> <Image v-for="(url, index) in urlList" :key="url" :src="url" fit="contain" width="120px" height="80px" preview :preview-list="urlList" :initial-index="index" /> </Space> </template> <script> export default { data() { return { urlList: [ 'https://file.iviewui.com/images/image-demo-1.jpg', 'https://file.iviewui.com/images/image-demo-2.jpg', 'https://file.iviewui.com/images/image-demo-3.jpg', 'https://file.iviewui.com/images/image-demo-4.jpg', 'https://file.iviewui.com/images/image-demo-5.jpg', 'https://file.iviewui.com/images/image-demo-6.jpg' ] } } } </script>
Quick Note for Edge Cases
If you ever need to loop over multiple elements at once (and thus require a <template> wrapper), remember to place the :key on each individual element inside the <template>, not the wrapper itself. But in your scenario, the <template> is unnecessary—directly looping the <Image> component is cleaner and fixes the error.
After making this change, your image preview functionality should work as expected without any more key-related error messages.
内容的提问来源于stack exchange,提问作者Charlene

