溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

petite-vue怎么使用

發(fā)布時間:2022-03-24 11:17:50 來源:億速云 閱讀:629 作者:iii 欄目:web開發(fā)

這篇文章主要介紹“petite-vue怎么使用”,在日常操作中,相信很多人在petite-vue怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”petite-vue怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

前言

petite-vue怎么使用

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

簡介

petite-vue怎么使用

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

  • 大小只有5.8kb

  • Vue 兼容模版語法

  • 基于DOM,就地轉(zhuǎn)換

  • 響應式驅(qū)動

上活

下面對 petite-vue 的使用做一些介紹。

簡單使用

<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>

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

了解過 Alpine.js 這個框架的同學看到這里可能有點眼熟了,兩者語法之間是很像的。

<!--  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ù)可以接受一個對象,類似于我們平時使用 data 和 methods 一樣,這時 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)聽每個元素的生命周期事件。

<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)建,通過template可以實現(xiàn)復用。

<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>

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

全局狀態(tài)管理

借助 reactive 響應式 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>

自定義指令

這里來簡單實現(xiàn)一個輸入框自動聚焦的指令。

<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ù),因為petite-vue 沒有虛擬DOM

  • 不支持Map、Set等響應類型

  • Transition, KeepAlive, Teleport, Suspense

  • v-on="object"

  • v-is &

  • v-bind:style auto-prefixing

到此,關于“petite-vue怎么使用”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI