我如何收听对host元素的class
属性所做的更改?
There's an easy way to do it, but that might not be appropriate in all situations.
Simply add 'class' as a @Prop and a @Watch:
@Prop() class: string;
@Watch('class') handleClassChange(class: string) {
console.log(class);
}
Tracking DOM attribute changes requires the use of a MutationObserver
.
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
componentDidLoad() {
// Target element that will be observed
const target = this.el;
const config = {
attributes: true,
attributeOldValue: true,
attributeFilter: ['class']
};
function subscriberCallback(mutations) {
mutations.forEach((mutation) => {
console.log(mutation);
});
}
const observer = new MutationObserver(subscriberCallback);
observer.observe(target, config);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.