模糊大视野 發表於 2019-6-17 12:11:00

Angular中innerHTML标签的样式不起作用详解

<p>1.背景</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;在最近angular的项目中,需要用到标签来指定一个div的样式:&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">//HTML部分
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="contents"</span><span style="color: rgba(255, 0, 0, 1)"> </span><span style="color: rgba(0, 0, 255, 1)">="contents"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">3</span>
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 0, 1)">//TS部分
</span><span style="color: rgba(0, 128, 128, 1)">5</span> contents = '<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">p</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>商品信息栏位<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">br</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)">style</span><span style="color: rgba(0, 0, 255, 1)">="color:red;"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>商品信息介绍<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">p</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>';</pre>
</div>
<p><span style="color: rgba(51, 51, 153, 1)">&nbsp; &nbsp; &nbsp; &nbsp; 但是上面的样式并不起作用,在Chorme中查看源码,发现style标签的样式在Angular编译的时候被屏蔽掉。这是为什么呢?客观别急,请往下看。</span></p>
<p>2.解决方案</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;先说解决方案,最后再分析出现这种问题的原因。修改上面的TS:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;"> //在使用的页面引入DomSanitizer
import { DomSanitizer } from '@angular/platform-browser';


//构造方法里注入sanitizer对象
    constructor( private sanitizer: DomSanitizer
    ) { }

// 对HTML代码做处理
    this.contents= this.sanitizer.bypassSecurityTrustHtml("&lt;p&gt;W3商品信息栏位&lt;br&gt;&lt;span style="color:red;"&gt;商品信息介绍&lt;/span&gt;&lt;/p&gt;");
</pre>
</div>
<p>  这样虽然可以解决问题,但是这样做还不够:</p>
<ul>
<li><span style="color: rgba(255, 0, 0, 1)">代码冗余繁杂</span>:如果我们的contents内容过大,这样我们的代码就显得很乱,影响可读性和美观;</li>
<li><span style="color: rgba(255, 0, 0, 1)">不能复用</span>:如果其他ts中也要用到innerHTML标签,又要重新写一遍上面的TS内容,没有复用性;</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; 基于以上两点,我们用<span style="color: rgba(0, 0, 0, 1); background-color: rgba(255, 0, 0, 1)">自定义管道(pipe)</span>来优化以上代码,使用<span style="background-color: rgba(255, 0, 0, 1)">ng generate pipe safe-html</span>命令来生成一个pipe,并做适当的修改:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;collapse:true;;gutter:true;">//对safe-html.pipe.ts做适当修改<br><br>import {Pipe, PipeTransform} from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';

@Pipe({name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform {
    constructor(private sanitized: DomSanitizer) {
    }

    transform(value) {
      return this.sanitized.bypassSecurityTrustHtml(value);
    }
}<br><br>// 在使用innerHTML标签的属性里使用以上safeHtml管道</pre>
<pre>&lt;div class="contents" ="contents|safeHtml"&gt;&lt;/div&gt;</pre>
</div>
<p>3.原因及原理</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;所以,为什么会出现上面的问题呢?原来,Angular中默认将所有输入值视为不受信任。当我们通过 property,attribute,样式,类绑定或插值等方式,</p>
<p>&nbsp; &nbsp;将一个值从模板中插入到DOM中时,Angular会自帮我们清除和转义不受信任的值。在开头的例子中,span标签里的样式被屏蔽了,不信请看:</p>
<p>&nbsp; &nbsp;<img src="https://img2018.cnblogs.com/blog/1609477/201906/1609477-20190617115808234-858079790.png">  </p>
<p>&nbsp;&nbsp;Angular 在编译的时候,会自动清理 HTML 输入并转义不安全的代码,因此在这种情况下,style被屏蔽,样式失效。这时候如果需要将样式片段渲染出来,</p>
<p>&nbsp;就需要用到DomSanitizer了。<span style="color: rgba(255, 0, 0, 1)">DomSanitizer 可以把值净化为在不同 DOM 上下文中的安全内容,来帮我们防范跨站脚本攻击(XSS)类的安全问题</span>。</p>
<p>&nbsp;</p>
<p>参考:https://angular.cn/api/platform-browser/DomSanitizer#description</p>
<p>  </p><br><br>
来源:https://www.cnblogs.com/robinw666/p/11039007.html
頁: [1]
查看完整版本: Angular中innerHTML标签的样式不起作用详解