重新加载窗口

This commit is contained in:
2021-06-13 14:10:11 +08:00
parent 16a94b7c5a
commit 1db0ce888b
3 changed files with 74 additions and 31 deletions

View File

@@ -4,29 +4,42 @@ import { cloneDeep } from 'lodash'
export default class ComponentDynamic extends Component { export default class ComponentDynamic extends Component {
state = { state = {
key: null,
component: null component: null
} }
async componentDidMount() { shouldComponentUpdate() {
let c; if (this.props.onRef) {
this.props.onRef(this)
}
return true
}
componentDidMount() {
this.loadComponent()
}
async loadComponent() {
let component;
try { try {
if (this.props.is) { if (this.props.is) {
if (this.props.is.constructor === Function) { if (this.props.is.constructor === Function) {
// 导入函数 // 导入函数
c = await this.props.is() component = await this.props.is()
} else { } else {
// 导入路径,必须是src以下节点,如 pages/home // 导入路径,必须是src以下节点,如 pages/home
c = await import(`../../${this.props.is}`) component = await import(`../../${this.props.is}`)
} }
} }
} } catch {
catch { component = await import(`views/error/404`)
c = await import(`views/error/404`)
} }
this.setState({ this.setState({
component: c.default key: Math.random().toString(16).slice(2),
component: component.default
}) })
} }
@@ -36,10 +49,9 @@ export default class ComponentDynamic extends Component {
delete props.is delete props.is
return (<> if (this.state.component) {
{ return <this.state.component key={this.state.key} {...props} />
this.state.component && <this.state.component {...props} /> }
} return <></>
</>)
} }
} }

View File

@@ -9,26 +9,40 @@ NProgress.configure({ parent: '.ant-layout-content > .yo-tab-external-mount > .y
class ComponentDynamic extends Component { class ComponentDynamic extends Component {
state = { state = {
// 组件内部组件的key,用于刷新
key: null,
component: null component: null
} }
componentDidMount() { shouldComponentUpdate() {
if (this.props.onRef) {
this.props.onRef(this)
}
return true
}
componentDidMount() {
this.loadComponent()
}
loadComponent() {
NProgress.start() NProgress.start()
// 在这里使用setTimeout调用,是防止打开窗口时卡顿
setTimeout(async () => { setTimeout(async () => {
let c; let component;
try { try {
c = await import(`../../../../pages${this.props.path}`) component = await import(`../../../../pages${this.props.path}`)
} } catch {
catch { component = await import('views/error/404')
c = await import(`views/error/404`)
} }
this.setState({ this.setState({
component: c.default key: Math.random().toString(16).slice(2),
component: component.default
}, () => { }, () => {
NProgress.done() NProgress.done()
}) })
@@ -37,11 +51,10 @@ class ComponentDynamic extends Component {
} }
render() { render() {
return (<> if (this.state.component) {
{ return <this.state.component key={this.state.key} {...this.props} />
this.state.component && <this.state.component {...this.props} /> }
} return <></>
</>)
} }
} }
@@ -51,25 +64,42 @@ export default class index extends Component {
actived: '' actived: ''
} }
panes = []
componentDidMount() {
window.realodContentWindow = this.onReload
}
static getDerivedStateFromProps(props) { static getDerivedStateFromProps(props) {
return { return {
actived: props.actived actived: props.actived
} }
} }
onChange = (activeKey) => { onChange(activeKey) {
this.props.parent.setState({ this.props.parent.setState({
actived: activeKey actived: activeKey
}) })
} }
onClose = (targetKey, action) => { onClose(targetKey, action) {
if (action === 'remove') { if (action === 'remove') {
window.closeContentWindow(targetKey) window.closeContentWindow(targetKey)
} }
} }
onReload = (key) => {
key = key || this.state.actived
const pane = this.panes.find(p => p.props.id === key)
if (pane) {
pane.loadComponent()
}
}
render() { render() {
this.panes = []
return ( return (
<Layout.Content> <Layout.Content>
<div className="yo-tab-external-mount"> <div className="yo-tab-external-mount">
@@ -77,8 +107,8 @@ export default class index extends Component {
type="editable-card" type="editable-card"
hideAdd hideAdd
activeKey={this.state.actived} activeKey={this.state.actived}
onChange={this.onChange} onChange={(activeKey) => this.onChange(activeKey)}
onEdit={this.onClose} onEdit={(targetKey, action) => this.onClose(targetKey, action)}
> >
{ {
this.props.panes.map(pane => { this.props.panes.map(pane => {
@@ -91,7 +121,7 @@ export default class index extends Component {
trigger={['contextMenu']} trigger={['contextMenu']}
overlay={ overlay={
<Menu> <Menu>
<Menu.Item key="0">重新加载</Menu.Item> <Menu.Item key="0" onClick={() => this.onReload(pane.key)}>重新加载</Menu.Item>
<Menu.Divider /> <Menu.Divider />
<Menu.Item key="1" onClick={() => window.closeContentWindow(pane.key)}>关闭</Menu.Item> <Menu.Item key="1" onClick={() => window.closeContentWindow(pane.key)}>关闭</Menu.Item>
<Menu.Item key="2" onClick={() => window.closeOtherContentWindow(pane.key)}>关闭其他标签页</Menu.Item> <Menu.Item key="2" onClick={() => window.closeOtherContentWindow(pane.key)}>关闭其他标签页</Menu.Item>
@@ -125,6 +155,7 @@ export default class index extends Component {
id={pane.key} id={pane.key}
key={pane.key} key={pane.key}
param={pane.param} param={pane.param}
onRef={p => this.panes.push(p)}
/> />
</div> </div>
) )

View File

@@ -17,7 +17,7 @@ export default class index extends Component {
{/* search */} {/* search */}
</div> </div>
<div className="header-actions"> <div className="header-actions">
<span className="header-action"> <span className="header-action" onClick={() => window.realodContentWindow()}>
<AntIcon type="reload" /> <AntIcon type="reload" />
</span> </span>
<span className="header-action"> <span className="header-action">