admin管理员组文章数量:1394184
I'm trying to extend the prototype of the HTMLElement object in main.ts
, so that I can use it throughout my whole Angular 6 project.
But I get the Property 'fadeOut' does not exist on type 'HTMLElement'
.
HTMLElement.prototype.fadeOut = function(duration: number = 300): Promise<void> {
const keyframes: AnimationKeyFrame[] = [{ opacity: 1 }, { opacity: 0 }];
return new Promise(resolve => {
const animation: Animation = this.animate(keyframes, duration);
animation.onfinish = () => resolve();
});
};
const circle = document.getElementById('circle');
HTMLElement.prototype.fadeOut = function(duration = 300) {
const keyframes = [{ opacity: 1 }, { opacity: 0 }];
return this.animate(keyframes, duration).finished
};
circle.fadeOut(2000);
#circle {
height: 100px;
width: 100px;
margin: 50px;
border-radius: 50%;
background-color: #0f477f;
}
<div id="circle"></>
I'm trying to extend the prototype of the HTMLElement object in main.ts
, so that I can use it throughout my whole Angular 6 project.
But I get the Property 'fadeOut' does not exist on type 'HTMLElement'
.
HTMLElement.prototype.fadeOut = function(duration: number = 300): Promise<void> {
const keyframes: AnimationKeyFrame[] = [{ opacity: 1 }, { opacity: 0 }];
return new Promise(resolve => {
const animation: Animation = this.animate(keyframes, duration);
animation.onfinish = () => resolve();
});
};
const circle = document.getElementById('circle');
HTMLElement.prototype.fadeOut = function(duration = 300) {
const keyframes = [{ opacity: 1 }, { opacity: 0 }];
return this.animate(keyframes, duration).finished
};
circle.fadeOut(2000);
#circle {
height: 100px;
width: 100px;
margin: 50px;
border-radius: 50%;
background-color: #0f477f;
}
<div id="circle"></>
What am I doing wrong?
Where do I need to put this code so that can I make this method available everywhere?
Do you also see a possibility to make this code cleaner?
Share Improve this question edited Aug 3, 2018 at 21:04 Kode Bryant asked Aug 3, 2018 at 20:44 Kode BryantKode Bryant 5119 silver badges24 bronze badges 8- Maybe HTMLElement.prototype['fadeOut'] works. – Christian Benseler Commented Aug 3, 2018 at 20:46
- If that worked then what the OP tried would work too. – Pointy Commented Aug 3, 2018 at 20:49
- What exactly does the code that attempts to use the method look like? – Pointy Commented Aug 3, 2018 at 20:50
-
Once you get the prototype thing figured out, I think your function can just be:
function(duration: number = 300) { return this.animate([{opacity:1},{opacity:0}], duration).finished; }
– user21926 Commented Aug 3, 2018 at 20:52 - I've just added a snippet, so the code works. @SeanBright You're right! Gotta dive deeper into this amazing API. Thanks! – Kode Bryant Commented Aug 3, 2018 at 20:59
1 Answer
Reset to default 9You need to add a definition to be merged with the original interface in which you define the extra function to be added to HTMLElement
interface HTMLElement {
fadeOut(duration: number): Promise<void>
}
// Will now work
HTMLElement.prototype.fadeOut = function (duration: number = 300): Promise<void> {
const keyframes: AnimationKeyFrame[] = [{ opacity: 1 }, { opacity: 0 }];
return new Promise(resolve => {
const animation: Animation = this.animate(keyframes, duration);
animation.onfinish = () => resolve();
});
};
If the code is in a module you need to declare the interface in the global namespace
declare global {
interface HTMLElement {
fadeOut(duration: number): Promise<void>
}
}
HTMLElement.prototype.fadeOut = function (duration: number = 300): Promise<void> {
const keyframes: AnimationKeyFrame[] = [{ opacity: 1 }, { opacity: 0 }];
return new Promise(resolve => {
const animation: Animation = this.animate(keyframes, duration);
animation.onfinish = () => resolve();
});
};
export var x;
本文标签: javascriptExtend HTMLElement prototypeStack Overflow
版权声明:本文标题:javascript - Extend HTMLElement prototype - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744749476a2623086.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论