Angular项目中使用Swiper和Ueditor

发布时间:2019-06-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Angular项目中使用Swiper和Ueditor脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

这一阵子开始学习Angular,张dalao给我一个阉割了很多功能的初级后台项目作为练手,虽然初级,但是有些东西对于初学者来说真的很难,就比如说现在就要讲的引入SwiPEr和UedITor这两个插件。
难点在于这两个东西都挺老的了,Swiper还好,而Ueditor已经很久没有更新了,在网上查了很久,磕磕绊绊,终于顺利的把这两个东西引入到了项目里。

废话不多说,上图上代码


先讲Ueditor吧,下图是引入以后的Ueditor富文本编辑器

Ueditor

这个是Ueditor的GitHub地址

按照GitHub的教程来

先把文件download下来

clipboard.png

然后在项目里安装

npm install ngx-ueditor --save

然后在AppModel.ts里面引入下面的代码

import { browserModule } From '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { UEditorModule } from 'ngx-ueditor';

@NgModule({
  imports: [ 
    BrowserModule,
    FormsModule,
    UEditorModule.forRoot({
      js: [
        `./assets/ueditor/ueditor.config.js`,
        `./assets/ueditor/ueditor.all.min.js`,
      ],
      // 默认前端配置项
      options: {
        UEDITOR_HOME_URL: './assets/ueditor/'
      }
    })
  ],
  declarations: [Appcomponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

可以看到js:[]里面引入了两个js文件,这两个文件就在刚刚下下来的压缩包里

路径有点复杂
ngx-ueditor-mastersrcassets

把这个ueditor文件夹整个解压到项目里的assets目录下,这样准备工作就做完了

Angular项目中使用Swiper和Ueditor

接下来就是在组件里面用了
下面的是htML代码

<ueditor 
[(ngModel)]="html" //双向绑定编辑器的内容
[config]="editorConfig" //配置
[loadingTip]="'加载中……'" //加载时的文字
(onReady)="_ready($event)" //加载完成时的回调
(onDestroy)="_destroy()"  //销毁时的回调
(ngModelChange)="_change($event)" //内容改变时的回调>
</ueditor>

接下来是ts代码

  html = ``; //编辑器内容
  editorConfig={
    wordCount: true, // 文字计数
    initialFrameHeight: 300, // 设置高度
    initialFrameWidth: '100%', // 设置度
  }

   _ready($event){

  }
  _destroy(){

  }
  _change($event){

  }

具体的API可以在文档里面的查到,就不多说

这样就可以在Angular里面使用Ueditor了

下班咯,明天再更新一下Swiper的使用


周末好好的补了一下觉,周一精神满满,现在来讲讲怎么引入Swiper

这是Angular中使用Swiper教程的参考链接

首先要在项目中安装Swiper

npm install swiper --save

然后在Angular.json里面引入swiper的js和css文件

Angular项目中使用Swiper和Ueditor

当然你也可以在组件里面引用

Angular项目中使用Swiper和Ueditor

然后就是安装模组定义档

npm install @types/swiper --save

配置tsconfig文件

Angular项目中使用Swiper和Ueditor

当然如果你是npm安装的话,这里会自动配置好

下面是配置tsconfig.app.json

Angular项目中使用Swiper和Ueditor

配置完成以后就可以在Angular里面使用Swiper了

这个是我自己项目里使用的的HTML代码

<div class="swiper-container" style="width: 100%;height: 100%">
 <div class="swiper-wrapper">
   <div class="swiper-slide" *ngFor="let data of manualDetail.contentList">
    <img [src]="data.url" alt="" style="width: 100%;height: 100%">
   </div>
 </div>
</div>

下面是ts代码

setTimeout(() => {//如果不设setTimeout 前进后退按钮就无效
      this.testSwiper = new Swiper('.swiper-container', {

        spaceBetween: 10,

        direction: 'horizontal',
        loop: false,
        // 如果需要分页器
        pagination: {
          el: '.swiper-pagination',
        },
        // 如果需要前进后退按钮
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
        // 如果需要滚动条
        scrollbar: {
          el: '.swiper-scrollbar',
        },
        watchSlidesVisibility: true,//防止不可点击
        observer:true,//修改swiper自己或子元素时,自动初始化swiper
        observeParents:true,//修改swiper的父元素时,自动初始化swiper
      });
    }, 0);

下面就是效果图了

clipboard.png

其他的api可以在官网查到

不过有一些问题至今没有解决,就是官网的一些方法现在还没办法使用,比如说缩略图,写在ts里会报错。

脚本宝典总结

以上是脚本宝典为你收集整理的Angular项目中使用Swiper和Ueditor全部内容,希望文章能够帮你解决Angular项目中使用Swiper和Ueditor所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。