国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Angular封裝WangEditor富文本組件的方法

發(fā)布時(shí)間:2021-08-17 12:16 來(lái)源: 閱讀:0 作者:青城同學(xué)的博客 欄目: JavaScript 歡迎投稿:712375056

富文本組件是web程序中很常用的一個(gè)組件,特別是要開(kāi)發(fā)一個(gè)博客,論壇這類(lèi)的網(wǎng)站后臺。

得益于A(yíng)ngular的強大,封裝WangEditor組件非常簡(jiǎn)單

1.使用yarn或者npm安裝wangeditor

yarn add wangeditor

2.創(chuàng )建一個(gè)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操作。
  • 實(shí)現ControlValueAccessor,讓這個(gè)組件支持Angular的表單驗證。
  • 實(shí)現ngOnDestroy,組件在銷(xiāo)毀的時(shí)候,調用WangEditor的destory

4.使用組件

<q-wang-editor [height]="550"></q-wang-editor> 

5.效果預覽

6.最后

一個(gè)WangEditor的Angular組件封裝就基本完成了。如果需要更多功能,比如圖片上傳,等可以再根據自己的需求增加功能即可

到此這篇關(guān)于A(yíng)ngular封裝WangEditor富文本組件的文章就介紹到這了,更多相關(guān)Angular WangEditor富文本組件內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

我的公强要了我高潮| 免费国产白丝喷水娇喘视频| 天堂а√在线地址| 无卡无码无免费毛片| 伊人久久大香线蕉综合网站| 伊人久久五月丁香综合中文亚洲|