首页 > 其他分享 >Vue——属性指令、style和class、条件渲染、列表渲染、事件处理、数据双向绑定、过滤案例

Vue——属性指令、style和class、条件渲染、列表渲染、事件处理、数据双向绑定、过滤案例

时间:2023-06-03 09:11:48浏览次数:41  
标签:事件处理 style Vue console log 渲染 new class

vm对象

<body>
<div id="app">
    <h1>{{name}}</h1>
    <button @click="handleClick">点我</button>
</div>
</body>
<script>
    // 1 写在data或method中的属性或方法,从vm中直接可以点出来
    // 2 method的函数中,如果想使用data或methods中的属性,直接this点名字就可以了
    let vm = new Vue({
        el: '#app',
        data: {
            name: 'xxx',
            age: 19,
        },
        methods: {
            handleClick() {
                console.log(this)
                this.handleClick1()
            },
            handleClick1() {
                console.log(this)
                this.name = '彭于晏'
            }
        }
    })
</script>

函数传参

<body>
<div id="app">
    <h1>函数,可以多传参数,也可以少传参数,都不会报错</h1>
    <button @click="handleClick('xxx')">点我</button>
    <h1>事件对象,调用函数,不传参数,会把当前事件对象,传入,可以不接收,也可以接收</h1>
    <button @click="handleClick2">点我2</button>
    <br>
    <button @click="handleClick3('xxx',$event)">点我3</button>
</div>
</body>
<script>
    let vm = new Vue({
        el: '#app',
        data: {},
        methods: {
            handleClick(name, age) {
                console.log(name, age)
            },
            handleClick2(event) {
                console.log(event)
            },
            handleClick3(name, event) {
                console.log(name)
                console.log(event)
            }
        },
    })
</script>

属性指令

// 标签上   name   id  class  src  href ,height  属性
// 如果这样,name='xx' 就写死了,我们想动态的绑定变量,变量变化,属性的值也变化

//  v-bind:属性名='变量' 可以简写成  :属性名='变量'
<body>
<div id="app">
    <hr>
    <button @click="handleClick">点我变大</button>
    <img :src="url" alt="" :height="h" width="w">
</div>
</body>
<script>
    let vm = new Vue({
        el: '#app',
        data: {
            url: 'https://img2.woyaogexing.com/2023/06/01/11de6aa55c3ccc0fc35ba1ab6e4ae2b5.jpg'
            h: '200px',
            w: '200px'
        },
        methods: {
            handleClick() {
                this.h = '400px'
                this.w = '400px'
            },
        }
    })
</script>

style和class

// style 和 class也是属性,可以使用属性指令  :class=   :style=
// 但是他们比较特殊,单独再讲

    <style>
        .size {
            font-size: 60px;
        }

        .red {
            background-color: red;
        }

        .yellow {
            background-color: yellow;
        }
    </style>
</head>
<body>
<div id="app">
    <h1>class的使用:字符串,数组,对象</h1>
    <button @click="handleClick">点我变样式</button>
    <br>
    <div :class="class_arr">
        我是div
    </div>

    <h1>style的使用:字符串,数组,对象</h1>
    <br>
    <div :style="style_obj">
        我是div
    </div>
</div>
</body>
<script>
    let vm = new Vue({
        el: '#app',
        data: {
            // class 推荐使用 数组方式
            // class 绑定的变量,类型可以是 字符串,数组,对象
            // class_str: 'size', // vm.class_str='size red'
            class_arr:['red'], // 给该变量,追加值,class变化,页面会发生变化:vm.class_arr.push('size')
            // class_obj:{size:true,red:false}  // 对象的用法,必须先提前设置,后期通过修改true或false控制类
            // class_obj:{size:true},  // 但是不能往对象中放之前不存在的值,放不存在的值,没有响应式

            // style 推荐使用 对象形式
            // style_str:'font-size:80px;background-color:green',  // vm.style_str='background-color: red'
            // style_arr: [{'font-size':'60px'}]  // vm.style_arr.push({'background-color':'red'})
            // style_obj:{'font-size':'60px','backend-color':'green'}
            style_obj:{fontSize:'60px',color:'green'},// 省略key值的引号后,要变成驼峰形式
            // 有以下三种写法 Vue.set(this.style_obj,'color','red')或者this.style_obj['color'] = 'pink'或者this.style_obj.fontSize = '20px'
        },
        methods: {
            handleClick() {
                // this.class_obj.yellow = true // 直接放,没有响应式
                Vue.set(this.style_obj,'color','red')  // 这样才有响应式
            }
        }
    })
