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

尤大大新活petite-vue的實(shí)現(xiàn)

 更新時(shí)間:2021年07月14日 09:53:17   作者:8號(hào)的凌晨4點(diǎn)  
打開(kāi)尤大大的GitHub,發(fā)現(xiàn)多了個(gè)叫 petite-vue 的東西,Vue3 和 Vite 還沒(méi)學(xué)完呢,又開(kāi)始整新東西了?本文就來(lái)介紹一下

前言

打開(kāi)尤大大的GitHub,發(fā)現(xiàn)多了個(gè)叫 petite-vue 的東西,好家伙,Vue3 和 Vite 還沒(méi)學(xué)完呢,又開(kāi)始整新東西了?本著學(xué)不死就往死里學(xué)的態(tài)度,咱還是來(lái)瞅瞅這到底是個(gè)啥東西吧,誰(shuí)讓他是咱的祖師爺呢!

簡(jiǎn)介

從名字來(lái)看可以知道 petite-vue 是一個(gè) mini 版的vue,大小只有5.8kb,可以說(shuō)是非常小了。據(jù)尤大大介紹,petite-vue 是 Vue 的可替代發(fā)行版,針對(duì)漸進(jìn)式增強(qiáng)進(jìn)行了優(yōu)化。它提供了與標(biāo)準(zhǔn) Vue 相同的模板語(yǔ)法和響應(yīng)式模型:

  • 大小只有5.8kb
  • Vue 兼容模版語(yǔ)法
  • 基于DOM,就地轉(zhuǎn)換
  • 響應(yīng)式驅(qū)動(dòng)

上活

下面對(duì) petite-vue 的使用做一些介紹。

簡(jiǎn)單使用

<body>
  <script src="https://unpkg.com/petite-vue" defer init></script>
  <div v-scope="{ count: 0 }">
    <button @click="count--">-</button>
    <span>{{ count }}</span>
    <button @click="count++">+</button>
  </div>
</body>

通過(guò) script 標(biāo)簽引入同時(shí)添加 init ,接著就可以使用 v-scope 綁定數(shù)據(jù),這樣一個(gè)簡(jiǎn)單的計(jì)數(shù)器就實(shí)現(xiàn)了。

了解過(guò) Alpine.js 這個(gè)框架的同學(xué)看到這里可能有點(diǎn)眼熟了,兩者語(yǔ)法之間是很像的。

<!--  Alpine.js  -->
<div x-data="{ open: false }">
  <button @click="open = true">Open Dropdown</button>
  <ul x-show="open" @click.away="open = false">
    Dropdown Body
  </ul>
</div>

除了用 init 的方式之外,也可以用下面的方式:

<body>
  <div v-scope="{ count: 0 }">
    <button @click="count--">-</button>
    <span>{{ count }}</span>
    <button @click="count++">+</button>
  </div>
  <!--  放在body底部  -->
  <script src="https://unpkg.com/petite-vue"></script>
  <script>
    PetiteVue.createApp().mount()
  </script>
</body>

或使用 ES module 的方式:

<body>
  <script type="module">
    import { createApp } from 'https://unpkg.com/petite-vue?module'
    createApp().mount()
  </script>
  
  <div v-scope="{ count: 0 }">
    <button @click="count--">-</button>
    <span>{{ count }}</span>
    <button @click="count++">+</button>
  </div>  
</body>

根作用域

createApp 函數(shù)可以接受一個(gè)對(duì)象,類似于我們平時(shí)使用 data 和 methods 一樣,這時(shí) v-scope 不需要綁定值。

<body>
  <script type="module">
    import { createApp } from 'https://unpkg.com/petite-vue?module'
    createApp({
      count: 0,
      increment() {
        this.count++
      },
      decrement() {
        this.count--
      }
    }).mount()
  </script>
  
  <div v-scope>
    <button @click="decrement">-</button>
    <span>{{ count }}</span>
    <button @click="increment">+</button>
  </div>
</body>

