簡體   English   中英

在Angular 2中使用TinyMCE組件時出錯

[英]Error using TinyMCE component in Angular 2

我是Agular 2的新手,我正在嘗試在項目中使用TinyMCE編輯器。

我遵循以下說明來創建和使用tinyMCE組件: https ://www.tinymce.com/docs/integrations/angular2/

完成上述步驟后,我無法使TinyMCE正常工作,出現以下錯誤:

錯誤錯誤:未被捕獲(承諾):錯誤:模板解析錯誤:無法綁定到'elementId',因為它不是'simple-tiny'的已知屬性。

  1. 如果'simple-tiny'是Angular組件,並且具有'elementId'輸入,則請驗證它是否是此模塊的一部分。
  2. 如果“ simple-tiny”是Web組件,則將“ CUSTOM_ELEMENTS_SCHEMA”添加到該組件的“ @ NgModule.schemas”以禁止顯示此消息。
  3. 要允許任何屬性,請在此組件的“ @ NgModule.schemas”中添加“ NO_ERRORS_SCHEMA”。 (”

這是我的代碼:

微小-editor.component.ts

import { Component, OnChanges, AfterViewInit, EventEmitter, OnDestroy, 
Input, Output } from '@angular/core';
import 'tinymce';
import 'tinymce/themes/modern';

declare var tinymce: any;

@Component({
  selector: 'simple-tiny',
  template: `<textarea id="{{elementId}}"></textarea>`
})
export class TinyEditorComponent implements AfterViewInit, OnDestroy {
  @Input() elementId: string;
  @Output() onEditorContentChange = new EventEmitter<any>();

  editor;

  constructor() { }

  ngAfterViewInit() {
    tinymce.init({
      selector: `#${this.elementId}`,
      skin_url: '../assets/skins/lightgray',
      plugins: ['link', 'paste', 'table'],
      setup: editor => {
        this.editor = editor;
        editor.on('keyup change', () => {
          const content = editor.getContent();
          this.onEditorContentChange.emit(content);
        });
      }
    });
  }

  ngOnDestroy() {
    tinymce.remove(this.editor);
  }

}

parent.component.html

<simple-tiny [elementId]="'descripcion'" (onEditorContentChange)="keyupHandler($event)"></simple-tiny>

我已經將組件導入到app.module.ts中
我已經將腳本添加到angular-cli.json中

我正在使用TinyMCE版本4.5.6

我想念什么?

這是我的app.module.ts。 我正在使用模板。 app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';

import { AppComponent } from './app.component';
import { BsDropdownModule } from 'ng2-bootstrap/dropdown';
import { TabsModule } from 'ng2-bootstrap/tabs';
import { NAV_DROPDOWN_DIRECTIVES } from './shared/nav-dropdown.directive';

import { ChartsModule } from 'ng2-charts/ng2-charts';
import { SIDEBAR_TOGGLE_DIRECTIVES } from './shared/sidebar.directive';
import { AsideToggleDirective } from './shared/aside.directive';
import { BreadcrumbsComponent } from './shared/breadcrumb.component';

// Routing Module
import { AppRoutingModule } from './app.routing';

//Layouts
import { FullLayoutComponent } from './layouts/full-layout.component';
import { SimpleLayoutComponent } from './layouts/simple-layout.component';

//Modules
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule, RequestOptions } from '@angular/http';
import { AuthModule } from './auth/auth.module';

//Services
import { AuthGuard } from './auth/auth.guard';
import { AuthService } from './auth/auth.service';
import { PropertiesService } from './services/properties.service';
import { TypesService } from './services/types.service';

import { TinyEditorComponent } from './tiny-editor/tiny-editor.component';

@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule,
    BsDropdownModule.forRoot(),
    TabsModule.forRoot(),
    ChartsModule,
    AuthModule,
    FormsModule,
    ReactiveFormsModule,
    HttpModule
  ],
  declarations: [
    AppComponent,
    FullLayoutComponent,
    SimpleLayoutComponent,
    NAV_DROPDOWN_DIRECTIVES,
    BreadcrumbsComponent,
    SIDEBAR_TOGGLE_DIRECTIVES,
    AsideToggleDirective,
    TinyEditorComponent
  ],
  providers: [
    {
    provide: LocationStrategy,
    useClass: HashLocationStrategy
    },
    AuthService,
    AuthGuard,
    PropertiesService,
    TypesService
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

不確定建造者的問題,可能是角度傾斜嗎?

elementId表示要包裝到編輯器的HTML元素,並且在Directive中作為@Input參數接收。 嘗試僅使用:

selector: '#' + this.elementId,

這將是

  ngAfterViewInit() {
    tinymce.init({
      selector: '#' + this.elementId,
      skin_url: '../assets/skins/lightgray',
      plugins: ['link', 'paste', 'table'],
      setup: editor => {
        this.editor = editor;
        editor.on('keyup change', () => {
          const content = editor.getContent();
          this.onEditorContentChange.emit(content);
        });
      }
    });
  }

如果仍然無法使用,請嘗試使用默認的選擇標識符,即

select: '#wysiwygEditor',

並在HTML中

<simple-tiny id="wysiwygEditor" (onEditorContentChange)="keyupHandler($event)"></simple-tiny>

希望這可以幫助。 干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM