iT邦幫忙

0

使用async function 載入CKeditor

小弟使用async function 來非同步載入ckeditor.js 但是出現了以下錯誤
Uncaught (in promise) TypeError: Cannot read property 'document' of undefined
想請問這是甚麼意思 該如何解決呢?
以下是程式碼

<script>
		async function start_select(default_switch){
			await import("./ckeditor/ckeditor.js");
		}
		start_select()
	</script>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1

不能這樣去應用 ckeditor 。
你參考一下官方文件處理

https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/frameworks/overview.html

豬豬人 iT邦新手 4 級 ‧ 2020-11-25 11:56:43 檢舉

謝謝星大,但我是用ckeditor4,好像找不到相關應用...

https://ckeditor.com/

下方就有針對 ckeditor 4做說明了啊

https://ckeditor.com/docs/ckeditor4/latest/guide/dev_installation.html

這是4的說明。裏面也有對應的載入。
基本最好還是用npm安裝。會比較容易應用。

0
亞歷斯
iT邦新手 5 級 ‧ 2020-11-26 21:15:38

看起來你使用的是本地檔案,如果需要延後載入本地端的檔案,並不需要使用 async function,你可以建立<script/> 元素插入檔案:

function loadCkeditor() {
    var scriptEl = document.createElement('script');
    scriptEl.src = './ckeditor/ckeditor.js';

    document.body.appendChild(scriptEl);
}

loadCkeditor();

另外,如果專案許可,使用套件管理工具(npm, yarn)會是更好的選擇。

我要發表回答

立即登入回答