Vue初学,内层v-for中,怎么使用外层v-for的item值

新手上路,请多包涵

内层想在v-for中,使用外层v-for的item值图片描述

会提示我错误:

  • Property or method "item" is not defined on the instance but
    referenced during render. Make sure that this property is reactive,
    either in the data option, or for class-based components, by
    initializing the property.
阅读 6.1k
4 个回答

v-for指令是形成了一个作用域的,你要访问v-for渲染的数据,那么就必须要包含在里面,以下是我的一个建议代码,你可以修改结构如下:

    <div class="area">
        <template v-for="(item,key) of cities">
            <div class="title" :key="key">{{ key }}</div>
            <div class="item-list" v-for="(innerItem,index) in item" :key="innerItem.id">
                <div class = "item">{{ item.name }}</div>
            </div>
        </template>
    </div>

vue 不准在外部访问 v-for 中的变量. v-for中的变量只是一个暂时的变量. 话说你这写的到底是为了干啥啊

使用不一样的变量名就行了, 本来就可以的

你这写的都有问题,明明是2个同级的for循环。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题