You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { Component } from 'react';
import { ApplyPluginsType } from 'umi';
import dva from 'dva';
// @ts-ignore
import createLoading from '{{{ dvaLoadingPkgPath }}}';
import { plugin, history } from '../core/umiExports';
let app:any = null;
export function _onCreate(options = {}) {
const runtimeDva = plugin.applyPlugins({
key: 'dva',
type: ApplyPluginsType.modify,
initialValue: {},
});
app = dva({
history,
{{{ ExtendDvaConfig }}}
...(runtimeDva.config || {}),
// @ts-ignore
...(typeof window !== 'undefined' && window.g_useSSR ? { initialState: window.g_initialProps } : {}),
...(options || {}),
});
{{{ EnhanceApp }}}
app.use(createLoading());
{{{ RegisterPlugins }}}
(runtimeDva.plugins || []).forEach((plugin:any) => {
app.use(plugin);
});
{{{ RegisterModels }}}
return app;
}
export function getApp() {
return app;
}
export class _DvaContainer extends Component {
constructor(props: any) {
super(props);
// run only in client, avoid override server _onCreate()
if (typeof window !== 'undefined') {
_onCreate();
}
}
componentWillUnmount() {
let app = getApp();
app._models.forEach((model:any) => {
app.unmodel(model.namespace);
});
app._models = [];
try {
// appfor gc
// immer app read-only try catch
app = null;
} catch(e) {
console.error(e);
}
}
render() {
const app = getApp();
app.router(() => this.props.children);
return app.start()();
}
}