欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3使用JSX的方法實(shí)例(筆記自用)

 更新時(shí)間:2023年02月23日 09:38:37   作者:weixin_39763711  
以前我們經(jīng)常在react中使用jsx,現(xiàn)在我們?cè)趘ue中也是用jsx,下面這篇文章主要給大家介紹了關(guān)于Vue3使用JSX的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

1. Vue3 中 JSX 的基本應(yīng)用

  • 使用 .jsx 格式文件和 defineComponent
  • defineComponent 可傳入 setup 函數(shù) 或 組件的配置
  • 插值使用單括號(hào) {}

1.1 在 .vue 文件中使用 jsx

// 父
 
<template>
  <div class="home">
    <JSXDemo1 />
  </div>
</template>
 
<script>
import JSXDemo1 from '@/components/JSXDemo1.vue'
export default {
  name: 'HomeView',
  components: {
    JSXDemo1
  }
}
</script>
 
// JSXDemo1.vue
 
<script>
import { ref } from 'vue'
export default {
  setup () {
    const countRef = ref(200)
 
    const render = () => {
      return <p>DEMO1--{countRef.value}</p> // jsx就是js語(yǔ)法,所以要加 .value
    }
    return render
  }
}
</script>

1.2 .jsx文件格式

// 父組件
 
import { defineComponent, ref } from 'vue'
import JSXChild from './JSXChild.jsx'
 
export default defineComponent(() => { // 傳入 setup 函數(shù)
  const countRef = ref(300)
 
  const render = () => {
    return <>
      <p>DEMO2--{countRef.value}</p>
      <JSXChild a={countRef.value + 100}></JSXChild>
    </>
  }
  return render 
})
 
// 子組件 JSXChild.jsx
 
import { defineComponent } from 'vue'
 
export default defineComponent({ // 傳入組件配置
  props: ['a'],
  setup (props) {
    const render = () => {
      return <>
        <p>child {props.a}</p>
      </>
    }
    return render
  }
})

2. JSX 和 template 的區(qū)別

  • 語(yǔ)法上有很大區(qū)別
  • JSX 本質(zhì)就是 js 代碼,可以使用 js 的任何能力
  • template 只能嵌入簡(jiǎn)單的 js 表達(dá)式,其他需要指令,如 v-if
  • JSX 已經(jīng)成為 ES 規(guī)范,template 還是 Vue 自家規(guī)范
  • 本質(zhì)是相同的:
  • 都會(huì)被編譯為 js 代碼(render 函數(shù))

2.1 插值

  • template 使用雙括號(hào) {{ }}
  • jsx 使用單括號(hào) { }
// template
 
<template>
  <p>{{ name }} -- {{ age }}</p>
</template>
 
// jsx
 
const render = () => {
    return <>
        <p>child {props.a}</p>
    </>
}

2.2 自定義組件

  • template 組件名使用時(shí)可改變大小寫(xiě)或是駝峰,jsx 不可更改
  • 引入動(dòng)態(tài)參數(shù),template使用冒號(hào)+參數(shù)名(:msg='msg'),jsx 不需要冒號(hào)
// template
 
<template>
  <div class="home">
    <watch-effect :msg="msgRef"/>
  </div>
</template>
 
<script>
import { ref } from 'vue'
import WatchEffect from '@/components/WatchEffect.vue'
export default {
  name: 'HomeView',
  components: {
    WatchEffect,
  },
  setup () {
    const msgRef = ref('123')
    return {
        msgRef
    }
  }
}
</script>
 
// jsx 組件名稱不可變,要和引入名字保持一致
 
import { defineComponent, ref } from 'vue'
import JSXChild from './JSXChild.jsx'
 
export default defineComponent(() => {
  const countRef = ref(300)
 
  const render = () => {
    return <>
      <p>DEMO2--{countRef.value}</p>
      <JSXChild a={countRef.value + 100}></JSXChild>
    </>
  }
  return render
})

2.3 屬性和事件

template 區(qū)分屬性和事件的寫(xiě)法,jsx 不區(qū)分
// jsx 屬性和事件的寫(xiě)法一樣
 
import { defineComponent, ref } from 'vue'
import JSXChild from './JSXChild.jsx'
 
export default defineComponent(() => {
  const countRef = ref(300)
 
  function onChange () {
    console.log('onChange')
  }
  const render = () => {
    return <>
      <p>DEMO2--{countRef.value}</p>
      <JSXChild a={countRef.value + 100} change={onChange}></JSXChild>
    </>
  }
  return render
})

2.4 條件和循環(huán) 

條件 template 使用 v-if 指令,jsx 在表達(dá)式中使用 && (類似 if( a && b))
// template v-if
 
<template>
  <p v-if="flagRef">template demo</p>
  <button @click="changeFlagRef">click</button>
</template>
<script>
import { ref } from 'vue'
export default {
  setup () {
    const flagRef = ref(true)
 
    function changeFlagRef () {
      flagRef.value = !flagRef.value
    }
 
    return {
      flagRef,
      changeFlagRef
    }
  }
}
</script>
 
// jsx &&符號(hào)判斷
 
import { defineComponent, ref } from 'vue'
import JSXChild from './JSXChild.jsx'
 
