vue指令 ╰半夏微凉° 2023-06-01 12:50 5阅读 0赞 ### 1.常用内置指令 ### v:text : 更新元素的 textContent v-html : 更新元素的 innerHTML v-if : 如果为true, 当前标签才会输出到页面 v-else: 如果为false, 当前标签才会输出到页面 v-show : 通过控制display样式来控制显示/隐藏 v-for : 遍历数组/对象 v-on : 绑定事件监听, 一般简写为@ v-bind : 强制绑定解析表达式, 可以省略v-bind v-model : 双向数据绑定 ref : 为某个元素注册一个唯一标识, vue对象通过$refs属性访问这个元素对象 v-cloak : 使用它防止闪现表达式, 与css配合: [v-cloak] { display: none } <div id="example"> <p ref="content">abcdefg</p> <p v-text="msg"></p> <!--p.textContent=msg--> <p v-html="msg"></p> <!--p.innerHtml=msg--> <button @click="hint">提示</button> <p v-cloak>{ {msg}}</p> <!--解析msg之前有[v-cloak],解析之后无[v-cloak]指令--> </div> <scrip type="text/javascript" src="../js/vue.js"></script> <scrip type="text/javascript"> alert("------") //解析msg数据之前弹窗 new Vue({ el: "#example", data: { msg: "message", }, methods: { hint () { alert(this.$refs.content.textContent) } } }) </script> ### 2.自定义指令 ### 1. 注册全局指令 Vue.directive('my-directive', function(el, binding){ el.innerHTML = binding.value.toupperCase() }) 2. 注册局部指令 directives : { 'my-directive' : { bind (el, binding) { el.innerHTML = binding.value.toupperCase() } } } 3. 使用指令 v-my-directive='xxx' <!-- 需求: 自定义2个指令 1. 功能类型于v-text, 但转换为全大写 v-upper-text 2. 功能类型于v-text, 但转换为全小写 v-lower-text --> <div id="test1"> <p v-upper-text="msg1"></p> <p v-lower-text="msg1"></p> </div> <div id="test2"> <p v-upper-text="msg2"></p> <p v-lower-text="msg2"></p> </div> <scrip type="text/javascript" src="../js/vue.js"></script> <scrip type="text/javascript"> // 注册一个全局指令 // el: 指令所在的标签对象 // binding: 包含指令相关数据的容器对象 Vue.directive("upper-text",function (el,binding) { console.log(el,binding) el.textContent = binding.value.toUpperCase() }) new Vue({ el: "#test1", data: { msg1: "NBA I Love This Game!" }, directives: { //注册局部指令:只在当前vm管理范围内有效 "lower-text"(el,binding){ //es6写法 原本写法:"lower-text": function(el,binding){} el.textContent = binding.value.toLowerCase() } } }) new Vue({ el: "#test2", data: { msg2: "Just Do It!" } }) </script> 转载于:https://www.cnblogs.com/itzlg/p/11315835.html
相关 Vue指令 Vue指令 * v-text * v-html * v-show * v-if * v-else * v-else-if * v-for ... 迈不过友情╰/ 2024年04月17日 05:59/ 0 赞/ 132 阅读
相关 vue 指令 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> 谁借莪1个温暖的怀抱¢/ 2023年11月05日 00:52/ 0 赞/ 160 阅读
相关 Vue指令 一、Vue指令的规定 1. 所有的Vue指令必须以v-开头,后面带命令动词。 2. 指令的参数:v-指令动词.参数 3. 指令的修饰符:v-指令动词:修饰符 二 阳光穿透心脏的1/2处/ 2023年06月25日 11:29/ 0 赞/ 68 阅读
相关 Vue指令 v-text v-text的作用跟插值表达式是一样的,但是不同的地方在于:v-text会覆盖元素中原本的内容,插值表达式只会替换自己的这个占位符,不会把整个元素的内容清空 男娘i/ 2023年06月09日 07:59/ 0 赞/ 62 阅读
相关 Vue 指令 基本指令 (1)v-bind指令 语法格式1:单个语法格式 v-bind:attributeName=variable 以下是HTML代码: < 矫情吗;*/ 2023年06月06日 08:22/ 0 赞/ 59 阅读
相关 vue指令 总结 v-text v-text是一个渲染文本的指令 不能解析标签 会把标签当成字符串渲染在页面上 v-html v-html 也是一个渲染 电玩女神/ 2023年02月22日 13:56/ 0 赞/ 76 阅读
相关 vue指令 vue指令 一个正在努力爱好运动的前端 座右铭:越努力越幸运,越运动越健康,热爱编程,热爱运动。 -------------------- 文章目录 朱雀/ 2022年12月22日 06:12/ 0 赞/ 182 阅读
相关 vue指令 vue介绍 1.作者 尤雨溪(vue) \[阿里\] 3.0 2.vue概念 官网:[Vue.js][] 渐进式 JavaScript 框架 渐进式:主 偏执的太偏执、/ 2022年09月16日 06:05/ 0 赞/ 212 阅读
相关 Vue指令 本文笔记基于「千古壹号」的GitHub项目:https://github.com/qianguyihao/web 非商业用途自由转载,保持署名,注明出处! ------- 待我称王封你为后i/ 2021年11月17日 13:10/ 0 赞/ 480 阅读
相关 Vue指令 Vue指令 1. watch监听 在vue中watch就是时时刻刻的监听绑定的这个data模型数据,当这个数据发生变化的时候,watch就会被触发,自动的执行其中的 £神魔★判官ぃ/ 2021年07月24日 19:31/ 0 赞/ 570 阅读
还没有评论,来说两句吧...