简体   繁体   English

构建js文件vue cli项目

[英]Structuring js files vue cli project

How should I structure my Vue CLI project?我应该如何构建我的Vue CLI项目? I am unable to find any proper documentation regarding this.我找不到任何有关此的适当文件。 Basically I have around 10 modules and each module has a js file associated with it.基本上我有大约 10 个模块,每个模块都有一个与之关联的 js 文件。

Currently, I am putting all the pages written in my router.js in views directory and all the components in the components directory.目前,我将在我的 router.js 中编写的所有页面放在views目录中,并将所有组件放在components目录中。 I want to know where should I keep mine js files?我想知道我应该把我的js文件放在哪里?

  1. All the js api calls associated with every module与每个模块关联的所有 js api 调用

  2. JS files containing all the constants related to every module??包含与每个模块相关的所有常量的 JS 文件?

Q1: Usually API calls are stored under a respective store if you are using Vuex. Q1:如果您使用 Vuex,通常 API 调用存储在相应的存储下。 If not you can use define them as mixins and use where necessary.如果没有,您可以使用将它们定义为 mixins 并在必要时使用。 The mixins are the parts of the javascript code that are reused in different components. mixin 是 javascript 代码的一部分,可在不同组件中重用。 In a mixin you can put any component's methods from Vue.js they will be merged with the ones of the component that uses it.在 mixin 中,您可以放置 Vue.js 中的任何组件的方法,它们将与使用它的组件的方法合并。

Q2: This can definitely go under mixins. Q2:这个绝对可以在mixins下go。

You can also have a util folder (optional) where it contains the functions that you use in components, such as regex value testing, constants, or filters.您还可以拥有一个 util 文件夹(可选),其中包含您在组件中使用的函数,例如正则表达式值测试、常量或过滤器。

Refer to this boilerplate if your project is mid-scale or large-scale.如果您的项目是中型或大型的,请参阅样板文件。

create a service folder,create service.js -api call goes here(now all you need is to call it when ever you need it) you have a store folder with store.js(index.js) inside store folder create modules folder with you modules.创建一个服务文件夹,创建 service.js -api 调用在这里(现在你只需要在需要时调用它)你有一个 store 文件夹,其中 store.js(index.js) 在 store 文件夹中创建 modules 文件夹你模块。 inside store.js create modules:[user,event...] basically that's it.在 store.js 中创建模块:[user,event...] 基本上就是这样。 edit your modules files event.js user.js.编辑您的模块文件 event.js user.js。 you can add getters,mutations,state,actions.您可以添加吸气剂、突变、state、操作。 just dont forget export const namespaced = true so it`ll go to the global namespace只是不要忘记 export const namespaced = true 所以它会 go 到全局命名空间

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

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