Element UI 是为 Vue 2 设计的,而 Element Plus 是 Element UI 的 Vue 3 版本。在这两个版本中,Transfer 穿梭框组件(如果有的话)的 API 和使用方式可能会有所不同。以下是根据我对这两个版本组件的了解,对 Vue 2 的 Element UI 和 Vue 3 的 Element Plus 中的 Transfer 穿梭框组件的属性、事件和方法的区分介绍。
Vue 2 + Element UI
在 Element UI 中,Transfer 穿梭框组件允许用户通过两列列表来移动选项,通常用于分类选择或数据筛选。
属性 (Attributes)
- data: 穿梭框的数据源,格式为数组,每个元素是一个对象,至少包含- key、- label、- disabled等字段
- titles: 列表标题的数组,长度应为 2
- filterable: 是否可搜索
- filter-method: 自定义搜索方法
- props: 配置选项,用于指定- data数组中每个元素的哪个字段作为选项的 key、label、disabled 等
- target-keys: 选中项对应的- key数组
- render-content: 自定义内容渲染函数
- ...: 其他通用属性
事件 (Events)
- change: 选中项发生变化时触发
- select: 选中项发生变化时触发(在 Element UI 2.13.0+ 版本中提供)
- select-all: 当点击全选按钮时触发(在 Element UI 2.13.0+ 版本中提供)
- remove: 移除项时触发(在 Element UI 2.13.0+ 版本中提供)
- ...: 其他通用事件
方法 (Methods)
- Element UI 的 Transfer 组件通常不提供直接调用的方法,而是通过属性和事件来控制其行为。
示例
            
            
              vue
              
              
            
          
          <template>
  <el-transfer
    v-model="targetKeys"
    :data="data"
    :titles="['Source', 'Target']"
    @change="handleChange"
  ></el-transfer>
</template>
<script>
export default {
  data() {
    return {
      targetKeys: [],
      data: [
        { key: '1', label: 'Option 1' },
        { key: '2', label: 'Option 2' },
        // ...
      ],
    };
  },
  methods: {
    handleChange(value, direction, movedKeys) {
      console.log(value, direction, movedKeys);
    },
  },
};
</script>Vue 3 + Element Plus
在 Vue 3 的 Element Plus 中,Transfer 穿梭框组件的 API 可能会有所不同,但基本概念和用法应该相似。
属性 (Attributes)
- 与 Element UI 中的属性类似,但可能有一些新增或变更的属性。请查阅 Element Plus 官方文档以获取最新信息。
事件 (Events)
- 与 Element UI 中的事件类似,但也可能有新增的事件。
方法 (Methods)
- Element Plus 的 Transfer 组件可能提供了更多直接调用的方法,但这需要查看具体文档来确定。
示例
由于 Element Plus 的 API 可能会随时间变化,以下是一个假设的示例,基于 Vue 3 和 Composition API 的写法:
            
            
              vue
              
              
            
          
          <template>
  <el-transfer
    v-model="targetKeys"
    :data="data"
    :titles="['Source', 'Target']"
    @change="handleChange"
  ></el-transfer>
</template>
<script>
import { ref } from 'vue';
export default {
  setup() {
    const targetKeys = ref([]);
    const data = ref([
      { key: '1', label: 'Option 1' },
      { key: '2', label: 'Option 2' },
      // ...
    ]);
    const handleChange = (value, direction, movedKeys) => {
      console.log(value, direction, movedKeys);
    };
    return {
      targetKeys,
      data,
      handleChange,
    };
  },
};
</script>请注意,由于 Element Plus 是基于 Vue 3 的,因此在示例中使用了 Vue 3 的 Composition API。同时,由于 Element Plus 的 API 可能会更新,请务必查阅官方文档以获取最新信息。