V-for and slot-scoped报错问题

2 篇文章 0 订阅
1 篇文章 0 订阅

此场景是为了用v-for动态渲染表格的slot

可能会这么写

<a-table>
    <span v-for="(item, index) in header" :key="index" :slot="item.dataIndex" slot-scope="text"  >
          {{ text }}
    </span>
</a-table>

但是这样子会报错,因为v-for和slot-scope在同一级

Ambiguous combined usage of slot-scope and v-for on (v-for takes higher priority). Use a wrapper < template> for the scoped slot to make it clearer.

提示在外边包一层< template>,于是你可能改成下面这样,但是也会报错

<a-table>
    <template v-for="(item, index) in header" :key="index">
        <span :slot="item.dataIndex" slot-scope="text"  >
             {{ text }}
        </span>
    </template>
</a-table>

< template> cannot be keyed. Place the key on real elements instead.

提示< template>template不能写key, 即使没有这个错,表格数据也不会渲染出来,因为这一层没有slot,应该说slot应该是放最外面,同时把:key放里面

改成如下

<a-table>
    <template v-for="(item, index) in header" :slot="item.dataIndex" slot-scope="text">
        <span :key="index">
             {{ text }}
        </span>
    </template>
</a-table>

以上解决问题

有个slot没有渲染的问题

         <template v-for="(slotname, idx) in ['status', 'sub_account_status']" :slot="slotname" slot-scope="text" >
            <span :key="idx">
            <a-tag v-if="text === '正常'" color="blue">{{ text }}</a-tag>
            <a-tag v-else color="red">{{ text }}</a-tag>
          </span>
        </template>
        <!-- 包名称、关联账号 -->
        <template v-for="(slotname, idx) in ['app_name', 'roles']" :slot="slotname" slot-scope="text" >
          <span :key="idx">
            <a-tooltip placement="top" >
              <template slot="title">
                <span v-for="(item, index) in text" :key="index">{{ item }}<br/></span>
              </template>
              <div class="tableHidden">
                <a-tag v-for="(item, index) in text" :key="index">{{ item }} </a-tag>
              </div>
            </a-tooltip>
          </span>
        </template>

好像是因为2个v-for="(slotname, idx)"里的slotname名字一样了,对的,就是取的临时变量名,修改成不一样的就好了,神奇

        <template v-for="(name, idx) in ['status', 'sub_account_status']" :slot="name" slot-scope="text" >
            // 上面那个name
          <span :key="idx">
            。。。
          </span>
        </template>
        <!-- 包名称、关联账号 -->
        <template v-for="(slotname, idx) in ['app_name', 'roles']" :slot="slotname" slot-scope="text" >
          <span :key="idx">
            。。。
            </a-tooltip>
          </span>
        </template>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值