【问题标题】:vue component to use common functionsvue组件使用常用功能
【发布时间】:2019-01-27 21:02:23
【问题描述】:

我希望掌握 Vue CLI3 项目系统。目前正在将内联 vue 的长单个 html 文件重构为真正的“.vue”组件。一个目标是在我的 vue 组件中使用一些常用功能来处理各种事情。 在我的 common-functions.js 文件中,我有这样的内容:

function capitalize(str) {
    return str[0].toUpperCase() + str.substr(1, );
};

在我的 HelloWorld.vue 文件中,我得到了这个,但经过多次尝试都无法正常工作。我发现的所有搜索似乎都在处理其他事情,肯定有一种简单的方法可以只使用一些常用功能,对吧??

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <ul>
      <li v-for='c in categoryNames'>{{ c }}</li>
    </ul>
  </div>
</template>

<script>

  require('../js/common-functions.js');

  export default {
    name: 'HelloWorld',
    data () {
      return {
        msg: capitalize('welcome to Your Vue.js App!'),
        categoryNames : this.$root.categoryNames
      }
    }
  }

</script>

当然消息是:

[Vue warn]: Error in data(): "ReferenceError: capitalize is not defined"

found in

---> <HelloWorld> at src/components/HelloWorld.vue
       <App> at src/App.vue
         <Root>

【问题讨论】:

    标签: vue.js components


    【解决方案1】:

    一个解决方案:

    通过Vue.use()将您的全局函数注册到Vue.prototype

    如下演示:

    let myGlobalAPIGroup1 = { // API Group 1
      install: function (_Vue) {
        if(!_Vue.prototype.$apiGroup1) {
          _Vue.prototype.$apiGroup1 = {}
        }
        _Vue.prototype.$apiGroup1.capitalize = function (str) {
          return str[0].toUpperCase() + str.substr(1, );
        }
      }
    }
    
    let myGlobalAPIGroup2 = { // API Group 2
      install: function (_Vue) {
        if(!_Vue.prototype.$apiGroup2) {
          _Vue.prototype.$apiGroup2 = {}
        }
        _Vue.prototype.$apiGroup2.capitalize = function (str) {
          return str[0].toUpperCase() + str.substr(1, ) + '@';
        }
      }
    }
    
    Vue.use(myGlobalAPIGroup1) //register
    Vue.use(myGlobalAPIGroup2) //register
    
    new Vue({
      el: '#app',
      data() {
        return {
          testValues: ['label a', 'label b'],
        }
      },
      methods:{
        testAPI1: function(item) {
          return this.$apiGroup1.capitalize(item)
        },
        testAPI2: function(item) {
          return this.$apiGroup2.capitalize(item)
        }
      }
    })
    #app > div {
      display: inline-block;
      margin-left: 5px;
      border: 1px solid black;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
    <div id="app">
      <div>
        <h3>Group 1:</h3>
        <p v-for="(item, index) in testValues" :key="index">{{testAPI1(item)}}</p>
      </div>
    
      <div>
        <h3>Group 2:</h3>
        <p v-for="(item, index) in testValues" :key="index">{{testAPI2(item)}}</p>
      </div>
    </div>

    【讨论】:

    • 感谢您的回答,这对我的增长知识非常有用。
    【解决方案2】:

    common-functions.js末尾,导出函数:

    export default capitalize;
    

    HelloWorld.vue 中,使用以下命令导入:

    import capitalize from '../js/common-functions.js';
    // this should replace the require line
    

    【讨论】:

    • 这个方法似乎是我正在寻找的基本用法,它对我有用。尽管我选择了将许多函数作为对象的一部分而不是单独的函数导出的方法,但由于为 js 代码提供了单独的名称空间,我可以看到这是多么有用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2021-03-28
    • 2020-11-24
    • 2019-11-13
    • 2021-09-26
    相关资源
    最近更新 更多