指定掛載元素

<body>
  <script type="module">
    import { createApp } from 'https://unpkg.com/petite-vue?module'
    createApp({
      count: 0
    }).mount('#app')
  </script>
  
  <div id="app">
    {{ count }}
  </div>
</body>

生命周期

可以監(jiān)聽(tīng)每個(gè)元素的生命周期事件。

<body>
  <script type="module">
    import { createApp } from 'https://unpkg.com/petite-vue?module'
    createApp({
      onMounted1(el) {
        console.log(el) // <span>1</span>
      },
      onMounted2(el) {
        console.log(el) // <span>2</span>
      }
    }).mount('#app')
  </script>
  
  <div id="app">
    <span @mounted="onMounted1($el)">1</span>
    <span @mounted="onMounted2($el)">2</span>
  </div>
</body>

組件

在 petite-vue 里,組件可以使用函數(shù)的方式創(chuàng)建,通過(guò)template可以實(shí)現(xiàn)復(fù)用。

<body>
  <script type="module">
  import { createApp } from 'https://unpkg.com/petite-vue?module'

  function Counter(props) {
    return {
      $template: '#counter-template',
      count: props.initialCount,
      increment() {
        this.count++
      },
      decrement() {
        this.count++
      }
    }
  }

  createApp({
    Counter
  }).mount()
</script>

<template id="counter-template">
  <button @click="decrement">-</button>
  <span>{{ count }}</span>
  <button @click="increment">+</button>
</template>

<!-- 復(fù)用 -->
<div v-scope="Counter({ initialCount: 1 })"></div>
<div v-scope="Counter({ initialCount: 2 })"></div>
</body>

全局狀態(tài)管理

借助 reactive 響應(yīng)式 API 可以很輕松的創(chuàng)建全局狀態(tài)管理

<body>
  <script type="module">
    import { createApp, reactive } from 'https://unpkg.com/petite-vue?module'

    const store = reactive({
      count: 0,
      increment() {
        this.count++
      }
    })
    // 將count加1
    store.increment()
    createApp({
      store
    }).mount()
  </script>

  <div v-scope>
    <!-- 輸出1 -->
    <span>{{ store.count }}</span>
  </div>
  <div v-scope>
    <button @click="store.increment">+</button>
  </div>
</body>

自定義指令

這里來(lái)簡(jiǎn)單實(shí)現(xiàn)一個(gè)輸入框自動(dòng)聚焦的指令。

<body>
  <script type="module">
    import { createApp } from 'https://unpkg.com/petite-vue?module'
    
    const autoFocus = (ctx) => {
      ctx.el.focus()
    }

    createApp().directive('auto-focus', autoFocus).mount()
  </script>

  <div v-scope>
    <input v-auto-focus />
  </div>
</body>

內(nèi)置指令

  • v-model
  • v-if / v-else / v-else-if
  • v-for
  • v-show
  • v-html
  • v-text
  • v-pre
  • v-once
  • v-cloak

注意:v-for 不需要key,另外 v-for 不支持 深度解構(gòu)

<body>
  <script type="module">
    import { createApp } from 'https://unpkg.com/petite-vue?module'
    
    createApp({
      userList: [
        { name: '張三', age: { a: 23, b: 24 } },
        { name: '李四', age: { a: 23, b: 24 } },
        { name: '王五', age: { a: 23, b: 24 } }
      ]
    }).mount()
  </script>

  <div v-scope>
    <!-- 支持 -->
    <li v-for="{ age } in userList">
      {{ age.a }}
    </li>
    <!-- 不支持 -->
    <li v-for="{ age: { a } } in userList">
      {{ a }}
    </li>
  </div>
</body>

不支持

為了更輕量小巧,petite-vue 不支持以下特性:

  • ref()、computed
  • render函數(shù),因?yàn)閜etite-vue 沒(méi)有虛擬DOM
  • 不支持Map、Set等響應(yīng)類型
  • Transition, KeepAlive, Teleport, Suspense
  • v-on="object"
  • v-is &
  • v-bind:style auto-prefixing

