Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image: add support for transmit "attrs" and "listeners" #15578

Merged
merged 8 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/image/src/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<img
v-else
class="el-image__inner"
v-bind="$attrs"
v-on="$listeners"
Copy link
Contributor

@SimonaliaChen SimonaliaChen May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里会造成load事件触发两次,可以把组件加载图片时的load事件手动emit的逻辑移除了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新,烦请review

:src="src"
:alt="alt"
:style="imageStyle"
:class="{ 'el-image__inner--center': alignCenter }">
</div>
Expand Down Expand Up @@ -41,8 +42,7 @@
src: String,
fit: String,
lazy: Boolean,
scrollContainer: {},
alt: String
scrollContainer: {}
},

data() {
Expand Down Expand Up @@ -102,6 +102,14 @@
const img = new Image();
img.onload = e => this.handleLoad(e, img);
img.onerror = this.handleError.bind(this);

// bind html attrs
// so it can behave consistently
Object.keys(this.$attrs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

模板中不是已经绑定了$attrs嘛 为什么还需要手动设置呢

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为在应用时,有些attr会影响请求资源的行为、结果,比如 referrerpolicycrossorigin 属性,举个简单的例子:

<img src="test.jpg" referrerpolicy="origin" />

对应的请求头:

Referer: http://localhost:8085/
<img src="test.jpg" referrerpolicy="no-referrer" />

对应的请求头中没有 Referer 字段。

原来的 loadImage 函数中没有针对这点这处理,会导致 loadImage 发出的请求与 img 标签发出的请求不一致。

.forEach((key) => {
const value = this.$attrs[key];
img.setAttribute(key, value);
});
img.src = this.src;
},
handleLoad(e, img) {
Expand Down
47 changes: 47 additions & 0 deletions test/unit/specs/image.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,52 @@ describe('Image', () => {
await wait();
expect(image2.loading).to.be.false;
});

it('$attrs', async() => {
vm = createVue({
template: `
<el-image
alt="$attrs test"
referrerpolicy="origin"
:src="src"></el-image>
`,
data() {
return {
src
};
}
}, true);

await wait();
expect(vm.$el.getAttribute('alt')).to.be.equal('$attrs test');
expect(vm.$el.getAttribute('referrerpolicy')).to.be.equal('origin');
});

it('pass event listeners', (done) => {
let result;
vm = createVue({
template: `
<el-image @click="handleClick" :src="src"></el-image>
`,
data() {
return {
src
};
},
methods: {
handleClick(e) {
result = 'test';
}
}
}, true);

setTimeout(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用 async 函数。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

vm.$el.querySelector('.el-image__inner').click();
setTimeout(()=>{
expect(result).to.exist;
done();
}, 20);
}, 20);
});
});