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

VUE.JS 实践 第三章

目录

一、内容渲染指令

二、属性绑定指令

三、事件绑定指令

四、双向数据绑定指令

五、条件渲染指令

六、列表渲染指令


一、内容渲染指令

常见的内容渲染指令如下。

1、v-text

代码:创建src\components\VText.vue文件

<template> <div v-text="message"></div> </template> <script setup> const message = '<span>咬定青山不放松,立根原在破岩中</span>' </script>

运行结果:

vue中 插值表达式 {{ }} 和 v-text 用法区别: https://blog.csdn.net/wakaka112233/article/details/106378750

2、v-html

代码:创建src\components\VHtml.vue文件

<template> <div v-html="html"></div> </template> <script setup> const html = '<strong>千磨万击还坚劲,任尔东西南北风</strong>' </script>

运行结果

3、v-text 和v-html 的区别,

<template> <!-- 根节点只能有一个,所有内容包在一个 div 里 --> <div> <p v-text="message"></p> <div v-html="html"></div> <div v-html="html1"></div> </div> </template> <script setup> // 纯文本:v-text 会把标签当字符串输出 const message = '<span>咬定青山不放松,立根原在破岩中</span>' // v-html 会解析渲染HTML标记语言 const html = '<strong>千磨万击还坚劲,任尔东西南北风</strong>' const html1 = '<span style="color:red">千磨万击还坚劲,任尔东西南北风</span>' </script>

输出结果

二、属性绑定指令

演示v-bind的使用方法

代码:创建src\components\VBind.vue文件

<template> <p><input type="text" v-bind:placeholder="username"></p> <p><input type="password" :placeholder="password"></p> </template> <script setup> const username = '请输入用户名' const password = '请输入密码' </script>

运行结果

三、事件绑定指令

演示v-on的使用方法

<template> <button @click="showInfo">输出信息</button> <button @click="showInfo1">输出信息1</button> <p>{{ msg }}</p> </template> <script setup> const showInfo = () => { console.log('欢迎来到Vue.js的世界!') } import { ref } from 'vue' const msg = ref('') const showInfo1 = () => { msg.value = '欢迎来到Vue.js的世界!' } setTimeout(() => { msg.value = '欢迎来到Vue.js的世界1111!' }, 5000); </script>

运行结果

四、双向数据绑定指令

v-model是什么?

  • Vue 专门用来做表单双向绑定的指令
  • 数据变 → 视图变;视图变 → 数据变
  • 适用于:input、textarea、select 等表单元素

演示v-model的使用方法

<template> <!-- 视图变化,数据变化 --> 请输入姓名:<input type="text" v-model="username"> <div>姓名是:{{ username }} <p><button @click="sjbh">数据变化,视图改变</button></p> </div> </template> <script setup> import { ref } from 'vue' const username = ref('zhangsan') const sjbh=()=>{ username.value='李四' } </script>

运行结果

.number 用法演示

<template> <!-- 视图变化,数据变化 --> 请输入姓名:<input type="text" v-model="username"> <div>姓名是:{{ username }} <p><button @click="sjbh">数据变化,视图改变</button></p> <!-- .未使用number属性用法的结果 --> <p>未使用number属性用法的结果</p> <input type="text" v-model="n1"> + <input type="text" v-model="n2">= {{ n1 + n2 }} <!-- .使用number属性用法的结果 --> <p>使用number属性用法的结果</p> <input type="text" v-model.number="n3"> + <input type="text" v-model.number="n4"> = {{ n3 + n4 }} </div> </template> <script setup> import { ref } from 'vue' const username = ref('zhangsan') const sjbh=()=>{ username.value='李四' } // .number属性用法 const n1 = ref(1) const n2 = ref(2) const n3 = ref(1) const n4 = ref(2) </script>

运行结果

五、条件渲染指令

1、v-if

2. v-show

<template> <p v-if="flag">通过v-if控制的元素</p> <p v-show="flag">通过v-show控制的元素</p> <button @click="flag = !flag">显示/隐藏</button> </template> <script setup> import { ref } from 'vue' const flag = ref(true) </script>

运行结果

