Angular封裝WangEditor富文本組件的方法
富文本組件是web程序中很常用的一個組件,特別是要開發(fā)一個博客,論壇這類的網(wǎng)站后臺。
得益于Angular的強(qiáng)大,封裝WangEditor組件非常簡單
1.使用yarn或者npm安裝wangeditor
yarn add wangeditor
2.創(chuàng)建一個Angular的組件
ng g c q-wang-editor
3.封裝組件邏輯
3.1 模板
<div #wang></div>
3.2 ts邏輯
import { Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild, ViewEncapsulation } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import E from "wangeditor"
import hljs from 'highlight.js'
import "node_modules/highlight.js/styles/xcode.css"
@Component({
selector: 'q-wang-editor',
templateUrl: './q-wang-editor.component.html',
styleUrls: [
'./q-wang-editor.component.less',
'../../../../../node_modules/highlight.js/styles/xcode.css'],
encapsulation: ViewEncapsulation.None,
})
export class QWangEditorComponent implements OnInit, ControlValueAccessor,OnDestroy {
@ViewChild("wang")
editor!: ElementRef;
@Input() value: string = '';
@Input() height = 300;
@Output() valueChange = new EventEmitter();
onChange: ((value: string) => {}) | undefined;
html = ''
wangEditor: E | undefined;
constructor() { }
ngOnDestroy(): void {
this.wangEditor?.destroy();
}
writeValue(obj: any): void {
this.html = obj;
this.wangEditor?.txt.html(this.html)
}
registerOnChange(fn: any): void {
}
registerOnTouched(fn: any): void {
}
ngOnInit(): void {
setTimeout(() => {
this.wangEditor = new E(this.editor.nativeElement)
this.wangEditor.config.zIndex = 500;
this.wangEditor.config.height = this.height
this.wangEditor.highlight = hljs;
this.wangEditor.config.onchange = (html: any) => {
this.valueChange.emit(html)
if (this.onChange) {
this.onChange(html);
}
}
this.wangEditor.config.onchangeTimeout = 500;
this.wangEditor.create();
this.wangEditor.txt.html(this.html)
}, 200);
}
}
大致思路:
- 使用ViewChild引用html的dom元素
- 在OnInit的成功后,初始化WangEditor編輯器,把模板中的ElementRef放入到WangEditor的容器中去,讓W(xué)angEditor去控制界面的dom操作。
- 實現(xiàn)ControlValueAccessor,讓這個組件支持Angular的表單驗證。
- 實現(xiàn)ngOnDestroy,組件在銷毀的時候,調(diào)用WangEditor的destory
4.使用組件
<q-wang-editor [height]="550"></q-wang-editor>
5.效果預(yù)覽

6.最后
一個WangEditor的Angular組件封裝就基本完成了。如果需要更多功能,比如圖片上傳,等可以再根據(jù)自己的需求增加功能即可
到此這篇關(guān)于Angular封裝WangEditor富文本組件的文章就介紹到這了,更多相關(guān)Angular WangEditor富文本組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Angular.js組件之input mask對input輸入進(jìn)行格式化詳解
這篇文章主要給大家介紹了關(guān)于Angular.js組件之input mask對input輸入進(jìn)行格式化的相關(guān)內(nèi)容,文中通過示例代碼詳細(xì)介紹了將銀行卡號和日期的方法,需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07
AngularJs IE Compatibility 兼容老版本IE
本文主要介紹AngularJs IE Compatibility 兼容老版本IE的問題及解決辦法,有興趣的小伙伴可以參考下2016-09-09
詳解@angular/cli 改變默認(rèn)啟動端口兩種方式
這篇文章主要介紹了詳解@angular/cli 改變默認(rèn)啟動端口兩種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
一篇文章快速了解Angular和Ionic生命周期和鉤子函數(shù)
Ionic以AngularJS和ApacheCordova為基礎(chǔ),使用Node.js進(jìn)行模塊管理,使用Html5、Css(SASS)和Javascript技術(shù)進(jìn)行APP開發(fā),這篇文章主要給大家介紹了如何通過一篇文章快速了解Angular和Ionic生命周期和鉤子函數(shù)的相關(guān)資料,需要的朋友可以參考下2021-07-07
Angular 4.X開發(fā)實踐中的踩坑小結(jié)
這篇文章主要給大家介紹了關(guān)于Angular 4.X開發(fā)實踐中的一些踩坑經(jīng)驗,文中主要介紹的是使用ngIf或者ngSwitch出錯以及多級依賴注入器的相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07
Angular4學(xué)習(xí)之Angular CLI的安裝與使用教程
網(wǎng)上很多教程過時,命令在angular4中不適用等等,所以下面這篇文章主要給大家介紹了關(guān)于Angular4學(xué)習(xí)之Angular CLI的安裝與使用教程的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
詳解Angular系列之變化檢測(Change Detection)
這篇文章主要介紹了詳解Angular系列之變化檢測(Change Detection),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02

