next.js - Nextjs:在卸载组件时删除元素
问题描述
知道如何在 componentwillunmount 上的 nextjs 中从 dom 中删除一个元素吗?我正在使用 Head 组件加载在页面上生成元素的 JS 脚本。当我导航到另一个页面时,该元素仍然存在,因此我需要将其删除。我想过使用componentwillunmount和document.getelementbyclassname,但由于某种原因它在nextjs中不起作用。
关于如何解决它的任何想法?
这不会做任何事情,即使按钮被拾起:
componentDidMount() {
oldButton2 = document.getElementsByClassName("button");
}
componentWillUnmount() {
if (oldButton2.parentNode) {
oldButton2.parentNode.removeChild(oldButton2);
}
}
解决方案
推荐阅读
- angular - 为大纲更改 mat-icon 中的主题
- visual-studio-code - Tab Stop 在 VS Code 中的片段选项中
- inno-setup - Inno Setup打包的文件中检测到木马
- c - 在 UDP 非阻塞客户端/服务器对中生成的随机 EWOULDBLOCKs 与 SIGIO 生成的 recvfroms
- python-3.x - Pandas DF 中的截断行
- cassandra - 如何在 cassandra 中将数据从一列移动到同一表中具有不同数据类型的另一列
- slack - 只有主要所有者可以在 Slack 上发布消息
- c# - 如何从类库中引用 nuget 包的子集?
- javascript - Express - 使用 express.static 时路由不起作用
- c++ - unix读/写如何绑定异步io完成功能进行串口编程?