六、列表渲染指令

5.1 演示使用v-for根据一维数组渲染列表

<template> <div v-for="(item, index) in list" :key="index"> 索引是:{{ index }} --- 元素的内容是:{{ item }} </div> </template> <script setup> import { ref,onMounted,reactive } from 'vue' // reactive 声明普通数组 / 基础类型变量是错误用法,会导致数据无法响应式更新。推荐用ref const list = ref(['HTML', 'CSS', 'JavaScript']) // 组件挂载后修改数组 onMounted(() => { // 正确:ref 声明的数组直接修改,页面会自动更新 list.value.push('Vue') }) </script>

运行结果

5.2 演示使用v-for根据对象数组渲染列表

<template> <div v-for="item in list" :key="item.id"> id是:{{ item.id }} --- 元素的内容是:{{ item.message }} </div> </template> <script setup> import { reactive } from 'vue' const list = reactive([ { id: 1, message: '梅', }, { id: 2, message: '兰', }, { id: 3, message: '竹', }, { id: 4, message: '菊', } ]) </script>

运行结果

5.3演示使用v-for根据对象渲染列表

<template> <div v-for="(value, name) in user" :key="name"> 属性名是:{{ name }} --- 属性值是:{{ value }} </div> </template> <script setup> import { reactive } from 'vue' const user = reactive({ id: 11, name: '小明', gender: '男' }) </script>

运行结果

http://www.jsqmd.com/news/536886/

相关文章:

  • 揭秘AI专著生成秘诀!掌握这些工具,轻松打造专业学术专著
  • SQL 中聚集函数(Aggregate Functions)与 `ANY`/`ALL` 谓词的核心用法、语义等价关系及实际应用要点
  • 在 SAP 中,Cost Object(成本对象) 是归集、控制与结算成本的核心载体,其设置与定义分为主数据创建(前台操作)和后台配置(SPRO)两大场景,不同类型成本对象路径不同
  • Java中的继承:从入门到精通
  • LD8035显示驱动芯片技术文档为何无法生成?
  • MedGemma-X惊艳效果:上传一张胸片,获得多维度结构化诊断分析
  • PyTorch 2.8镜像应用场景:广告公司定制化AI创意生成私有平台案例
  • ChatTTS与OpenVoice本地部署实战:从语音合成到高效推理的完整指南
  • Llama-3.2V-11B-cot实战教程:上传→提问→展开推演→导出结论四步闭环
  • ABAQUS有限元模型:基于CEL算法的斜桩锤击入土模拟
  • 现代C++ | 基础革命特性
  • 吃透 Android 布局资源:从 Chapter2 实战项目看懂四大核心布局
  • 国家金融监督管理总局地市级分支局计算机岗之日常运维:从基础到进阶的全面解析
  • 无源晶振如何用
  • PCB画板时的层数设置
  • Axios + Vue 错误处理规范:中后台项目实战,统一捕获系统 / 业务 / 接口异常|API 与异步请求规范篇
  • 2026 本科论文 AI 工具榜单: 9 款神器,搞定从选题到答辩全流程
  • 边缘AI网关搭建:YOLO12-N在智能交通摄像头中的低延迟部署方案
  • Qwen3.5-4B-Claude-Opus应用场景:在线教育平台嵌入式推理助手
  • 未来运维工程师的核心竞争力,可能跟你想的不太一样
  • OpenClaw自动化办公:用GLM-4.7-Flash实现邮件自动整理与回复
  • pnpm 使用教程
  • 利用DeepSeek接口构建高并发智能客服系统的架构设计与性能优化
  • C语言实现多态相关话题
  • 答辩逆袭指南:Paperxie AI PPT 如何让论文答辩从 “手忙脚乱” 变 “从容出彩”
  • RTX4090D显存优化:OpenClaw长文本处理对接Qwen3-32B实测
  • weixin258基于微信小程序的课堂点名系统springboot(文档+源码)_kaic
  • 【大模型学习】常见AI工作流框架组合
  • 用ABAQUS玩转液压油缸模拟:基于CEL算法的加载模型
  • H3CNE--17.DHCP和DHCP中继代理