A common use case for Quasar applications is to run code before the root Vue app instance is instantiated, like injecting and initializing your own dependencies (examples: Vue components, libraries…) or simply configuring some startup code of your app.
在quasar 框架中boot 文件通常在root Vue被實例化之前運行,比如注冊和初始化自己的依賴
(例如vue組件,庫等)或者是app啟動程序的一些簡單配置。
Since you won’t be having access to any /main.js
file (so that Quasar CLI can seamlessly initialize and build same codebase for SPA/PWA/SSR/Cordova/Electron) Quasar provides an elegant solution to that problem by allowing users to define so-called boot files.
由於沒有main.js文件(因此quasar CLI才能為SPA/PWA/SSR/Cordova/Electron無縫隙初始化與構建時使用相同的代碼庫)quasar允許用戶自定義boot文件。
In earlier Quasar versions, to run code before the root Vue instance was instantiated, you could alter the /src/main.js
file and add any code you needed to execute.
在早期的版本中,root Vue實例被實例化之前,可以通過修改src下的main.js文件,添加需要執行的代碼。
There is a major problem with this approach: with a growing project, your main.js
file was very likely to get cluttered and challenging to maintain, which breaks with Quasar’s concept of encouraging developers to write maintainable and elegant cross-platform applications.
這樣就產生了一個問題:隨着項目的發展,main.js文件將會變的臃腫且難以維護.將破壞quasar
倡導開發者編寫優雅且可維護的 跨平台程序的理念。
With boot files, it is possible to split each of your dependencies into self-contained, easy to maintain files. It is also trivial to disable any of the boot files or even contextually determine which of the boot files get into the build through quasar.conf.js
configuration.
通過boot文件,可以拆分為每一個獨立,易維護的boot文件,這些boot文件可以在quasar.conf.js配置文件中設置禁用和啟用。
所以我們通過創建一個boot啟動文件來注入全局的filter,步驟如下:
一、使用Quasar CLI生成一個boot文件名:
quasar new boot
這個name自己隨意起,CLi會在/src/boot/生成這個js文件
二、在生成的這個文件中編寫需要的filter內容
三、在quasar.conf.js中把這個文件名添加到啟動配置中
最后可以在vue頁面中直接使用
參考資料: