这篇“petite-vue如何实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“petite-vue如何实现”文章吧。
从名字来看可以知道 petite-vue 是一个 mini 版的vue,大小只有5.8kb,可以说是非常小了。petite-vue 是 Vue 的可替代发行版,针对渐进式增强进行了优化。它提供了与标准 Vue 相同的模板语法和响应式模型:
大小只有5.8kb
Vue 兼容模版语法
基于DOM,就地转换
响应式驱动
<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 绑定数据,这样一个简单的计数器就实现了。
了解过 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 函数可以接受一个对象,类似于我们平时使用 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>
可以监听每个元素的生命周期事件。
<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 里,组件可以使用函数的方式创建,通过template可以实现复用。
<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>
借助 reactive 响应式 API 可以很轻松的创建全局状态管理
<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>
这里来简单实现一个输入框自动聚焦的指令。
<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>
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 不支持 深度解构
<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函数,因为petite-vue 没有虚拟DOM
不支持Map、Set等响应类型
Transition, KeepAlive, Teleport, Suspense
v-on="object"
v-is &
v-bind:style auto-prefixing
以上就是关于“petite-vue如何实现”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。