總結(jié)

以上就是對(duì) petite-vue 的一些簡(jiǎn)單介紹和使用,拋磚引玉,更多新的探索就由你們?nèi)グl(fā)現(xiàn)了。

總的來(lái)說(shuō),prtite-vue 保留了 Vue 的一些基礎(chǔ)特性,這使得 Vue 開(kāi)發(fā)者可以無(wú)成本使用,在以往,當(dāng)我們?cè)陂_(kāi)發(fā)一些小而簡(jiǎn)單的頁(yè)面想要引用 Vue 但又常常因?yàn)榘w積帶來(lái)的考慮而放棄,現(xiàn)在,petite-vue 的出現(xiàn)或許可以拯救這種情況了,畢竟它真的很小,大小只有 5.8kb,大約只是 Alpine.js 的一半。

到此這篇關(guān)于尤大大新活petite-vue的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue petite內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue cli3 配置 stylus全局變量的使用方式

    vue cli3 配置 stylus全局變量的使用方式

    這篇文章主要介紹了vue cli3 配置 stylus全局變量的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 在Vue框架中配置Mock服務(wù)器的方法

    在Vue框架中配置Mock服務(wù)器的方法

    在前端開(kāi)發(fā)中,如果需要模擬后端數(shù)據(jù),而又不想開(kāi)發(fā)一個(gè)后端服務(wù)器, 則可以借助mock.js配置一個(gè)后端服務(wù)器來(lái)返回前端需要的數(shù)據(jù),本文將會(huì)分別介紹在Quasar項(xiàng)目和Vite項(xiàng)目中Mock服務(wù)器的配置方法
    2022-12-12
  • 在vue-cli項(xiàng)目中使用bootstrap的方法示例

    在vue-cli項(xiàng)目中使用bootstrap的方法示例

    本篇文章主要介紹了在vue-cli項(xiàng)目中使用bootstrap的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Vue配合iView實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)的示例代碼

    Vue配合iView實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)的示例代碼

    本篇文章主要介紹了Vue配合iView實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • VUE腳手架具體使用方法

    VUE腳手架具體使用方法

    這篇文章主要介紹了VUE腳手架具體使用方法,vue-cli這個(gè)構(gòu)建工具大大降低了webpack的使用難度,小編覺(jué)得不錯(cuò),下面就一起來(lái)了解一下具體使用方法
    2019-05-05
  • element表單驗(yàn)證如何清除校驗(yàn)提示語(yǔ)

    element表單驗(yàn)證如何清除校驗(yàn)提示語(yǔ)

    本文主要介紹了element表單驗(yàn)證如何清除校驗(yàn)提示語(yǔ),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • mint-ui如何自定義messageBox樣式

    mint-ui如何自定義messageBox樣式

    這篇文章主要介紹了mint-ui如何自定義messageBox樣式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue動(dòng)態(tài)組件實(shí)例解析

    Vue動(dòng)態(tài)組件實(shí)例解析

    讓多個(gè)組件使用同一個(gè)掛載點(diǎn),并動(dòng)態(tài)切換,這就是動(dòng)態(tài)組件。這篇文章主要介紹了Vue動(dòng)態(tài)組件 ,需要的朋友可以參考下
    2017-08-08
  • Vue3之組件狀態(tài)保持KeepAlive的簡(jiǎn)單使用

    Vue3之組件狀態(tài)保持KeepAlive的簡(jiǎn)單使用

    這篇文章主要介紹了Vue3之組件狀態(tài)保持KeepAlive的簡(jiǎn)單使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • vue中map()快速使用方法小結(jié)

    vue中map()快速使用方法小結(jié)

    map()函數(shù)是在JS的數(shù)組中定義的,它返回一個(gè)新的數(shù)組,下面這篇文章主要給大家介紹了關(guān)于vue中map()快速使用的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12

最新評(píng)論