網站首頁 Vue 正文
vue filters和directives訪問this
記錄一次奇葩的需求。
要求自定義一個指令,點擊后能跳轉指定路由。
directives和filters壓根就沒法訪問this。
腦袋都想破了。
不廢話了,上代碼。
<template> <div> <div v-join="(divData, that)">tag標簽</div> <p>{{divData}}</p> <p>{{divData | sum(that)}}</p> </div> </template> <script> export default { name: 'Main', data(){ return { divData:'傳入的值', that: this, filtersData: '過濾器的值' } }, filters: { sum(val, that){ return `${val}和${that.filtersData}` } }, directives: { join: { inserted(el, binding){ }, bind(el, binding){ console.log('------2') el.addEventListener('click', function(){ binding.value.that.$router.push({ path: '/aside' }) }) } } } } </script>
解決方案是在data中定義一個that變量指向this,再將這個變量當做參數傳進directives,在directives內部就可以訪問到this了,filters同理。
directives所遇小bug
自己在利用vue寫todoList的時候遇到一個小bug,記錄一下
寫個指令,當雙擊進行編輯todo,讓其自動聚焦,結果效果如下,
代碼如下:
directives: { focus(el,bindings) { if(bindings.value) { el.focus(); } } }
<input v-focus="todo == t" type="text" v-show="todo == t" v-model="todo.title" @blur="reset" @keyup.13="reset" >
多方查找原因,把自定義v-focus指令放末尾,就好了,代碼如下:
<input type="text" v-show="todo == t" v-model="todo.title" @blur="reset" @keyup.13="reset" v-focus="todo == t">
是否自定義指令都應放后面呢?這就需要以后驗證了
完整代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link href="../node_modules/bootstrap/dist/css/bootstrap.css"> <style> .del{ text-decoration: line-through; color: #ccc!important; } </style> </head> <body> <div id="app"> <div class="container"> <div class="row"> <div class="col-md-12"> <div class="panel panel-warning"> <div class="panel-heading"> <input type="text" v-model="value" class="form-control" @keydown.enter="add"> </div> <div class="panel-body"> <ul class="list-group"> <li class="list-group-item" @dblclick="change(todo)" v-for="(todo,index) in todos" :key="index"> <div v-show="todo != t" :class="{del: todo.isSelected}"> <input type="checkbox" v-model="todo.isSelected">{{todo.title}} <button class="pull-right btn btn-danger btn-xs" @click="remove(index)">⨂</button> </div> <input type="text" v-show="todo == t" v-model="todo.title" @blur="reset" @keyup.13="reset" v-focus="todo == t"> <!-- 下面是錯誤代碼,可以把上面的注釋掉打開下面的對比下 --> <!-- <input v-focus="todo == t" type="text" v-show="todo == t" v-model="todo.title" @blur="reset" @keyup.13="reset" > --> </li> </ul> </div> <div class="panel-footer"> <ul class="nav nav-pills"> <li role="presentation" class="active"><a href="#">全部</a></li> <li role="presentation"><a href="#">已完成</a></li> <li role="presentation"><a href="#">未完成</a></li> </ul> </div> </div> </div> </div> </div> </div> <script src="../node_modules/vue/dist/vue.js"></script> <script> let vm = new Vue({ el:'#app', data:{ todos:[], hash:'complete',//路徑切換時 獲取的hash值 value:'',// 輸入框中需要增加的內容, t:''//當前點擊的那一個 }, created(){ //當vue創建時執行的方法 //如果storage有 就用這里的值 沒有就是空數組 this.todos = JSON.parse(localStorage.getItem('todos')) || [{isSelected:true,title:'晚上回去睡覺'}]; }, watch:{ //watch默認 只監控一層,例如 todos 可以監控數組的變化,監控不到對象的變化 todos:{ handler(){ localStorage.setItem('todos',JSON.stringify(this.todos)); }, deep:true } }, methods:{ addTodo(){ let todo = {isSelected:false,title:this.value}; this.todos.push(todo); this.value = ''; }, remove(todo){ this.todos = this.todos.filter(item=>todo!==item); }, change(todo){ //todo代表的是我當前點擊的是哪一個,存儲當前點擊的這一項 this.t = todo; }, reset(){ this.t = ''; } }, computed:{ lists(){ if(this.hash === 'complete'){ return this.todos; } if(this.hash === 'finish'){ return this.todos.filter(item=>item.isSelected) } if(this.hash === 'unfinish'){ return this.todos.filter(item=>!item.isSelected) } }, total(){ //求出數組中為false的個數 //將數組中的true全部干掉,求出剩余length return this.todos.filter(item=>!item.isSelected).length; } }, directives:{ //指令,就是操作dom focus(el,bindings){ //bindings中有一個value屬性 代表的是指令對應的值v-auto-focus="值" if(bindings.value){ el.focus(); } //console.log(el,bindings); } } }); let listener = () => { let hash = window.location.hash.slice(1) || 'complete'; //如果打開頁面沒有hash默認是全部 vm.hash = hash; }; listener(); //頁面一加載 就需要獲取一次hash值,否則可能導致 回到默認hash window.addEventListener('hashchange',listener,false); </script> </body> </html>
原文鏈接:https://blog.csdn.net/weixin_44038264/article/details/107858680
相關推薦
- 2022-04-11 關于指令重排現象的兩個階段詳解_相關技巧
- 2022-11-19 C語言數據結構不掛科指南之隊列詳解_C 語言
- 2023-03-29 Python之二維正態分布采樣置信橢圓繪制_python
- 2022-05-22 C#多線程編程Task用法詳解_C#教程
- 2023-02-15 Python函數常見幾種return返回值類型_python
- 2022-08-15 gitlab代碼合并(merge request )取消 [默認刪除分支(Delete source
- 2022-07-14 Qt創建SQlite數據庫的示例代碼_C 語言
- 2022-03-27 帶你從編碼角度分析C++重載原理_C 語言
- 最近更新
-
- 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同步修改后的遠程分支