午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:20日期:2022-06-01 14:13:26
目錄
  • 1. Vue3 中 JSX 的基本應用
    • 1.1 在 .vue 文件中使用 jsx
    • 1.2 .jsx文件格式
  • 2. JSX 和 template 的區別
    • 2.1 插值
    • 2.2 自定義組件
    • 2.3 屬性和事件
    • 2.4 條件和循環 
  • 3. JSX 和 slot (體會 JSX 的優越性)
    • 總結 

      1. Vue3 中 JSX 的基本應用

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

      1.1 在 .vue 文件中使用 jsx

      // 父
       
      <template>
        <div>
          <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語法,所以要加 .value
          }
          return render
        }
      }
      </script>

      1.2 .jsx文件格式

      // 父組件
       
      import { defineComponent, ref } from "vue"
      import JSXChild from "./JSXChild.jsx"
       
      export default defineComponent(() => { // 傳入 setup 函數
        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 的區別

      • 語法上有很大區別
      • JSX 本質就是 js 代碼,可以使用 js 的任何能力
      • template 只能嵌入簡單的 js 表達式,其他需要指令,如 v-if
      • JSX 已經成為 ES 規范,template 還是 Vue 自家規范
      • 本質是相同的:
      • 都會被編譯為 js 代碼(render 函數)

      2.1 插值

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

      2.2 自定義組件

      • template 組件名使用時可改變大小寫或是駝峰,jsx 不可更改
      • 引入動態參數,template使用冒號+參數名(:msg='msg'),jsx 不需要冒號
      // template
       
      <template>
        <div>
          <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 區分屬性和事件的寫法,jsx 不區分
      // jsx 屬性和事件的寫法一樣
       
      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 條件和循環 

      條件 template 使用 v-if 指令,jsx 在表達式中使用 && (類似 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 &&符號判斷
       
      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
      })
       循環 template 使用 v-for 指令,jsx 使用數組的 .map 函數
      // 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 數組 .map 函數
       
      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 (體會 JSX 的優越性)

      • slot 是 Vue 發明的概念,為了完善 template 的能力
      • slot 一直是 Vue 初學者的“噩夢”,特別是:作用域 slot
      • 但使用 JSX 將很容易理解,因為 JSX 本質就是 js

      總結 

      到此這篇關于Vue3使用JSX的文章就介紹到這了,更多相關Vue3使用JSX內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

      標簽: JavaScript
      主站蜘蛛池模板: 二区在线播放 | 久久久久久夜 | 一级片在线免费播放 | 亚洲成人高清在线 | 国产视频网 | 成人午夜av| 国产成人免费观看视频 | xxxx18国产| 日本精品视频一区二区 | 欧美日韩国产麻豆 | 国产精品九九视频 | 成人动漫中文字幕 | 99热这里只有精品5 www.四虎com | 欧美日韩精品在线观看 | 午夜影院h| 国产99自拍 | 国产成人精品免费看视频 | 四虎黄色片 | 狠狠干欧美 | 99精品毛片| 精品一区二区在线视频 | 天堂素人约啪 | 成人av免费在线 | 91九色在线视频 | 粗大黑人巨茎大战欧美成人免费看 | 一级黄色大片视频 | 五月激情婷婷综合 | 久艹精品 | 亚洲视频在线视频 | 一级片中文字幕 | 欧美大喷水吹潮合集在线观看 | 成人性生活毛片 | 国产精品女人久久久 | 免费成人深夜夜行网站 | 热久久国产精品 | 午夜激情免费 | 国产成人综合欧美精品久久 | 一区二区三区亚洲 | 日韩欧美亚洲国产 | 中国美女毛片 | 欧美第九页 |