简体   繁体   English

如何在外部 .js 文件中分离 .vue 组件的方法?

[英]How to separate the methods of a .vue component in an external .js file?

My component was left with many lines of code, so I decided to put the methods in a separate file called functions.js.我的组件留下了许多代码行,所以我决定将这些方法放在一个名为 functions.js 的单独文件中。 I can not call those methods.我不能调用那些方法。

I tried this:我试过这个:

functions.js函数.js

function sendList() {...};
function getLists() {...};
function deleteList(listId) {...}

export default {sendList, getLists, deleteList}

MyLayout.vue我的布局.vue

...
<script>
import {sendList, getLists, deleteList} from '../statics/functions.js'
...
created() { this.getLists();},
...

3 errors appear:出现3个错误:

vue.runtime.esm.js?2b0e:587 [Vue warn]: Error in created hook: "TypeError: this.getLists is not a function" vue.runtime.esm.js?2b0e:587 [Vue 警告]:创建钩子时出错:“TypeError:this.getLists 不是函数”

TypeError: this.getLists is not a function TypeError:this.getLists 不是函数

Property or method "lists" is not defined on the instance but referenced during render.属性或方法“列表”未在实例上定义,但在渲染期间被引用。 Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.通过初始化该属性,确保该属性是反应性的,无论是在数据选项中,还是对于基于类的组件。 See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties .请参阅: https ://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties。

I guess 2 things should be fixed:我想应该解决两件事:

  1. First thing is to make sure to export without default in your functions.js file, like below:首先要确保在您的 functions.js 文件中没有default导出,如下所示:
function sendList() {...};
function getLists() {...};
function deleteList(listId) {...}

export { sendList, getLists, deleteList }

...or even more prettier using ES6 syntax: ...或者使用 ES6 语法更漂亮:

const sendList = () => {...};
const getLists = () => {...};
const deleteList = (listId) => {...}

export { sendList, getLists, deleteList }
  1. Second thing, import them and use without this , like below:第二件事,导入它们并在没有this的情况下使用,如下所示:
...
<script>
import { sendList, getLists, deleteList } from '../statics/functions.js'
...
created() { getLists() },
...

The other answerer is correct that you need to be using export instead of export default .另一个回答者是正确的,您需要使用export而不是export default

If you really need them to be methods on the component instance, after importing them, you can add them to the methods like so:如果您确实需要它们成为组件实例上的方法,则在导入它们之后,您可以将它们添加到方法中,如下所示:

methods: {
  deleteList,
  sendList,
  getLists,
  anotherFunction() {
   ...
  },
}

and then they will be accessible as this.getLists() and so on.然后它们将可以通过this.getLists()等方式访问。 It will still be several lines of code (one for each method you're importing), but way fewer than having the method and all the associated logic.它仍然是几行代码(您要导入的每个方法一个),但比拥有该方法和所有相关逻辑要少得多。

Oh, and as for the third error, the not defined on the instance but referenced during render ?哦,至于第三个错误, not defined on the instance but referenced during render That happens when you've got something in the template that hasn't been properly defined in the script part.当您在脚本部分中没有正确定义模板中的某些内容时,就会发生这种情况。 Did you type lists somewhere you meant to type list ?您是否在要输入lists的地方输入了list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM