搜尋
首頁微信小程式微信開發filter怎樣全域使用

filter怎樣全域使用

Jun 04, 2018 pm 03:55 PM
filter使用怎樣

這次帶給大家filter怎麼全域使用,filter全域使用的注意事項有哪些,以下就是實戰案例,一起來看一下。

官方給出

Vue.filters(id , [definition])
//id {string}
//definition {function}

詳情查看

在專案中我們如果有多個filters,那麼我怎麼一次註冊,全域可用,我們在專案裡新建一個filters資料夾,如下,index.js是出口文件,readMore是一個對字串做處理的過濾器

檔案目錄

下面貼上程式碼:

//index.js
// 引入所有的过滤函数
import readMore from './readMore';
// 导出在一个对象上
export default {
  readMore
};
//readMore.js
//查看更多文字显示'...'
let readMore = (text,length,suffix) => {
 if(text) {
  if(text.length <p style="text-align: left;">然後在main.js裡面做如下處理:</p><p style="text-align: left;"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/291b9a26604affe749aca9aa2b99c650-1.png?x-oss-process=image/resize,p_40" class="lazy" alt=""></p><p   style="max-width:90%">##main.js做全域註冊<strong></strong></p><pre class="brush:php;toolbar:false">//全局注册自定义的过滤器
import filters from './filters';
for(let key in filters){
 Vue.filter(key, (val,value1,value2) => {
 return filters[key](val,value1,value2);
 });
}
就可以在全域使用了

//在test.vue里面使用
  <p>全文`)"></p>
<span>#<span>{{'文字文字' | readMore(15,'...')}}</span>#</span>
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

實戰專案編譯後不在根目錄怎麼辦

如何取得dom內class的值

以上是filter怎樣全域使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)