99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

Taro 渲染HTML

2021-09-23 21:10 更新

請注意:本章節(jié)所有內(nèi)容只在小程序端起效果。

Taro 可以直接通過 Element#innerHTML 或 Vue 的 v-html 或 React/Nerv 的 dangerouslySetInnerHTML 直接渲染 HTML:

import Tabs from ‘@theme/Tabs’; import TabItem from ‘@theme/TabItem’;

  1. function helloWorld() {
  2. const html = `<h1 style="color: red">Wallace is way taller than other reporters.</h1>`
  3. return <View dangerouslySetInnerHTML={{ __html: html }}></View>
  4. }
  1. <template>
  2. <view v-html="html"></view>
  3. </template>

  4. <script>
  5. export default {
  6. data () {
  7. return {
  8. html: `<h1 style="color: red">Wallace is way taller than other reporters.</h1>`
  9. } }

  10. }
  11. </script>

自定義 HTML 樣式

直接設(shè)置 HTML 不會讓 HTML 標(biāo)簽帶上瀏覽器的默認(rèn)樣式,Taro 提供兩種內(nèi)置樣式我們可以直接引入生效:

  • @tarojs/taro/html.css: W3C HTML4 的內(nèi)置樣式,只有 HTML4 標(biāo)簽樣式,體積較小,兼容性強(qiáng),能適應(yīng)大多數(shù)情況。
  • @tarojs/taro/html5.css: Chrome(Blink) HTML5 的內(nèi)置樣式,內(nèi)置樣式豐富,包括了大多數(shù) HTML5 標(biāo)簽,體積較大,不一定支持所有小程序容器。

為了讓內(nèi)置的標(biāo)簽樣式起作用,我們還需要將 HTML 容器的 CSS 類設(shè)置為 .taro_html:

  1. import '@tarojs/taro/html.css'
  2. function helloWorld() {
  3. const html = `<h1 style="color: red">Wallace is way taller than other reporters.</h1>`
  4. return <View className="taro_html" dangerouslySetInnerHTML={{ __html: html }}></View>
  5. }
  1. <template>
  2. <view v-html="html" class="taro_html"></view>
  3. </template>

  4. <script>
  5. import '@tarojs/taro/html.css'

  6. export default {
  7. data () {
  8. return {
  9. html: `<h1 style="color: red">Wallace is way taller than other reporters.</h1>`
  10. }
  11. }
  12. }
  13. </script>

同樣地,我們也可以自己編寫 CSS 樣式,Taro 最終渲染的 HTML 標(biāo)簽的類名都和 HTML 標(biāo)簽名保持一致,例如我們的容器 CSS 類名為 my_css,想要設(shè)置 h1 標(biāo)簽的樣式,那就這樣這樣做:

  1. .my_css .h1 {
  2. font-size: 30px;
  3. }

你可能會傾向于其他瀏覽器的默認(rèn)樣式:

HTML 轉(zhuǎn)義

由于進(jìn)行 HTML 轉(zhuǎn)義需要消耗較多的性能和較大的體積,默認(rèn)而言 Taro 的 HTML 接口只接受轉(zhuǎn)義后的 HTML 字符串,我們推薦直接在服務(wù)端返回轉(zhuǎn)義后的 HTML。如果確實(shí)需要在客戶端轉(zhuǎn)義,開源社區(qū)有兩個(gè)較好的選擇:

  • he: 體積較大,但開源協(xié)議更為寬松
  • entities: 體積較小,但開源協(xié)議更為嚴(yán)格

你可能會需要高級選項(xiàng)transformText 配合 HTML 轉(zhuǎn)義進(jìn)行字符串渲染。

綁定事件

出于作用域和安全因素考慮,Taro 會把 HTML 字符串中的事件和 JavaScript 全部清除。但我們?nèi)匀挥修k法給 HTML 綁定事件:

  1. import '@tarojs/taro/html.css'
  2. function helloWorld() {
  3. const html = `<h1 id="test">Wallace is way taller than other reporters.</h1>`
  4. useEffect(() => {
  5. const el = document.getElementById('test')
  6. function testOnTap (event) {
  7. // do something
  8. ...
  9. }
  10. el.addEventListener('tap', testOnTap)
  11. return () => {
  12. el.removeEventListener('tap', testOnTap)
  13. }
  14. }, [])
  15. return <View className="taro_html" dangerouslySetInnerHTML={{ __html: html }}></View>
  16. }
  1. <template>
  2. <view v-html="html" class="taro_html"></view>
  3. </template>

  4. <script>
  5. import '@tarojs/taro/html.css'

  6. export default {
  7. data () {
  8. return {
  9. html: `<h1 id="test">Wallace is way taller than other reporters.</h1>`
  10. }
  11. },
  12. mounted () {
  13. const el = document.getElementById('test')
  14. el.addEventListener('tap', this.testOnTap)
  15. },
  16. testOnTap (event) {
  17. // do something
  18. ...
  19. },
  20. beforeDestroy () {
  21. const el = document.getElementById('test')
  22. el.removeEventListener('tap', this.testOnTap)
  23. }
  24. }
  25. </script>

如果需要?jiǎng)討B(tài)綁定事件的元素沒有 ID 的話,你可能需要使用高級選項(xiàng)transformElement。

高級選項(xiàng)

如果內(nèi)置的功能無法滿足需求或渲染結(jié)果不如預(yù)期,Taro 還提供了 HTML 渲染的高級選項(xiàng),高級選項(xiàng)可以通過 Taro.options.html 訪問:

  1. import Taro from '@tarojs/taro'
  2. // 不解析 souce 標(biāo)簽中的內(nèi)容
  3. Taro.options.html.skipElements.add('source')

skipElements

類型:Set<string>

默認(rèn)值:new Set(['style', 'script'])

HashSet 中包含的 HTML 標(biāo)簽將不會被解析。

voidElements

類型:Set<string>

默認(rèn)值:new Set([ '!doctype', 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr' ])

HashSet 中包含的 HTML 標(biāo)簽不需要閉合標(biāo)簽,例如 <img />。

closingElements

類型:Set<string>

默認(rèn)值:new Set([ 'html', 'head', 'body', 'p', 'dt', 'dd', 'li', 'option', 'thead', 'th', 'tbody', 'tr', 'td', 'tfoot', 'colgroup' ])

HashSet 中包含的 HTML 標(biāo)簽可以自動閉合,且不能被嵌套。

transformText

類型:(taroText: TaroText, text: Text) => TaroText

默認(rèn)值:void

該函數(shù)第一個(gè)參數(shù)的值為 Taro 默認(rèn)處理好的 TaroText,第二個(gè)參數(shù)是 HTML 解析器解析好的 Text,最后返回的 TaroText 對象參與 HTML 中的字符串渲染。

transformElement

類型:(taroElement: TaroElement, element: Element) => TaroElement

默認(rèn)值:void

該函數(shù)第一個(gè)參數(shù)的值為 Taro 默認(rèn)處理好的 TaroElement,第二個(gè)參數(shù)是 HTML 解析器解析好的 Element,最后返回的 TaroElement 對象參與 HTML 元素渲染。

示例:

  1. // 給所有 img 標(biāo)簽添加 aotu 類
  2. Taro.options.html.transformElement = (el) => {
  3. if (el.nodeName === 'img') {
  4. el.setAttribute('class', 'aotu')
  5. }
  6. return el
  7. }


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號