網站首頁 Vue 正文
vue過濾器filters獲取不到this對象
原理
- 在data中定義一個屬性that,把this存儲到that中
- 在調用filters中的方法sum的時候將that傳進去即可
下面舉個例子
用filters計算data中 a+b 的值
- 注意:filters中的sum方法的第一個參數是|左邊那個a,第二個參數才是括號寫的that
<template> ? <div>{{a|sum(that)}}</div> </template> <script> ? export default { ? ? name: "test", ? ? data() { ? ? ? return { ? ? ? ? that: this, ? ? ? ? a: 1, ? ? ? ? b: 2 ? ? ? } ? ? }, ? ? filters: { ? ? ? sum(a, that) { ? ? ? ? console.log(that); ? ? ? ? return a + that.b; ? ? ? } ? ? }, ? } </script>
Vue filters this指向問題
Vue實例中filter不依賴于當前vue實例上下文
所以在filter中無法直接訪問當前vue實例, 所以可以使用computed替代。
可是當遇到需要根據html文本改變,v-for的數據等情況而改變時,computed的功能就無法滿足我們的需求了。
那我們就可以使用methods代替
data: { ? ? shopItemType: {} }, methods: {? ?? ?shopItemType2str(id){ ? ? ? ? return this.shopItemType[id]; ? ? } }
<tr v-for="shopItem in shopItems"> ?? ?<td>{{shopItemType2str(shopItem.item_type)}}</td> </tr>
原文鏈接:https://blog.csdn.net/ZZQ928000/article/details/105141690
相關推薦
- 2022-09-22 數據結構:順序表和鏈表學習小結
- 2022-10-11 oracle 12c和plsql的詳細安裝和配置過程(超級詳細,小白也能懂)
- 2023-10-15 webrtc 測試video_loopback
- 2023-01-17 Qt中控件的函數使用教程分享_C 語言
- 2023-02-01 C#模擬實現QQ窗體功能_C#教程
- 2023-08-13 常見的linux命令
- 2022-06-06 Array.prototype.myjoin
- 2022-07-25 Android如何使用正則表達式只保留字母數字_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支