javascript – 装载机中的Ionic 2自定义svg微调器

前端之家收集整理的这篇文章主要介绍了javascript – 装载机中的Ionic 2自定义svg微调器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将我的SVG添加到加载创建功能,但是当我去查看它时,我看到它是一个空标记应该是什么.
  1. let loading = this.loadingCtrl.create({spinner: ' ',content: this.appConfig.customSpinner })

上面是我的创建代码,该变量是下面的SVG代码.

  1. <svg id="Layer_3" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2419 1188.4">
  2. <defs>
  3. <mask id="mask">
  4. <path fill="#000" d="M570.2 87.3L163.8 322c-15.6 9-25.2 25.6-25.2 43.6v469.3c0 18 9.6 34.6 25.2 43.6l406.4 234.7c15.6 9 34.7 9 50.3 0l406.4-234.7c15.6-9 25.2-25.6 25.2-43.6V365.6c0-18-9.6-34.6-25.2-43.6L620.5 87.3c-15.5-8.9-34.7-8.9-50.3 0z"/>
  5.  
  6. <path fill="#000" d="M787.4 474.6V343.5H394.2v505.6h131V661.8h262.2v-131H525.2v-56.2z"/>
  7. <path fill="#000" d="M581.4 718h206v131.1h-206z"/>
  8.  
  9. <circle fill="#fff" cx="0" cy="1450" r="551.3"/>
  10. </mask>
  11. </defs>
  12. <style>
  13. .st2{fill:none;stroke-width:40;stroke-miterlimit:10;}
  14. </style>
  15.  
  16.  
  17. <path id="background" mask="url(#mask)" fill="#F16E18" d="M570.2 87.3L163.8 322c-15.6 9-25.2 25.6-25.2 43.6v469.3c0 18 9.6 34.6 25.2 43.6l406.4 234.7c15.6 9 34.7 9 50.3 0l406.4-234.7c15.6-9 25.2-25.6 25.2-43.6V365.6c0-18-9.6-34.6-25.2-43.6L620.5 87.3c-15.5-8.9-34.7-8.9-50.3 0z"/>
  18.  
  19. <path class="letter" mask="url(#mask)" fill="#fff" d="M787.4 474.6V343.5H394.2v505.6h131V661.8h262.2v-131H525.2v-56.2z"/>
  20. <path class="letter" mask="url(#mask)" fill="#fff" d="M581.4 718h206v131.1h-206z"/>
  21.  
  22. <path id="hexagon-2" stroke="transparent" class="st2" d="M570.1 82.5L163.7 317.2c-15.6 9-25.2 25.6-25.2 43.6v469.3c0 18 9.6 34.6 25.2 43.6l406.4 234.7c15.6 9 34.7 9 50.3 0l406.4-234.7c15.6-9 25.2-25.6 25.2-43.6V360.8c0-18-9.6-34.6-25.2-43.6L620.4 82.5c-15.5-8.9-34.7-8.9-50.3 0z"/>
  23.  
  24. <path id="hexagon-1" stroke="transparent" class="st2" d="M570.1 82.5L163.7 317.2c-15.6 9-25.2 25.6-25.2 43.6v469.3c0 18 9.6 34.6 25.2 43.6l406.4 234.7c15.6 9 34.7 9 50.3 0l406.4-234.7c15.6-9 25.2-25.6 25.2-43.6V360.8c0-18-9.6-34.6-25.2-43.6L620.4 82.5c-15.5-8.9-34.7-8.9-50.3 0z"/>
  25.  
  26. </svg>

我怎样才能让它渲染?我也将它复制到link的离子论坛上

我试图添加一个管道,使其在运行时安全,但也失败了.

我做了这样的内容

  1. <div [innerHTML]='appConfig.customSpinner | safe'></div>

这是我的烟斗:

  1. import { Pipe,PipeTransform } from '@angular/core';
  2. import { DomSanitizer } from '@angular/platform-browser';
  3.  
  4. @Pipe({name: 'safe'})
  5. export class SafeHtml {
  6. constructor(private sanitizer:DomSanitizer){}
  7.  
  8. transform(html:any):any {
  9. return this.sanitizer.bypassSecurityTrustHtml(html);
  10. }
  11. }

我也尝试过使用文件

  1. let loading = this.loadingCtrl.create({spinner: 'hide',content:"<object data='assets/spinner.svg' type='image/svg+xml'></object>"})

这仍然会导致同样的问题.

NB **

更改我的代码后,我意识到你不能在变量内部分配清洁剂的返回值,而是在类中声明一个声明的变量.一个我这样做我不再有TS类型错误和svg XML加载.

解决方法

微调器内容必须是’安全html’,即你必须使用bypassSecurityTrustHtml.
在你的情况下尝试使用:
  1. let loading = this.loadingCtrl.create({spinner: ' ',content: this.sanitizer.bypassSecurityTrustHtml(this.appConfig.customSpinner)
  2. })

检查这个related question.

这是我在Ionic 3应用程序中使用html5微调器的工作代码

  1. import { DomSanitizer } from '@angular/platform-browser';
  2.  
  3. constructor(private sanitizer: DomSanitizer){
  4.  
  5. getProgressBar(percentaje) {
  6. let html: string = '<span style="text-align: center">Loading...'
  7. + Math.round(percentaje)+'%</span>'
  8. + '<br><progress value="' + percentaje + '" max="100"></progress>';
  9. return this.sanitizer.bypassSecurityTrustHtml(html);
  10. }
  11.  
  12. doSomething(){
  13.  
  14. let loading = this.loadingCtrl.create({
  15. spinner: 'hide',});
  16. loading.data.content = this.getProgressBar(0);
  17. loading.present();
  18.  
  19. //some stuff
  20.  
  21. loading.data.content = this.getProgressBar(progress);
  22. }
  23. }

希望能帮助到你.

使用svg更新:

  1. let svg = `<svg width="100" height="100">
  2. <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  3. </svg>`;
  4.  
  5. this.safeSvg = this.sanitizer.bypassSecurityTrustHtml(svg);
  6.  
  7. let loading = this.loadingCtrl.create({
  8. spinner: 'hide',content: this.safeSvg,});
  9. loading.present();

工作代码可以在at this git repository找到

猜你在找的JavaScript相关文章