• Alpine.js 精简重


    建议有 js 基础,先阅读官网文档,如果您会 vue 类似框架,上手会更快

    https://alpinejs.dev

    js 代码中可以使用 Alpine.sore 定义全局数据

    1. Alpine.store('tabs', {
    2. current: 'first',
    3. items: ['first', 'second', 'third'],
    4. })

    x-text 可以运算任何 js 表达式

    <span x-text="1 + 2">span>

    @submit.prevent 阻止浏览器提交表单

    1. <form @submit.prevent="...">...form>
    2. <form @submit.prevent="console.log('submitted')" action="/foo">
    3. <button>Submitbutton>
    4. form>

     .stop 也是阻止事件,以下点击按钮不会触发

    1. <div @click="console.log('I will not get logged')">
    2. <button @click.stop>Click Mebutton>
    3. div>

    $watch 用于监听更改变化的值,需要使用回调添加第二个参数访问变化之前的值

    1. <div x-data="{ open: false }" x-init="$watch('open', value => console.log(value))">
    2. <div x-data="{ open: false }" x-init="$watch('open', (value, oldValue) => console.log(value, oldValue))">
    3. <button @click="open = ! open">Toggle Openbutton>
    4. div>

    x-effect  用于监听值并且可以使用表达式

    <div x-data="{ open: false }" x-effect="console.log(open)">

    x-bind 的简写,例如 :class 条件简写

    1. <div :class="show ? '' : 'hidden'">
    2. <div :class="show || 'hidden'">
    3. <div :class="closed ? 'hidden' : ''">
    4. <div :class="closed && 'hidden'">

    $event 事件对象,和原生 js 里的 event 一样

    1. <button @click="alert($event.target.getAttribute('message'))" message="Hello World">Say Hi
    2. #代码调用写法
    3. <button @click="handleClick">...button>
    4. <script>
    5. function handleClick(e) {
    6. // Now you can access the event object (e) directly
    7. }
    8. script>

    .outside 很方便,点击元素之外触发

    1. <div x-data="{ open: false }">
    2. <button @click="open = ! open">Togglebutton>
    3. <div x-show="open" @click.outside="open = false">
    4. Contents...
    5. div>
    6. div>

    .window 添加到事件后,监听的是根对象,以下示例将侦听页面任何位置按下的转义键

    <div @keyup.escape.window="...">...div>

    .once 添加到事件后只调用一次

    <button @click.once="console.log('I will only log once')">...button>

    .debounce 延迟,防抖动,比如输入框填写文字后一定时间后触发

    <input @input.debounce.500ms="fetchResults">

     .throttle 和 debounce  一样,但是只触发一次,可以自定义时间

    <div @scroll.window.throttle="handleScroll">...div>

    .self 确保点击的的是自己,而不是子元素

    1. <button @click.self="handleClick">
    2. Click Me
    3. <img src="...">
    4. button>

    x-model 适用于以下 input

    1. <input type="text">
    2. <textarea>
    3. <input type="checkbox">
    4. <input type="radio">
    5. <select>

     x-model.lazy 焦点离开触发,比如验证注册用户名和邮箱是否重复以及符合条件

    1. <input type="text" x-model.lazy="username">
    2. <span x-show="username.length > 20">The username is too long.span>

    x-model 会将任何数据转为字符串类型,可以添加 x-model.number 转换为数字类型

    1. <input type="text" x-model.number="age">
    2. <span x-text="typeof age">span>

    .debounce 同样防抖动,比如搜索框延迟请求数据,可自定义时间

    .throttle 同样可用

    <input type="text" x-model.debounce="search">

    通过在元素商添加 x-ref 值,可以在别处轻松的设置和获取 x-model 值

    1. el._x_model.get() (返回绑定属性的值)
    2. el._x_model.set() (设置绑定属性的值)
    3. <div x-data="{ username: 'calebporzio' }">
    4. <div x-ref="div" x-model="username">div>
    5. <button @click="$refs.div._x_model.set('phantomatrix')">
    6. Change username to: 'phantomatrix'
    7. button>
    8. <span x-text="$refs.div._x_model.get()">span>
    9. div>

    x-for 只能包含一个根目录,参考以下代码

    1. 无效
    2. <template x-for="color in colors">
    3. <span>The next color is span><span x-text="color">
    4. template>
    5. 有效
    6. <template x-for="color in colors">
    7. <p>
    8. <span>The next color is span><span x-text="color">
    9. p>
    10. template>

    x-ignore 忽略 alpinejs 的特性,以下代码中的中间 div 的 x-text 将不会生效

    1. <div x-data="{ label: 'From Alpine' }">
    2. <div x-ignore>
    3. <span x-text="label"></span>
    4. </div>
    5. </div>

    添加 x-ref 并在其他元素使用 $refs 来调用它,$refs 指向的是 x-ref = 的元素

    1. <button @click="$refs.text.remove()">Remove Text</button>
    2. <span x-ref="text">Hello 👋</span>

    x-cloak 直到 alpinejs 设置了值才显示,防止页面加载后但在 alpinejs 加载前闪现,建议使用 x-if 代替此写法

    必须先添加 css

    [x-cloak] { display: none !important; }
    <span x-cloak x-text="message"></span>

    x-teleport 追加元素到指定元素的后面,相当于 document.querySelector('boody').append

    可循环嵌套,注意循环嵌套追加到同一元素之后的是同一层级

    $el 指向当前 dom 节点

    <button @click="$el.innerHTML = 'Hello World!'">Replace me with "Hello World!"

    $dispatch 在元素之间传递事件,还可以在不同的 x-data 组件内通信传递

    1. <div @notify="alert('Hello World!')">
    2. <button @click="$dispatch('notify')">
    3. Notify
    4. button>
    5. div>
    6. #带数据传递
    7. <div @notify="alert($event.detail.message)">
    8. <button @click="$dispatch('notify', { message: 'Hello World!' })">
    9. Notify
    10. button>
    11. div>
    12. #同级别层次用
    13. <div x-data>
    14. <span @notify.window="...">span>
    15. <button @click="$dispatch('notify')">Notifybutton>
    16. div>

     $nextTick 事件更新后执行表达式,它返回的是一个 promise 对象

    1. 点击后 button 按钮文字由 hello 变成 Hello World!,点击之后控制台输出的是 Hello World!
    2. <div x-data="{ title: 'Hello' }">
    3. <button
    4. @click="
    5. title = 'Hello World!';
    6. $nextTick(() => { console.log($el.innerText) });
    7. "
    8. x-text="title"
    9. >button>
    10. div>
    11. 另一种写法
    12. <div x-data="{ title: 'Hello' }">
    13. <button
    14. @click="
    15. title = 'Hello World!';
    16. await $nextTick();
    17. console.log($el.innerText);
    18. "
    19. x-text="title"
    20. >button>
    21. div>

    $data 魔法属性,可以在data以及嵌套区域获取 x-data 属性值

    1. <div x-data="{ greeting: 'Hello' }">
    2. <div x-data="{ name: 'Caleb' }">
    3. <button @click="sayHello($data)">Say Hello</button>
    4. </div>
    5. </div>
    6. <script>
    7. function sayHello({ greeting, name }) {
    8. alert(greeting + ' ' + name + '!')
    9. }
    10. </script>

    x-text 等可以使用异步函数

    1. async function getLabel() {
    2. let response = await fetch('/api/label')
    3. return await response.text()
    4. }
    5. <span x-text="await getLabel()"></span>
    6. 或者直接写为
    7. <span x-text="getLabel"></span>

     

    ac446f46-5842-40c4-b895-a0afb493058f
  • 相关阅读:
    荧光素标记大鼠甲状腺滤泡上皮细胞FRTL-5,荧光素FITC标记的FRTL-5细胞,FITC-FRTL-5
    详解 Flink 的状态管理
    LeetCode刷题---最长回文子串
    Java IO 中常用的目录和文件操作,用到的时候从这里拷贝就行了
    iOS平台 | 华为AGC的App Linking分析--Universal Link方式
    Unity2023.1.19_DOTS_JobSystem
    如何在CPU上进行高效大语言模型推理
    如何利用Python实现熵权法?
    前端访问geoserver服务发生跨域的解决办法,以及利用html2canvas下载绘制的地图
    设计模式之工厂模式(学习笔记)
  • 原文地址:https://blog.csdn.net/myarche/article/details/133838985