Angular2里Ckeditor销毁的解决方式

一、问题

Angular2中,在使用ckeitor4.x编辑器的页面中,切换到其他页面,其他页面会报警告,警告内容如下: [CKEDITOR] Error code: editor-destroy-iframe. ckeditor.js:21 [CKEDITOR] For more information about this error go to docs.ckeditor.com/#!/guide/de… 根据链接文档解释如下:

翻译过来就是,需要我们在ckeditor从dom中卸载前先把ckeditor销毁。 看了angular1中的使用方式是在进入页面时先判断有没有ckeditor的实例,如果有就先销毁再创建新的,和这里是一个意思。只是这过程中我该如何在angular2中去实现这个销毁的过程。 尝试了一些方法都没有成功。没辙,只能在github.com上查看ng2-ckeditor这个项目里的issue里是否有这个问题。果然有。参考了下,改变两处引入方式,一个是index.html里ckeditor的js地址,二是页面中使用ckeditor组件的位置,config信息里添加插件需求。

二、解决步骤

  1. 在应用的index.html页面中将引入的js地址修改为:

2.使用位置修改 <ckeditor [(ngModel)]="templateData.content" [config]="{extraPlugins: 'divarea'}"> 增加[config]="{extraPlugins: 'divarea'}", 这里需要使用插件divarea

三、总结

问题是解决了,但原理是什么还需要再看看。不过到是体会了遇到解决不了的问题,应该回到社区,回到项目根源。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular中,可以使用路由模块来进行页面跳转,并且在页面跳转后销毁当前页面。下面是一个简单的例子: 1. 首先,在组件中,需要订阅路由事件,以便在路由导航发生时销毁当前组件。例如: ``` import { Component, OnInit, OnDestroy } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; import { filter, takeUntil } from 'rxjs/operators'; import { Subject } from 'rxjs'; @Component({ selector: 'app-home', template: ` <button (click)="goToAbout()">Go to About</button> ` }) export class HomeComponent implements OnInit, OnDestroy { private unsubscribe$ = new Subject<void>(); constructor(private router: Router) { } ngOnInit() { this.router.events.pipe( filter(event => event instanceof NavigationEnd), takeUntil(this.unsubscribe$) ).subscribe(() => { this.unsubscribe$.next(); this.unsubscribe$.complete(); }); } ngOnDestroy() { this.unsubscribe$.next(); this.unsubscribe$.complete(); } goToAbout() { this.router.navigate(['/about']); } } ``` 在这个例子中,首先定义了一个名为unsubscribe$的Subject对象,用于在销毁组件时取消订阅路由事件。然后在ngOnInit()方法中,订阅路由事件,并在路由导航完成后销毁当前组件,同时取消订阅路由事件。在goToAbout()方法中,使用router.navigate()方法来导航到/about路径。 2. 在路由模块中,需要配置路由守卫,以便在路由导航发生时销毁当前组件。例如: ``` import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { HomeComponent } from './home.component'; import { AboutComponent } from './about.component'; const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent, canActivate: [DestroyGuard] } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } ``` 在这个例子中,路由模块中配置了一个DestroyGuard守卫,用于在路由导航发生时销毁当前组件。可以在守卫中实现CanActivate接口,并在canActivate()方法中销毁当前组件。例如: ``` import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; @Injectable({ providedIn: 'root' }) export class DestroyGuard implements CanActivate { constructor(private router: Router) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { setTimeout(() => { this.router.getCurrentNavigation().previousNavigation?.extractedUrl.segment.shift(); }); return true; } } ``` 在这个例子中,首先在canActivate()方法中,使用setTimeout()方法来延迟执行一段时间。然后使用this.router.getCurrentNavigation().previousNavigation?.extractedUrl.segment.shift()方法来将前一个导航的路径中的第一个片段(即当前组件的路径)从导航历史记录中删除。这样,当页面跳转完成后,就会自动销毁当前组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值