Skip to content

Commit 0a7dca7

Browse files
committed
fix: 修复 editor 加载失败的问题
1 parent ad51b5e commit 0a7dca7

File tree

6 files changed

+79
-9
lines changed

6 files changed

+79
-9
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
78
"serve": "vite",
89
"serve:dev": "vite --mode development",
@@ -27,6 +28,7 @@
2728
"sass": "^1.63.6",
2829
"vite": "^4.4.3",
2930
"vite-plugin-compression": "^0.5.1",
31+
"vite-plugin-static-copy": "^1.0.1",
3032
"vite-plugin-vue2": "^2.0.3",
3133
"vue-template-compiler": "^2.7.14"
3234
}

src/components/AMISRenderer.vue

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
1-
<template><div></div></template>
1+
<template><div>Loading...</div></template>
22

33
<script>
4-
import "amis/sdk/sdk.js";
5-
import "amis/sdk/sdk.css";
6-
import "amis/sdk/iconfont.css";
4+
// import "amis/sdk/sdk.js";
5+
// import "amis/sdk/sdk.css";
6+
// import "amis/sdk/iconfont.css";
77
88
// 可以不引用, 如果你不想要任何辅助类样式的话 (比如 `m-t-xs` 这种)
99
// https://aisuda.bce.baidu.com/amis/zh-CN/style/index
1010
import "amis/sdk/helper.css";
1111
import qs from "qs";
1212
13+
function loadScript(src, callback) {
14+
const script = document.createElement("script");
15+
script.setAttribute("type", "text/javascript");
16+
script.setAttribute("src", src);
17+
script.onload = () => callback();
18+
script.onerror = () => callback(new Error(`Failed to load ${src}`));
19+
document.body.appendChild(script);
20+
}
21+
22+
function loadStyles(styles) {
23+
for (const path of styles) {
24+
const style = document.createElement("link");
25+
style.setAttribute("rel", "stylesheet");
26+
style.setAttribute("type", "text/css");
27+
style.setAttribute("href", path);
28+
document.head.appendChild(style);
29+
}
30+
}
31+
32+
function loadSDK() {
33+
return new Promise((resolve, reject) => {
34+
if (window.amisRequire) {
35+
resolve();
36+
return;
37+
}
38+
loadStyles([
39+
"/amis/sdk/sdk.css",
40+
"/amis/sdk/helper.css",
41+
"/amis/sdk/iconfont.css",
42+
]);
43+
loadScript("/amis/sdk/sdk.js", (err) => {
44+
if (err) {
45+
reject(err);
46+
return;
47+
}
48+
resolve();
49+
});
50+
});
51+
}
52+
1353
export default {
1454
name: "AMISRenderer",
1555
components: {},
@@ -52,7 +92,9 @@ export default {
5292
search: `?${qs.stringify(current.query)}`,
5393
};
5494
},
95+
loading: false,
5596
amisInstance: null,
97+
unmounted: false,
5698
};
5799
},
58100
@@ -67,7 +109,17 @@ export default {
67109
this.updateProps();
68110
},
69111
},
70-
mounted() {
112+
async mounted() {
113+
try {
114+
this.loading = true;
115+
await loadSDK();
116+
} finally {
117+
this.loading = false;
118+
}
119+
if (this.unmounted) {
120+
return;
121+
}
122+
71123
const scoped = amisRequire("amis/embed");
72124
const { normalizeLink } = amisRequire("amis");
73125
const router = this.$router;
@@ -146,7 +198,8 @@ export default {
146198
},
147199
148200
destroyed() {
201+
this.unmounted = true;
149202
this.amisInstance?.unmount();
150203
},
151204
};
152-
</script>
205+
</script>

src/index.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
html,
22
body,
33
#app,
4-
.wrapper {
4+
.app-wrapper {
55
margin: 0;
66
padding: 0;
77
width: 100%;
88
height: 100%;
99
position: relative;
1010
}
1111

12-
.wrapper {
12+
.app-wrapper {
1313
display: flex;
1414
}
1515

src/views/Form.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const schema = {
2424
placeholder: "请输入邮箱地址",
2525
name: "email",
2626
},
27+
{
28+
type: 'editor',
29+
name: 'js',
30+
label: 'JS',
31+
language: 'javascript',
32+
}
2733
],
2834
},
2935
],

src/views/Layout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="wrapper">
2+
<div class="app-wrapper">
33
<el-container>
44
<el-container>
55
<el-aside :class="{ collapse: sidebarCollapse }" id="main-aside">

vite.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import legacy from "@vitejs/plugin-legacy";
33
import { createVuePlugin } from "vite-plugin-vue2";
44
import viteCompression from "vite-plugin-compression";
55
import path from "path";
6+
import { viteStaticCopy } from 'vite-plugin-static-copy';
67

78
const HOST = "0.0.0.0";
89
const REPLACEMENT = `${path.resolve(__dirname, "./src")}/`;
@@ -28,6 +29,14 @@ export default (/** if you want to use mode : { mode }*/) => {
2829
],
2930
},
3031
plugins: [
32+
viteStaticCopy({
33+
targets: [
34+
{
35+
src: path.resolve(__dirname, './node_modules/amis/sdk') + '/[!.]*',
36+
dest: 'amis/sdk'
37+
}
38+
]
39+
}),
3140
createVuePlugin(/* options */),
3241
legacy({
3342
targets: ["ie >= 11"],

0 commit comments

Comments
 (0)