随手记: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 : '';
      }
    },
相关推荐
Anastasiozzzz11 小时前
阿亮随手记:动态条件生成Bean
java·前端·数据库
Highcharts.js11 小时前
数据之美:用Highcharts打造专业级弧线图
javascript·数据库·highcharts·图表开发·弧线图
mclwh11 小时前
关于React-Konva 报:Text components are not supported....错误的问题
前端·react.js
SuperEugene12 小时前
错误处理与 try/catch:真实项目里应该捕什么错?
前端·javascript·面试
Amumu1213812 小时前
CSS引入方式
前端·css
我是Superman丶13 小时前
【Demo】✋ 数字手势识别 Html
前端·html
HelloReader13 小时前
Leptos + Tauri 2 前端配置Trunk + SSG + 移动端热重载一次打通(Leptos 0.6 口径)
前端
HelloReader13 小时前
Next.js + Tauri 2 用 Static Export 把 React 元框架装进桌面/移动端
前端
Wect13 小时前
从输入URL到页面显示的完整技术流程
前端·面试·浏览器
没有bug.的程序员14 小时前
自动化测试之魂:Selenium 与 TestNG 深度集成内核、Page Object 模型实战与 Web UI 交付质量指南
前端·自动化测试·selenium·ui·testng·page·object