// 模拟componentDidMount
useEffect(() => {
editor = new E('#div1')
editor.config.onchange = (newHtml) => {
setContent(newHtml)
}
editor.create()
// 根据地址栏id做请求
if (params.id) {
ArticleSearchApi({ id: params.id }).then(res => {
if (res.errCode === 0) {
editor.txt.html(res.data.content) // 重新设置编辑器内容
setTitle(res.data.title)
setSubTitle(res.data.subTitle)
}
})
}
return () => {
// 组件销毁时销毁编辑器 注:class写法需要在componentWillUnmount中调用
editor.destroy()
}
}, [location.pathname])