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

标签 javascript svg ionic-framework

我正在尝试将我的 SVG 添加到加载创建函数中,但是当我去查看它时,我看到的只是一个空标签,它应该在该位置。

let loading = this.loadingCtrl.create({spinner: ' ',
            content: this.appConfig.customSpinner })

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

<svg id="Layer_3" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2419 1188.4">
   <defs>
    <mask id="mask">
       <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"/>

  <path fill="#000" d="M787.4 474.6V343.5H394.2v505.6h131V661.8h262.2v-131H525.2v-56.2z"/>
  <path fill="#000" d="M581.4 718h206v131.1h-206z"/>

    <circle fill="#fff" cx="0" cy="1450" r="551.3"/>
    </mask>
  </defs>
  <style> 
    .st2{fill:none;stroke-width:40;stroke-miterlimit:10;}
  </style>


 <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"/>

  <path class="letter" mask="url(#mask)" fill="#fff" d="M787.4 474.6V343.5H394.2v505.6h131V661.8h262.2v-131H525.2v-56.2z"/>
  <path class="letter" mask="url(#mask)" fill="#fff" d="M581.4 718h206v131.1h-206z"/>

  <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"/>

    <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"/>

</svg>

我怎样才能让它渲染?我也已将它复制到 ionic 论坛上 link

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

我制作的内容是这样的:

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

这是我的 pipe :

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({name: 'safe'})
export class SafeHtml {
  constructor(private sanitizer:DomSanitizer){}

  transform(html:any):any {
    return this.sanitizer.bypassSecurityTrustHtml(html);
  }
}

我也尝试过使用文件:

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

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

注意**

更改我的代码后,我意识到您不能在变量内部分配 sanitizer 的返回值,而是在类中声明的变量。我这样做了,我不再遇到 TS 类型错误和加载的 svg XML。

最佳答案

微调器内容必须是“安全 html”,即您必须使用 bypassSecurityTrustHtml。 在你的情况下尝试使用:

let loading = this.loadingCtrl.create({spinner: ' ',
        content: this.sanitizer.bypassSecurityTrustHtml(this.appConfig.customSpinner)
})

检查这个related question .

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

import { DomSanitizer } from '@angular/platform-browser';

constructor(private sanitizer: DomSanitizer){

getProgressBar(percentaje) {
    let html: string =  '<span style="text-align: center">Loading...'
    + Math.round(percentaje)+'%</span>'
    + '<br><progress value="' + percentaje + '" max="100"></progress>';
    return this.sanitizer.bypassSecurityTrustHtml(html);
    }

doSomething(){

    let loading = this.loadingCtrl.create({
       spinner: 'hide',
    });
    loading.data.content = this.getProgressBar(0);
    loading.present();

   //some stuff

   loading.data.content = this.getProgressBar(progress);
   }
}

希望对您有所帮助。

使用 svg 更新:

let svg = `<svg width="100" height="100">
            <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
          </svg>`;

this.safeSvg = this.sanitizer.bypassSecurityTrustHtml(svg);

let loading = this.loadingCtrl.create({
  spinner: 'hide',
  content: this.safeSvg,
});
loading.present();

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

关于javascript - 装载机中的 Ionic 2 自定义 svg 微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44109924/

相关文章:

javascript - 将 UTC 日期从 Javascript 发送到 MVC

javascript - body 和 html 标签的 100% 高度会导致滚动问题

css - 如何将 .svg 文件转换为字体?

css - 线条动画 SVG,动画不起作用

mysql - 如何在选项卡中传递方法(Ionic 2)

javascript - Angular 1.2 : Trigger page change from directive

Javascript自动运行onclick函数

html - 将 SVG 过滤器应用于 HTML5 Canvas ?

android - 错误 : Multiple dex files define Landroid/support/annotations/AnimRes with Admob and Facebook Cordova plugins

javascript - 我的插件中的 Successcallback 和 Errorcallback