【问题标题】:Import Javascript scripts into Angular将 Javascript 脚本导入 Angular
【发布时间】:2020-06-11 13:24:28
【问题描述】:

我正在尝试在我的 Angular 项目中使用 TinyMCE-Elfinder 集成器 (https://github.com/nao-pon/tinymceElfinder),我的 TinyMCE 胶囊组件已经在其中工作。

问题是,为了让集成器工作,我需要导入一些 JS 脚本。我尝试在与我的 TinyMCE 编辑器相同的 html 文件中定义它们,如下所示:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.min.css"/>
<!-- elFinder (load latest nightly) -->
<script type="text/javascript" src="https://studio-42.github.io/elFinder/demo/js/elfinder.min.js"></script>
<!-- tinyMCE -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.2.0/tinymce.min.js"></script>
<!-- tinymceElfinder -->
<script type="text/javascript" src="https://nao-pon.github.io/tinymceElfinder/tinymceElfinder.js"></script>

<editor
[(ngModel)]="content"
[init]="{
    base_url: '/tinymce',
    suffix: '.min',
    height: 500,
    menubar: false,
    plugins: [
        'advlist autolink lists link image charmap print preview anchor',
        'searchreplace visualblocks code fullscreen',
        'insertdatetime media table paste code help wordcount'
    ],
    toolbar:
        'undo redo | formatselect | bold italic backcolor link image media | \
        alignleft aligncenter alignright alignjustify | \
        bullist numlist outdent indent | removeformat | help | mkdir',

        file_picker_callback : mceElf.browser,
        images_upload_handler: mceElf.uploadHandler

    }"
(onKeyUp)="onKeyUp($event)"
></editor>

然后,从代码中可以看出,为编辑器定义了一个 file_picker_callback 和一个 image_upload_handler。

在此之后,我按照库说明声明了用于将 file_picker_callback 和 image_upload_handler (mceElf) 传递给我的 TS 文件的对象。所述代码如下所示:

...

declare const tinymceElfinder : any;

...

@Component({

})
export class RichEditorComponent {

    mceElf = new tinymceElfinder({
        url: 'php/connector.minimal.php',
        uploadTargetHash: 'l1_lw',
        nodeId: 'elfinder'
    });

    ...

}

但是,当我尝试运行我的应用时,我收到以下错误:

ERROR Error: "Uncaught (in promise): ReferenceError: tinymceElfinder is not defined

我的猜测是 Angular 忽略了 HTML 导入。我对 Angular 还很陌生,所以我真的不知道如何正确处理这个问题。有人有解决办法吗?

【问题讨论】:

标签: angular tinymce integration elfinder


【解决方案1】:

Angular 不接受将 JS 脚本写入 html 模板。尝试将所有 JS 脚本移动到 Angular 项目中的 index.html 文件中, 在head标签的末尾

关于如何将外部 JS 包含到 Angular 组件中的更多信息,你可以看到这篇文章:Angular 2: import external js file into component

【讨论】:

  • 我已经集成了 TinyMCE,我按照这些说明进行了操作。我现在要做的是将 ElFinder 集成到 TinyMCE 中
  • 包含脚本的 html 文件的名称是什么? (顶部的第一个)
  • 富编辑器.component.html。第二个是rich-editor.component.ts。我将它们用作 tinyMCE.editor 的胶囊
  • 这是错误,我将编辑我的答案以解决您的问题:)
  • 好吧,您的解决方案似乎“有点”奏效。现在我不再得到那个错误,但现在我得到了这个:未处理的承诺拒绝:elfNode.elfinder is not a function;区域: ;任务:Promise.then;值:TypeError:“elfNode.elfinder 不是函数”。我一直在谷歌搜索,显然这发生在 elfinder.min.js 尚未加载时。但是,我以与其他脚本相同的方式导入它,这就是您在此处提供的方式。我不太确定为什么有些脚本可以加载,而其他脚本不能加载
猜你喜欢
  • 2021-09-22
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 2020-05-07
  • 1970-01-01
  • 2021-07-14
相关资源
最近更新 更多