随手记:vue2 filters this指向undefined

今天在使用filters的时候,需要用到this的数据,结果发现,this打印出来的是undefined

原因: 过滤器注册在vue实例之前,所以this指向了window,但是因为严格模式原因,为 undefined;

所以需要全局声明this,在filters中才能指向vue

javascript 复制代码
    //表格中插槽使用filters的地方
      <template #joinStatus>
          <el-table-column label="随访状态" align="center">
            <template slot-scope="scope">
              {{ scope.row.joinStatus | joinStatusFilter(that) }}
            </template>
          </el-table-column>
        </template>


    //在data中声明that 指向this
    data() {
        return{
            // 保存this以便filter中使用
            that: this,
        }
    }
      

    filters: {
      joinStatusFilter(value, that) {
        let status = that.statusList.find(item => item.value == value);
        return status? status.label : '';
      }
    },
相关推荐
biubiubiu_LYQ13 小时前
萌新小白基础理解篇之 this 关键字
前端·javascript
甜味弥漫13 小时前
深度解析 JS 中的 this 指向:从底层逻辑到实战规则
javascript·面试
光影少年13 小时前
Redux 中间件作用(redux-thunk/redux-saga)
前端·react.js·掘金·金石计划
爱上好庆祝13 小时前
学习JS第十一天(JS的进阶)
前端·javascript·学习
喵个咪13 小时前
统一范式:中后台Admin项目标准化API分层开发方案(Vue/React通用)
前端·vue.js·react.js
Shirley~~13 小时前
figma Mcp
前端·ai编程
Bacon13 小时前
Gstack + Superpowers:当 AI 编程的"脑子"和"手脚"终于在一起了
前端·人工智能
喵个咪14 小时前
拒绝过度封装!GoWind Admin:基于Element Plus重塑中后台CRUD开发范式
前端·vue.js·typescript
Komorebi_999914 小时前
LangChain Day5 课程:Agent 智能代理
前端·langchain·大模型
七牛云行业应用14 小时前
别等了!xAI 的终端编码 Agent 来了——Grok Build 0.1 安装到并行 Agent全流程
前端