ImagePicker图片选择器
注意:只是图片选择器,一般用于上传图片前的文件选择操作,但不包括图片上传的功能
代码演示
简单的图片选择组件
import { Component } from '@angular/core';
const data = [
{
url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg'
},
{
url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
}
];
@Component({
selector: 'demo-image-picker-basic',
template: `
<div class="ip-segment-wrapper">
<div class="ip-segment-btn" [ngClass]="{ selected: multipleTab === 0 }" (click)="changeMultiple(0)">
切换到单选
</div>
<div class="ip-segment-btn" [ngClass]="{ selected: multipleTab === 1 }" (click)="changeMultiple(1)">
切换到多选
</div>
</div>
<ImagePicker
[files]="files"
[multiple]="multipleTab === 1"
[selectable]="files.length < 5"
(onChange)="fileChange($event)"
(onImageClick)="imageClick($event)"
></ImagePicker>
`,
styles: [
`
.ip-segment-wrapper {
display: flex;
border-radius: 5px;
overflow: hidden;
min-height: 27px;
opacity: 1;
}
.ip-segment-btn {
display: flex;
flex: 1;
justify-content: center;
align-items: center;
color: #108ee9;
font-size: 14px;
line-height: 1;
-webkit-transition: background 0.2s;
transition: background 0.2s;
position: relative;
border: 1px solid #108ee9;
width: 100%;
box-sizing: border-box;
border-left-width: 0;
}
.ip-segment-btn.selected {
background: #108ee9;
color: #fff;
}
.ip-segment-btn:nth-child(1) {
border-left-width: 1px;
border-radius: 5px 0 0 5px;
}
.ip-segment-btn:nth-child(2) {
border-left-width: 1px;
border-radius: 0 5px 5px 0;
}
`
]
})
export class DemoImagePickerBasicComponent {
files = data.slice(0);
multiple = false;
multipleTab = 0;
changeMultiple(value: number) {
this.multipleTab = value;
}
fileChange(params) {
console.log(params);
const { files, type, index } = params;
this.files = files;
}
imageClick(params) {
console.log(params);
}
}
自定义选择图片的方法
import { Component } from '@angular/core';
const data = [
{
url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg'
},
{
url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
}
];
@Component({
selector: 'demo-image-picker-custom',
template: `
<ImagePicker
[files]="files"
[multiple]="multiple"
[selectable]="files.length < 5"
(onChange)="fileChange($event)"
(onImageClick)="imageClick($event)"
(onAddImageClick)="addImageClick($event)"
></ImagePicker>
`
})
export class DemoImagePickerCustomComponent {
files = data.slice(0);
multiple = false;
fileChange(params) {
const { files, type, index } = params;
this.files = files;
}
addImageClick(e) {
e.preventDefault();
this.files = this.files.concat({
url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
});
}
imageClick(params) {
console.log(params);
}
}
自定义文件类型
import { Component } from '@angular/core';
const data = [
{
url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg'
},
{
url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
}
];
@Component({
selector: 'demo-image-picker-accept',
template: `
<ImagePicker
[files]="files"
[multiple]="multiple"
[selectable]="files.length < 5"
[accept]="'image/gif,image/jpeg,image/jpg,image/png'"
(onChange)="fileChange($event)"
(onImageClick)="imageClick($event)"
(onImageChange)="ImageChange($event)"
></ImagePicker>
`
})
export class DemoImagePickerAcceptComponent {
files = data.slice(0);
multiple = false;
multipleTab = 0;
ImageChange(params) {
const { files, type, index } = params;
this.files = files;
}
fileChange(event) {
console.log(event);
}
imageClick(params) {
console.log(params);
}
}
自定义数量
import { Component } from '@angular/core';
const data = [
{
url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg'
},
{
url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
}
];
@Component({
selector: 'demo-image-picker-length',
template: `
<ImagePicker
[files]="files"
[length]="6"
[selectable]="files.length < 7"
(onImageClick)="imageClick($event)"
(onImageChange)="imageChange($event)"
(onAddImageClick)="addImageClick($event)"
></ImagePicker>
`
})
export class DemoImagePickerLengthComponent {
files = data.slice(0);
multiple = false;
imageChange(params) {
const { files, type, index } = params;
this.files = files;
}
addImageClick(e) {
e.preventDefault();
this.files = this.files.concat({
url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
});
}
imageClick(params) {
console.log(params);
}
}
API
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
[files] | 图片文件数组,元素为对象,包含属性 url(必选, 可能还有id, orientation, 以及业务需要的其它属性 | Array | [] |
[selectable] | 是否显示添加按钮 | boolean | true |
[multiple] | 是否支持多选 | boolean | false |
[accept] | 图片类型 | string | ‘image/*’ |
[length] | 单行的图片数量 | number | 4 |
[capture] | 图片捕获设置, 具体请参考MDN中关于capture的说明 | boolean | string | false |
[disableDelete] | 是否隐藏删除按钮 | boolean | false |
(onChange) | files 值发生变化触发的回调函数, operationType 操作类型有添加,移除,如果是移除操作,则第三个参数代表的是移除图片的索引 | EventEmitter<{files: object, operationType: string, index: number}> | - |
(onImageClick) | 点击图片触发的回调 | EventEmitter<{index: number, files: object}> | - |
(onAddImageClick) | 点击添加图片时触发的回调方法 | EventEmitter<void> | - |
(onFail) | 选择失败 | EventEmitter<string> | - |