Skip to content

Commit

Permalink
将错误打印在控制台并输出到document
Browse files Browse the repository at this point in the history
  • Loading branch information
hiiwayne committed May 28, 2022
1 parent 8293ad5 commit 090fe94
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
19 changes: 13 additions & 6 deletions react/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
import React, { lazy, StrictMode, Suspense } from 'react'
import ReactDOM from 'react-dom/client'

const ErrorApp = () => {
return <div>未找到入口文件,请检查文件是否存在,且是 default 导出</div>
const ErrorApp = (props) => {
return (
<pre>
<code>{props.message}</code>
</pre>
)
}

const App = lazy(() =>
import(/* @vite-ignore */ __MAIN__).catch(() => ({
default: ErrorApp,
}))
import(/* @vite-ignore */ __MAIN__).catch((e) => {
console.error(e)
return {
default: () => <ErrorApp message={e.message} />,
}
})
)

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<StrictMode>
<Suspense fallback={<ErrorApp />}>
<Suspense fallback={null}>
<App />
</Suspense>
</StrictMode>
Expand Down
28 changes: 16 additions & 12 deletions vue/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { createApp, h } from 'vue'

const ErrorApp = {
default: {
template: '<div>未找到入口文件,请检查文件是否存在,且是 default 导出</div>',
}
}

import(/* @vite-ignore */ __MAIN__).catch(() => ErrorApp).then(App => {
createApp({
render() {
return h(App.default)
import(/* @vite-ignore */ __MAIN__)
.then((module) => module.default)
.catch((e) => {
console.error(e)
return {
render() {
return h('pre', [h('code', e.message)])
},
}
}).mount('#root')
})
})
.then((App) => {
createApp({
render() {
return h(App)
},
}).mount('#root')
})

0 comments on commit 090fe94

Please sign in to comment.