export default defineComponent(() => {
  const flagRef = ref(true)
 
  function changeFlagRef () {
    flagRef.value = !flagRef.value
  }
 
  const render = () => {
    return <>
      <p onClick={changeFlagRef}>DEMO2--{flagRef.value.toString()}</p>
      {flagRef.value && <JSXChild a={flagRef.value}></JSXChild>}
    </>
  }
  return render
})
 循環(huán) template 使用 v-for 指令,jsx 使用數(shù)組的 .map 函數(shù)
// template v-for
 
<template>
  <ul>
    <li v-for="item in state.list" :key="item">{{ item }}</li>
  </ul>
</template>
<script>
import { reactive } from 'vue'
export default {
  setup () {
    const state = reactive({
      list: ['a', 'b', 'c']
    })
 
    return {
      state
    }
  }
}
</script>
 
// jsx 數(shù)組 .map 函數(shù)
 
import { defineComponent, reactive } from 'vue'
 
export default defineComponent(() => {
  const state = reactive({
    list: ['a1', 'b1', 'c1']
  })
 
  const render = () => {
    return <>
      <ul>
        {state.list.map(item => <li>{item}</li>)}
      </ul>
    </>
  }
  return render
})

3. JSX 和 slot (體會(huì) JSX 的優(yōu)越性)

  • slot 是 Vue 發(fā)明的概念,為了完善 template 的能力
  • slot 一直是 Vue 初學(xué)者的“噩夢(mèng)”,特別是:作用域 slot
  • 但使用 JSX 將很容易理解,因?yàn)?JSX 本質(zhì)就是 js

總結(jié) 

到此這篇關(guān)于Vue3使用JSX的文章就介紹到這了,更多相關(guān)Vue3使用JSX內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • WEB前端性能優(yōu)化的7大手段詳解

    WEB前端性能優(yōu)化的7大手段詳解

    本文將詳細(xì)介紹前端性能優(yōu)化的7大手段,包括減少請(qǐng)求數(shù)量、減小資源大小、優(yōu)化網(wǎng)絡(luò)連接、優(yōu)化資源加載、減少重繪回流、使用性能更好的API和構(gòu)建優(yōu)化
    2020-02-02
  • Vue組件開(kāi)發(fā)初探

    Vue組件開(kāi)發(fā)初探

    本篇文章主要介紹了Vue組件開(kāi)發(fā)初探,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • Vue中this.$router.push參數(shù)獲取方法

    Vue中this.$router.push參數(shù)獲取方法

    下面小編就為大家分享一篇Vue中this.$router.push參數(shù)獲取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • Vue中Element的table多選表格如何實(shí)現(xiàn)單選

    Vue中Element的table多選表格如何實(shí)現(xiàn)單選

    這篇文章主要介紹了Vue中Element的table多選表格如何實(shí)現(xiàn)單選,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 關(guān)于Vue中的全局導(dǎo)航守衛(wèi)(beforeEach、afterEach)

    關(guān)于Vue中的全局導(dǎo)航守衛(wèi)(beforeEach、afterEach)

    這篇文章主要介紹了關(guān)于Vue中的全局導(dǎo)航守衛(wèi)(beforeEach、afterEach),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue實(shí)現(xiàn)復(fù)制文字復(fù)制圖片實(shí)例詳解

    vue實(shí)現(xiàn)復(fù)制文字復(fù)制圖片實(shí)例詳解

    這篇文章主要為大家介紹了vue實(shí)現(xiàn)復(fù)制文字復(fù)制圖片實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • vue新建環(huán)境變量以及網(wǎng)絡(luò)請(qǐng)求工具axios的二次封裝詳解

    vue新建環(huán)境變量以及網(wǎng)絡(luò)請(qǐng)求工具axios的二次封裝詳解

    這篇文章主要為大家介紹了vue新建環(huán)境變量以及網(wǎng)絡(luò)請(qǐng)求工具axios的二次封裝詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 深入探討Vue?3中的組合式函數(shù)編程方式

    深入探討Vue?3中的組合式函數(shù)編程方式

    Vue?3中引入了組合式函數(shù)編程方式,可以更好地實(shí)現(xiàn)代碼的復(fù)用和可維護(hù)性。通過(guò)定義可組合的函數(shù),可以將組件的邏輯和狀態(tài)進(jìn)行拆分和組合,實(shí)現(xiàn)更靈活的代碼組織方式。同時(shí),組合式函數(shù)也支持響應(yīng)式數(shù)據(jù)和生命周期鉤子函數(shù),更加貼近Vue開(kāi)發(fā)的實(shí)際場(chǎng)景
    2023-05-05
  • 解決vue-cli腳手架打包后vendor文件過(guò)大的問(wèn)題

    解決vue-cli腳手架打包后vendor文件過(guò)大的問(wèn)題

    今天小編就為大家分享一篇解決vue-cli腳手架打包后vendor文件過(guò)大的問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • uniapp?vue與nvue輪播圖之輪播圖組件的示例代碼

    uniapp?vue與nvue輪播圖之輪播圖組件的示例代碼

    這篇文章主要介紹了uniapp?vue與nvue輪播圖輪播圖組件的實(shí)例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12

最新評(píng)論