-
Notifications
You must be signed in to change notification settings - Fork 14.7k
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
Changes from 3 commits
77f329a
16d62ca
aa6c651
76066d3
9153563
41e89af
60f9b92
90e077a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,9 @@ | |
<img | ||
v-else | ||
class="el-image__inner" | ||
v-bind="$attrs" | ||
v-on="$listeners" | ||
:src="src" | ||
:alt="alt" | ||
:style="imageStyle" | ||
:class="{ 'el-image__inner--center': alignCenter }"> | ||
</div> | ||
|
@@ -41,8 +42,7 @@ | |
src: String, | ||
fit: String, | ||
lazy: Boolean, | ||
scrollContainer: {}, | ||
alt: String | ||
scrollContainer: {} | ||
}, | ||
|
||
data() { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 模板中不是已经绑定了$attrs嘛 为什么还需要手动设置呢 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 因为在应用时,有些attr会影响请求资源的行为、结果,比如 <img src="test.jpg" referrerpolicy="origin" /> 对应的请求头:
<img src="test.jpg" referrerpolicy="no-referrer" /> 对应的请求头中没有 原来的 |
||
.forEach((key) => { | ||
const value = this.$attrs[key]; | ||
img.setAttribute(key, value); | ||
}); | ||
img.src = this.src; | ||
}, | ||
handleLoad(e, img) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使用 async 函数。 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里会造成
load
事件触发两次,可以把组件加载图片时的load
事件手动emit的逻辑移除了There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已更新,烦请review