当前位置: 首页 > news >正文

vue入门:指令

文章目录

  • vue的内置指令
    • 说明:
  • 自定义指令

vue的内置指令

Vue 指令的本质是:

  • 声明式的 DOM 操作接口(隐藏底层 JavaScript 代码)。
  • 响应式数据的绑定媒介(连接数据和视图)。
  • 模板编译的标记(最终转换为渲染函数逻辑)。
  • 可扩展的语法糖(简化复杂操作,如 v-model)。

通过指令,Vue 实现了 数据驱动视图 的核心思想,让开发者更专注于业务逻辑而非手动操作 DOM。

指令名称简写形式描述示例
v-bind:动态绑定属性或组件 prop:src="url"v-bind:class="cls"
v-on@绑定事件监听器@click="handleClick"v-on:input
v-model双向数据绑定(表单输入和组件)v-model="message"
v-for列表渲染(循环生成元素)v-for="item in items"
v-if条件渲染(根据条件销毁/创建元素)v-if="isVisible"
v-show显示/隐藏元素(通过 CSS 的 display 控制)v-show="hasError"
v-html输出原始 HTML(注意 XSS 风险)v-html="rawHtml"
v-text更新元素的 textContent(覆盖内容)v-text="message"
v-pre跳过该元素及其子元素的编译(显示原始 Mustache 标签)<div v-pre>{{ 不会被编译 }}</div>
v-cloak隐藏未编译的 Mustache 标签(需配合 CSS 使用)[v-cloak] { display: none }
v-once一次性渲染(元素/组件只渲染一次,后续数据变化不更新)<span v-once>{{ staticText }}</span>
v-slot#定义插槽模板(用于具名插槽或作用域插槽)<template #header>...</template>

说明:

  1. v-bindv-on:最常用的简写指令,: 用于动态绑定,@ 用于事件监听。
  2. v-model:语法糖,等价于 v-bind:value + v-on:input 的组合。
  3. v-slot:在 Vue 3 中简写为 #,常用于具名插槽或作用域插槽。
  4. 特殊指令:如 v-prev-cloakv-once 用于优化或控制编译过程。

示例

<template>
  <div>
    
    <h2>v-text</h2>
    <div v-text="'hello vue'">hello world</div>
    
    <h2>v-html</h2>
    <div v-html="'<span style=&quot;color: red&quot;>hello vue</span>'">
      hello world
    </div>
    
    <h2>v-show</h2>
    <div v-show="show">hello vue</div>
    <button @click="show = !show">change show</button>
    
    <h2>v-if v-esle-if v-else</h2>
    <div v-if="number === 1">hello vue {{ number }}</div>
    <div v-else-if="number === 2">hello world {{ number }}</div>
    <div v-else>hello geektime {{ number }}</div>
    
    <h2>v-for v-bind</h2>
    <div v-for="num in [1, 2, 3]" v-bind:key="num">hello vue {{ num }}</div>
    
    <h2>v-on</h2>
    <button v-on:click="number = number + 1">number++</button>
    
    <h2>v-model</h2>
    <input v-model="message"/>
    
    <h2>v-pre</h2>
    <div v-pre>{{ this will not be compiled }}</div>
    
    <h2>v-once</h2>
    <div v-once>
      {{ number }}
    </div>
    
  </div>
</template>
<script>
export default {
  data: function () {
    this.log = window.console.log;
    return {
      show: true,
      number: 1,
      message: "hello"
    };
  }
};
</script>

自定义指令

<template>
  <div>
    <button @click="show = !show">
      销毁
    </button>
    <!--自定义指令 v-append-text -->
    <button v-if="show" v-append-text="`hello ${number}`" @click="number++">
      按钮
    </button>
  </div>
</template>
<script>
export default {
  directives: {
    appendText: {
      bind() {
        console.log("bind");
      },
      inserted(el, binding) {
        el.appendChild(document.createTextNode(binding.value));
        console.log("inserted", el, binding);
      },
      update() {
        console.log("update");
      },
      componentUpdated(el, binding) {
        el.removeChild(el.childNodes[el.childNodes.length - 1]);
        el.appendChild(document.createTextNode(binding.value));
        console.log("componentUpdated");
      },
      unbind() {
        console.log("unbind");
      }
    }
  },
  data() {
    return {
      number: 1,
      show: true
    };
  }
};
</script>

相关文章:

  • 蓝桥杯 2025 C++组 省 B 题解
  • 面试算法高频05-bfs-dfs
  • 科技赋能记忆共生-郑州
  • 【Java学习笔记】Java第一课,梦开始的地方!!!
  • (八)lerobot开源项目扩展so100的仿真操控(操作记录)
  • 【NIO番外篇】之组件 Channel
  • 《车辆人机工程-》实验报告
  • Linux进程替换与自定义shell详解
  • redisson的unlock方法
  • 行星际激波在日球层中的传播:Propagation of Interplanetary Shocks in the Heliosphere (第一部分)
  • GO语言入门:字符串处理1(打印与格式化输出)
  • Embedding质量评估、空间塌缩、 Alignment Uniformity
  • 【数据结构_5】链表(模拟实现以及leetcode上链表相关的题目)
  • 【AI】SpringAI 第一弹:SpringAI 的兴起介绍
  • NR 5G中的N5接口
  • 考研单词笔记 2025.04.13
  • 达梦数据库-学习-21-某表的空间占用过大处理过程
  • 【区块链+ 人才服务】龙岩市区块链实训实验室 | FISCO BCOS 应用案例
  • DPP推荐引擎架构升级演进之路|得物技术
  • 从红黑树到哈希表:原理对比与典型场景应用解析(分布式以及布隆过滤器)
  • 武契奇目前健康状况稳定,短期内将暂停日常工作
  • 长三角铁路今日预计发送418万人次,持续迎来出行客流高峰
  • 生命与大海相连:他在300多米的深海行走,在沉船一线打捞救援
  • 解放日报:浦东夯实“热带雨林”式科创生态
  • 街区党支部书记们亮出治理实招,解锁“善治街区二十法”
  • 中国武术协会原主席张耀庭逝世,曾促成电影《少林寺》拍摄