首页 > 其他分享 >vue之箭头函数

vue之箭头函数

时间:2023-04-06 20:14:10浏览次数:33  
标签:function vue console 函数 item 箭头 let

目录

说明

当在一个方法(函数)里面再定义一个方法(函数)时,内部的方法的this指向会有问题,如下:

<body>
<div id="app">
    <h1>{{l1}}</h1>
    <button @click="clickFunc">点我</button>
</div>
</body>
<script>
    vm = new Vue({
        el:'#app',
        data:{
            myText: 'ab',
            l1: ['a', 'ab', 'atd', 'b', 'd']
        },
        methods:{
            clickFunc(){
                this.l1 = this.l1.filter(function (item){
                    if(item.indexOf(this.myText) >= 0){
                        return true
                    }else{
                        return false
                    }
                })
            }
        }
    })
</script>

如图,此时点击按钮没有匹配上任何内容
image

解决方法一 重新定义this

第一种解决方法是,在内部函数外重新定义一下this,例如

<script>
    vm = new Vue({
        el:'#app',
        data:{
            myText: 'ab',
            l1: ['a', 'ab', 'atd', 'b', 'd']
        },
        methods:{
            clickFunc(){
                let _this=this
                this.l1 = this.l1.filter(function (item){
                    if(item.indexOf(_this.myText) >= 0){
                        return true
                    }else{
                        return false
                    }
                })
            }
        }
    })

如下图所示,筛选功能已生效
image

解决方法二 使用箭头函数

前头函数用于匿名函数,可以继承上一个函数的this

无参数的箭头函数

let f = function () {
    console.log('123')
}
f()
// 修改为箭头函数写法
let f = () => {     // 省略了function
    console.log('123')
}
f()

有一个参数的箭头函数

let f = function (item) {
   console.log(item)
}
f('hello world')
// 修改为箭头函数
let f = item => {   // 可以省略扩号(括号也可以不省略)
    console.log(item)
}
f('hello world')

有两个参数的箭头函数

let d1 = {'name': 'Hello World'}
let f = function (item, key) {
   console.log(item, key)
}
f(d1)
// 使用前头函数
let f = (item, key) => {    // 两个参数必须加括号
   console.log(item, key)
}
f(d1)

有一个参数一个返回值的箭头函数

let f = function (item) {
   return item + '!!!!'
}
res = f('hello world')
// 使用匿名函数
let f = item => item + '****'  // 省略了return
res = f('hello world')
console.log(res)

标签:function,vue,console,函数,item,箭头,let
From: https://www.cnblogs.com/smyz/p/17293998.html

相关文章

  • vue动态添加表单validateField验证
    1<template>2<el-formref="form":model="form":rules="rules"label-width="100px">3<divv-for="(input,index)ininputs":key="index">4<el-form-......
  • 配电网分布式电源和储能选址定容 以配电网总成本最低为目标函数,其中包括年运行成本
    配电网分布式电源和储能选址定容以配电网总成本最低为目标函数,其中包括年运行成本,设备维护折损成本、环境成本;以系统潮流运行为约束条件,采用粒子群算法求解,实现光伏、风电、储能设备的规划。YID:4980674139963843......
  • 函数-日期函数
    常见的日期函数:  代码:selectcurdate();/*返回当前日期*/selectcurtime();/*返回当前时间*/selectnow();/*返回当前日期+时间*/selectyear(now());/*返回当前日期中的年份*/selectmonth(now());/*返回当前日期中的月份*/selectday(now());/*返回当前日期中的日份......
  • 尾置const:参数列表相同却重载的函数
    观察下面两个函数,它们具有相同的参数列表,貌似无法重载:   string&operator[](size_tn){returnelements[n];}2.   conststring&operator(size_tn)const {returnelements[n];}但实际上,函数2的尾置const使得对*this进行了筛选,于是:当非const对象调......
  • uni-app:nvue:居左/居右/居中对齐(hbuilderx 3.7.3)
    一,代码:居中:<viewstyle="position:fixed;bottom:0;width:750rpx;height:60rpx;display:flex;flex-direction:row;justify-content:center;"><span>{{appName}}version:{{appVersion}}</span></view......
  • 灰狼优化算法GWO优化SVM支持向量机惩罚参数c和核函数参数g
    灰狼优化算法GWO优化SVM支持向量机惩罚参数c和核函数参数g,有例子,易上手,简单粗暴,替换数据即可,分类问题。仅适应于windows系统YID:6999630206572076......
  • 粒子群算法PSO优化LSSVM最小二乘支持向量机惩罚参数c和核函数参数g
    粒子群算法PSO优化LSSVM最小二乘支持向量机惩罚参数c和核函数参数g,用于回归预测,有例子,易上手,简单粗暴,直接替换数据即可。仅适应于windows系统。质量保证,完美运行。        本人在读博士研究生,已发表多篇sci,非网络上的学习代码,不存在可比性。ID:6999630547781158......
  • 量化交易之MQL4篇 - 对象函数的基础应用
    voidOnStart(){stringvlineObject="vlineObject";stringhlineObject="hlineObject";stringtrendObject="trendObject";stringtrendbyangleObject="trendbyangleObject";stringfi......
  • vue之数组的方法
    目录简介filter方法简介本文会把遇到的数组的方法慢慢补充进来filter方法filter()方法是一个过虑方法以下面的为例:列表dataList会每次取一个值,把值给匿名函数,并执行。比如第一次过虑会把a给dataList.filter(function(items){xxxx},第二次会把at传给函数,后面依次类推。<s......
  • 2023.04.06 - vue组件中动态指定监听的值
    业务场景:高拍仪给出的视频信息API回调里会不断返回图像数据。因为有主副摄像图像信息,并且两个图像信息会二选一展示在DOM容器里。所以就是二对一的关系。//主摄像数据letpriPic:string='';//副摄像数据letsubPic:string='';//展示在容器的数据=主摄像数据||副摄像......