【问题标题】:Importing a common library into a Vue SPA将通用库导入 Vue SPA
【发布时间】:2019-11-05 08:05:29
【问题描述】:

很难理解我认为应该是一项简单的任务,但我准备接受也许我正在以错误的方式处理这个问题,并愿意将我的计划改为正确的方式。

简而言之,我有一个 .js 文件,其中包含数百个在我们的应用程序中使用的对象和函数。我们正在更新我们的方法并重构一些功能。

作为一个非常基本的例子,我有:

**COMMON.JS**
const dynamicYearColumn= {
    resizable: false, suppressMovable: true, sortable: true, width: 100, menuTabs: [], type: 'numericColumn',
}


const defaultPercentageColumn= {

    resizable: false, suppressMovable: true, sortable: true, width: 150, menuTabs: [], type: 'numericColumn',
    //comparator: customPercentageCompare,
    valueFormatter: formatPercent,
    cellStyle: { 'text-align': 'right' },
    cellClass: function (params) { var className = numberToColorClass(params.value); return className }
}


function formatPercent(number) {

    if (isNaN(number.value) == true) { return '-' }
    return isFinite(numberWithCommas(parseFloat(number.value, 2).toFixed(this.defaultDecimalRounding))) ? numberWithCommas(parseFloat(number.value, 2).toFixed(this.defaultDecimalRounding)) + '%' : '?';
}

function numberWithCommas(n) {
    var parts = n.toString().split("."); return parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");
}

在我的 main.js 中,我现在有:

import common from './scripts/common.js'
const commonLibrary = {
    install() {
        Vue.common = common
        Vue.prototype.$common = common
    }
}

Vue.use(commonLibrary)

这就是我失败的地方。

在我的 common.js 中,如果我将代码包含在 export default 中,我必须更改代码(删除 const 以便对象是实际的对象定义,然后 formatPercent 将无法识别。

如果我 export {dynamicYearColumn,defaultPercentageColumn} 则“有点”有效,但我仍然在 formatPercent 和 numberWithCommas 上未定义。

同样,如果我尝试创建一个 Mixin,那么这也可以,但是 Mixins 方法中的任何函数都无法识别,另外有人建议我不要将这么大的库推入 Mixins,因为这不是很好的做法。

我很欣赏那里有大量优秀示例的资源,但老实说,我真的想不出正确的术语来开始寻找工作示例。

TLDR;

我只想允许从单个 JS 文件访问大量对象和函数,并在任何组件上从我的 Vue SPA 调用。

【问题讨论】:

    标签: javascript vue.js es6-modules


    【解决方案1】:

    听起来你快到了。

    假设您要这样做:

    import common from './scripts/common.js'
    

    要实现这一点,我们需要 common.js 有一个 default 导出。所以从export default开始。

    进一步,假设我们要以common.dynamicYearColumn、common.formatPercent 等方式访问对象和函数。即使忽略import/export 部分,这也意味着common 需要是一个对象名为dynamicYearColumn、formatPercent 等的属性。其中一些属性将是函数,但实际上并没有太大区别。

    所以让我们简要考虑一下如何构建这样的对象。

    const common = {}
    common.dynamicYearColumn = { /* object properties here */ }
    common.formatPercent = function (number) { /* implementation here */ }
    

    当然,对象字面量允许我们直接定义具有这些属性的对象,而不是稍后添加它们:

    const common = {
      dynamicYearColumn: { /* object properties here */ },
      formatPercent: function (number) { /* implementation here */ }
    }
    

    ES6 添加了一些语法糖来简化对象内函数的创建,因此可以简化为:

    const common = {
      dynamicYearColumn: { /* object properties here */ },
      formatPercent (number) { /* implementation here */ }
    }
    

    这是我们要在common.js 中创建然后导出的对象。综上所述,我们得到:

    export default {
      dynamicYearColumn: {
        // ...  
      },
    
      defaultPercentageColumn: {
        // ...
      },
    
      formatPercent (number) {
        // ...
      },
    
      numberWithCommas (n) {
        // ...
      }
    }
    

    将它们添加到Vue.prototype,就像您在示例中一样,将使它们在您的组件中可用:

    this.$common.formatPercent(25)
    

    在您的模板中是:

    {{ $common.formatPercent(25) }}
    

    这一切都很好,但请注意,您将无法将这些函数用作过滤器。所以你不能这样写:

    {{ 25 | $common.formatPercent }}
    

    如果这是您想要的,那么您必须将这些功能注册为插件中的过滤器。

    【讨论】:

      猜你喜欢
      • 2018-08-26
      • 2020-06-25
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 2020-10-03
      • 2018-12-03
      • 2018-10-29
      • 1970-01-01
      相关资源
      最近更新 更多