今天做项目的时候,遇见了一个奇怪的现象,先写一下所使用的东西
vue 项目,使用ant design vue 中的table组件
之前的代码
<template>
<a-table :columns="columns" :dataSource="data" bordered>
<template
v-for="field in ['name', 'address']"
:slot="field"
slot-scope="text"
>
<a>{{ text }}</a>
</template>
<template
v-for="field in ['phone', 'age']"
:slot="field"
slot-scope="text"
>
<span style="color: blue">{{ text }}</span>
</template>
<span slot="tags" slot-scope="tags">
<a-tag
v-for="tag in tags"
:color="tag==='loser' ? 'volcano' : (tag.length > 5 ? 'geekblue' : 'green')"
:key="tag"
>
{{tag.toUpperCase()}}
</a-tag>
</span>
<span slot="action" slot-scope="text, record">
<a href="javascript:;">Invite 一 {{record.name}}</a>
<a-divider type="vertical" />
<a href="javascript:;">Delete</a>
<a-divider type="vertical" />
<a href="javascript:;" class="ant-dropdown-link"> More actions <a-icon type="down" /> </a>
</span>
</a-table>
</template>
<script>
const columns = [
{
title: 'Name'
dataIndex: 'name',
scopedSlots: { customRender: 'name' }
},
{
title: 'Age',
dataIndex: 'age',
scopedSlots: { customRender: 'age' }
},
{
title: 'Address',
dataIndex: 'address',
scopedSlots: { customRender: 'age' }
},
{
title: 'Tags',
dataIndex: 'tags',
scopedSlots: { customRender: 'tags' },
},
{
title: 'Action',
key: 'action',
scopedSlots: { customRender: 'action' },
},
];
const data = [
{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
phone: 13333333333,
tags: ['nice', 'developer'],
},
{
key: '2',
name: 'Jim Green',
age: 42,
phone: 13333333333,
address: 'London No. 1 Lake Park',
tags: ['loser'],
},
{
key: '3',
name: 'Joe Black',
age: 32,
phone: 13333333333,
address: 'Sidney No. 1 Lake Park',
tags: ['cool', 'teacher'],
},
];
export default {
data() {
return {
data,
columns,
};
},
};
</script>
好像是 v-for=“field” 中的field使用了相同的,修改成不一样的就能正常渲染了
改过之后的代码
<template
v-for="field in ['name', 'address']"
:slot="field"
slot-scope="text"
>
<a>{{ text }}</a>
</template>
<template
v-for="col in ['phone', 'age']"
:slot="col"
slot-scope="text"
>
<span style="color: blue">{{ text }}</span>
</template>
不知是v-for的原因还是slot的原因,如果有知道的小伙伴,可以评论出来