</script>

条件渲染

<body>
<div id="app">
  <div v-if="score>90&&score<=100">优秀</div>
  <div v-else-if="score>80&&score<=90">良好</div>
  <div v-else-if="score>60&&score<=80">及格</div>
  <div v-else>不及格</div>
</div>
</body>
<script>
  let vm = new Vue({
    el:'#app',
    data:{
      score:92
    },
  })
</script>

列表渲染

// 循环渲染一些数据,比如购物车的数据
// v-for:循环字符串,数组,数字,对象
// 有购物车数据,循环显示在页面中
// 借助于bootstrap + Vue

<div id="app">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <h1 class="text-center">购物车</h1>
                <div class="pull-right">
                    <button class="btn btn-danger" @click="handleClick">加载购物车</button>
                </div>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>id号</th>
                        <th>商品名</th>
                        <th>商品价值</th>
                        <th>商品数量</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="good in good_list">
                        <th>{{good.id}}</th>
                        <td>{{good.name}}</td>
                        <td>{{good.price}}</td>
                        <td>{{good.count}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    let vm = new Vue({
        el:'#app',
        data:{
            good_list:[],
        },
        methods:{
            handleClick(){
                this.good_list = [
                    {'id': 1, 'name': '小汽车', 'count': 2, 'price': 100000},
                    {'id': 2, 'name': '脸盆', 'count': 1, 'price': 23},
                    {'id': 3, 'name': '方便面', 'count': 3, 'price': 6},
                    {'id': 4, 'name': '钢笔', 'count': 4, 'price': 5},
                ]
            }
        }
    })
</script>

v-for循环其他类型

// 数字 字符串 数组 对象
// 看到别人写v-for时,在标签上都会加一个 key 属性,目的是为了提高虚拟dom的替换效率
<el-carousel-item v-for="item in 4" :key="item">
// key的值必须唯一 如果不唯一就报错
    
###### 以后如果数据变了,页面没有发生变化#####
Vue.set(对象, key, value)
Vue.set(数组, 索引, 值)
    
<body>
<div id="app">
  <h1>循环数字</h1>
  <ul>
    <li v-for="(value,index) in number">{{value}}-----------{{index}}</li>
  </ul>
  <h1>循环字符串</h1>
  <ul>
    <li v-for="(value,index) in str">{{value}}-----------{{index}}</li>
  </ul>
  <h1>循环数组</h1>
  <ul>
    <li v-for="(value,index) in arr">{{value}}------------{{index}}</li>
  </ul>
  <h1>循环对象(相对于python key和value是反的)</h1>
  <ul>
    <li v-for="(value,index) in obj">{{value}}------------{{index}}</li>
  </ul>
</div>
</body>
<script>
  let vm = new Vue({
    el:'#app',
    data:{
      number:10,
      str:'XxMa',
      arr:[11,22,33,44,55],
      obj:{name:'xxx',age: 19,gender:'男'}
    }
  })
</script>

双向数据绑定 v-model

// 数据双向绑定---》只有input标签---》v-model 做双向数据绑定
// 咱们之前写的,其实都是数据的单向绑定
		修改js的值,页面变了

<body>
<div id="app">
    <h1>双向数据绑定</h1>
    <p>用户名<input type="text" v-model="username"></p>
    <p>用户名<input type="password" v-model="password"></p>
    <p>
        <button @click="handleSubmit">登录</button>
    </p>
</div>
</body>
<script>
    let vm = new Vue({
        el:'#app',
        data:{
            username:'xxx',
            password:'123',
        },
        methods:{
            handleSubmit(){
                console.log(this.username)
                console.log(this.password)
            }
        }
    })
</script>

事件处理

// 事件指令
	click:点击事件

// input标签的事件
	input:只要输入内容,就会触发
	change:只要输入框发生变化,就会触发
	blur:只要失去焦点,就会触发

<body>
<div id="app">
    <h1>input事件</h1>
    <input type="text" @input="handleInput" v-model="username">-----{{username}}
    <h1>change事件</h1>
    <input type="text" @change="handleChange" v-model="username1">------{{username1}}
    <h1>blur事件</h1>
    <input type="text" @blur="handleBlur" v-model="username2">-------{{username2}}
</div>
</body>
<script>
    let vm = new Vue({
        el:'#app',
        data:{
            username:'xxx',
            username1:'yyy',
            username2:'zzz'
        },
        methods:{
            handleInput(){
                console.log(this.username)
            },
            handleChange(){
                console.log(this.username1)
            },
            handleBlur(){
                console.log(this.username2)
            }
        }
    })
</script>

过滤案例(filter、indexOf)

<body>
<div id="app">
    <input type="text" v-model="search" @input="handleInput">
    <hr>
    <ul>
        <li v-for="item in new_dataList">{{item}}</li>
    </ul>
</div>
</body>
<script>
    let vm = new Vue({
        el:'#app',
        data:{
            search:'',
            dataList: ['a', 'at', 'atom', 'attoo', 'be', 'beyond', 'cs', 'csrf'],
            new_dataList: ['a', 'at', 'atom', 'attoo', 'be', 'beyond', 'cs', 'csrf'],
        },
        methods:{
            // handleInput(){
            //     console.log(this.search)
            //     // 根据用户输入的search,对数组进行过滤,剩下只有包含输入文字的字符串
            //     var _this = this
            //     this.new_dataList = this.dataList.filter(function(item){
            //         if (item.indexOf(_this.search)>=0){
            //             return true
            //         }else {
            //             return false
            //         }
            //     })
            // }

            // handleInput(){
            //     console.log(this.search)
            //     var _this = this
            //     this.new_dataList = this.dataList.filter(function(item){
            //         return item.indexOf(_this.search) >= 0
            //     })
            // }

            handleInput(){
                this.new_dataList = this.dataList.filter(item=>item.indexOf(this.search)>=0)
            }
        }
    })


    // 补充
    // 1 数组的过滤
    // var arr = ['a', 'at', 'atom', 'attoo', 'be', 'beyond', 'cs', 'csrf']
    // // 数组.filter(匿名函数,接收一个参数,函数必须返回true或false,如果返回true,表示这个值保留)
    // var new_arr = arr.filter(function(item){
    //     console.log(item)
    //     if (item.length>3){
    //         return true
    //     }else {
    //         return false
    //     }
    // })
    // console.log(new_arr)

    // 2 判断一个字符串是否在另一个字符串中
    // var s = 'is'
    // var s1 = 'XxMa is me'
    // var res = s1.indexOf(s)  // s的索引位置,如果大于等于0,说明s在s1中
    // console.log(res)

    // 3 过滤出 数组中有at的字符串
    // var arr = ['a', 'at', 'atom', 'attoo', 'be', 'beyond', 'cs', 'csrf']
    // var search = 'at'
    // var new_arr = arr.filter(function(item){
    //     if (item.indexOf(search)>=0){
    //         return true
    //     }else {
    //         return false
    //     }
    // })
    // console.log(new_arr)
</script>

标签:事件处理,style,Vue,console,log,渲染,new,class
From: https://www.cnblogs.com/XxMa/p/17453283.html

相关文章

  • 浏览器渲染HTML的步骤
       HTML被HTML解析器解析成DOMTree,CSS则被CSS解析器解析成CSSOMTree`。DOMTree和CSSOMTree解析完成后,被附加到一起,形成渲染树(RenderTree)。节点信息计算(重排),这个过程被叫做Layout(Webkit)或者Reflow(Mozilla)。即根据渲染树计算每个节点的几何信息生成布局......
  • 安卓中的theme和style
    资料Android中Theme主题和Style样式使用介绍总结样式和主题背景Androidstyling:themesvsstylesAndroidstyling:commonthemeattributesAndroidstyling:commonthemeattributesAndroidStyling:preferthemeattributes......
  • vue基础用法-内容渲染指令
    1.指令的概念指令(Directives)是vue为开发者提供的模板语法,用于辅助开发者渲染页面的基本结构。vue中的指令按照不同的用途可以分为如下6大类:内容渲染指令属性绑定指令事件绑定指令双向绑定指令条件渲染指令列表渲染指令注意:指令是vue开发中最基础、最常用、最简单的知识......
  • 输入URL到页面渲染过程
    1.输入URL;2.浏览器查找当前URL是都存在缓存并且比较是否过期;3.DNS解析对呀的IP地址;首先会在本地的hosts文件中查看,若没有则需要通过DNS(域名分布系统)服务器查找;4.根据IP地址建立TCP链接(进行三次握手);5.发起http请求;6.服务器处理请求,浏览器接收http响应(HTML文件);7.关闭TCP连接......
  • webgl 渲染带透明通道的视频(threeJS)
    首先,你需要一个这样的视频 或者一个这样的视频 webgl渲染可以用three.js,上下叠加的代码如下:import*asTHREEfrom'three';letvideoWidth=540;//视频实际的宽度letvideoHeight=540;//原视频实际的高度的一般//定义渲染器varrenderer=new......
  • Focus On 3D Terrain Programming三维地形渲染-Trent Polack-2003
    前言:你有多少次访问过你最喜欢的编程论坛或邮件列表,并对大量关于地形渲染算法的帖子感到惊讶,这些帖子似乎从各个角度向你袭来?地形渲染似乎是当今业余程序员最喜欢的主题;它是一个很好的门户网站,可以了解更高要求的问题及其解决方案。然而,地形渲染决不是一个简单的问题,特定的解决方......
  • Wpf基础入门——模板和样式(Template&Style)
    本篇文章学习于:刘铁猛老师《深入浅出WPF》什么是模板?在WPF中,通过引入模板(Template)微软将数据和算法的“内容”与“形式”解耦了。WPF中的Template分为两大类:ControlTemplate是算法内容的表现形式,一个控件怎样组织其内部结构才能让它更符合业务逻辑、让用户操作起来更舒服就......
  • 关于动态渲染的组件watch监听不到传入的props的问题
    watch:{propsTime:{handler(newValue,oldValue){console.log('props',newValue)this.getOverviewData()},//这里增加了一个immediate属性,说明监听到props传参后立即先去执行handler方法immediate:true,}......
  • [重读经典论文]RepVGG: Making VGG-style ConvNets Great Again
    1.参考视频:14.1RepVGG网络讲解博客:RepVGG网络简介2.主要内容2.1.与其他网络对比如下图所示,RepVGG无论是在精度还是速度上都已经超过了ResNet、EffcientNet以及ReNeXt等网络。2.2.创新点,结构重参数化在训练时,使用一个类似ResNet-style的多分支模型,而推理时转化成VGG-st......
  • 延迟渲染
     基础概念概念缩写中文名解析RenderTargetRT渲染目标/渲染纹理一种位于GPU显存的纹理缓冲区,可用作颜色写入和混合目标GeometryBufferGBuffer几何缓冲区一种特殊的RenderTarget,延迟着色技术的几何数据存储区RenderingPath-......