【问题标题】:Utility functions in Vue component inside or outside componentVue组件内部或外部组件中的实用功能
【发布时间】:2020-01-25 10:51:50
【问题描述】:

在 Vue 组件中注册函数的首选方法是什么。 我倾向于仅在组件中注册视图所需的方法(或需要直接组件数据访问)以及视图外部视图不需要的其他功能。 假设utilityFunction()utilityFunctionTwo() 当前仅在该组件内部使用。

这是一个例子:

<template>
    <button @click="bar"></button>
    <button @click="foo"></button>
    <button @click="baz"></button>
</template>

<script>
  export default {
      name: "SampleComponent",
      data() {
          return {
              someVariable: "ABC",
              otherVariable: 123
          }
      },
      methods: {
          foo() {
              //some logic
              utilityFunction(this.someVariable);
              //other logic
          },
          bar() {
              //some logic
              utilityFunction(this.someVariable);
              utilityFunctionTwo(this.otherVariable);
              //some other logic
          },
          baz() {
              //some logic
              utilityFunctionTwo(this.someVariable);
              //some other logic
          }
      }
  }
  function utilityFunction(arg){
      //do something
  }
  function utilityFunctionTwo(arg){
      //do something
  }
</script>

参数可能不同,实用程序函数可以是纯的并返回一些东西或改变参数。可能有很多不同的场景。但我希望你明白这一点。

另一种方法是将这些函数作为方法添加到您的组件中。 像这样:

<template>
    <button @click="bar"></button>
    <button @click="foo"></button>
    <button @click="baz"></button>
</template>

<script>
    export default {
        name: "SampleComponent",
        data() {
            return {
                someVariable: "ABC",
                otherVariable: 123
            }
        },
        methods: {
            foo() {
                //some logic
                this.utilityFunction();
                //other logic
            },
            bar() {
                //some logic
                this.utilityFunction();
                this.utilityFunctionTwo(this.otherVariable);
                //some other logic
            },
            baz() {
                //some logic
                this.utilityFunctionTwo(this.someVariable);
                //some other logic
            },
            utilityFunction() {
                //do something
                console.log(this.someVariable)
                //other stuff
            },
            utilityFunctionTwo(arg) {
                //do something
            }
        }
    }
</script>

在这种方法中,您有时不需要将参数传递给方法,因为它可以访问组件 data 对象。

由于原因,我稍微喜欢第一种方法:

  1. 我有模板使用的或组件所需的方法的简短列表(出于某种原因)。如果所有方法都放在那里,有时这个列表会变得很长。
  2. 如果函数将来可以在其他地方使用,我将能够轻松地将其添加到某个 .js 文件中并将其导入其他组件中。
  3. 如果不需要,函数无权访问组件范围
  4. 它让我不用输入 this 关键字 ;-) 如果在 lambda 中使用,有时可能会很有用。

我不确定这是否是一个意见问题,您是否可以纯粹根据您的个人喜好选择一种方法而不是另一种方法,或者它们是否是您应该选择一种方法而不是另一种方法的任何客观原因,例如(但不限于)被一种解决方案破坏的软件设计组件或原则的性能。

【问题讨论】:

标签: javascript vue.js vue-component


【解决方案1】:

区别在于utilityFunctionutilityFunctionTwo 辅助函数是不可测试的。它们不能直接访问。他们不能被嘲笑,即使他们是:

  export function utilityFunction(arg){
      //do something
  }

  export function utilityFunctionTwo(arg){
      //do something
  }

正如here 所解释的那样,由于模块的工作方式,只有从模块导出并在另一个模块中使用的函数才能窥探或模拟函数。

为了完全可测试或可重复使用,utilityFunctionutilityFunctionTwo 应移至另一个模块。

由于它们被组件私有地使用并且不被重用,一个合理的替代方法是使它们成为私有方法,这可以用下划线表示:

    methods: {
        ...
        _utilityFunction() {
            //do something
        },
        _utilityFunctionTwo(arg) {
            //do something
        }
    }

缺点是方法不能作为死代码自动删除,以防不使用,但常规函数可以。

【讨论】:

  • 感谢您的回答。测试这些方法的问题并不大。我真正要做的是测试foobarbaz及其效果。因为他们是我最关心的。将来我可能会找到另一种解决方案来摆脱实用方法,并且不需要在没有主要方法的情况下测试它们。这种方法可能直接来自我的主要背景 Java 中的单元测试。你不测试私有实现,你测试依赖它们的方法。
  • 您不测试私有实现,您测试依赖它们的方法 - 这是测试方法的问题,取决于具体情况。测试行为而不是实现模糊了单元测试和功能测试之间的界限。您可能不会在 Java 中遇到同样的困境,因为方法无处不在。即使他们是私人的,他们也可以在需要时被嘲笑或窥探。无论如何,测试是我在这里看到的唯一区别。
猜你喜欢
  • 2020-04-27
  • 2020-12-10
  • 2020-11-01
  • 2021-08-06
  • 1970-01-01
  • 2021-05-16
  • 1970-01-01
  • 1970-01-01
  • 2020-04-25
相关资源
最近更新 更多