【问题标题】:custom filters and or plugins to run inside components在组件内部运行的自定义过滤器和/或插件
【发布时间】:2021-07-10 08:14:30
【问题描述】:

我们已经构建了一个大型 Vuejs 应用程序,我们现在正在将其组件添加到 Vuepress。

但是,我们在弄清楚如何迁移整个项目中使用的一些全局使用的插件和可组合函数时遇到了问题。它们是格式化日期、货币和大小写的典型帮助插件。

一个例子是大写文本{{ postTitle | uppercase }},另一个在computedmethods中被称为uppercase(String)

我找不到任何关于如何创建和管理辅助函数的 Vuepress 文档。

非常感谢任何帮助者。

【问题讨论】:

    标签: filter plugins vuepress composable


    【解决方案1】:

    有一些选项可以将功能全局应用于您的 vuepress 项目。

    您可以将您的辅助函数作为 mixins 添加到每个组件中,或者在 enhanceApp.js 文件中全局定义它们。例如,在下面的代码中,我全局定义了 helperFunction() 方法

    // enhanceApp.js
    export default ({
        Vue, // the version of Vue being used in the VuePress app
        options, // the options for the root Vue instance
        router, // the router instance for the app
        siteData, // site metadata
        isServer // is this enhancement applied in server-rendering or client
    }) => {
        Vue.mixin({
            methods: {
                helperFunction () {
                    // Your helper function
                }
            }
        })
    }
    

    This question 有一个很好的例子,可以在单个文件组件中而不是在全局范围内定义 mixins。你可以在Vue.js documentation找到更多关于mixins的信息。

    另一种方法是将您的辅助函数 into vuepress plugins 分开。

    【讨论】:

    • 谢谢 - 尝试过helperFunction() { return "helperFunction"; } 但它没有被` {{ "helperFunction : " |帮助函数}}`。对于 Vuepress 文档,我无法继续。就像任何文件在哪里以及它们如何被调用......
    • 跟进:我确实在浏览器中收到错误:[Vue warn]: Failed to resolve filter: helperFunction
    猜你喜欢
    • 2019-04-10
    • 1970-01-01
    • 2015-10-01
    • 2010-12-13
    • 2014-04-07
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    相关资源
    最近更新 更多