简体   繁体   English

我如何创建一个模块来运行一些样板代码以保持我的代码库干燥

[英]How do I create a module to run some boilerplate code to keep my codebase DRY

I am doing this in all my vue modules 我正在所有vue模块中执行此操作

import axios from 'axios'
axios.defaults.xsrfHeaderName = 'X-CSRFTOKEN'
axios.defaults.xsrfCookieName = 'csrftoken'
axios.defaults.withCredentials = true

I would like to not repeat this and preferably do something like 我不想重复,最好做些类似的事情

import axios from './myaxios'

I tried: 我试过了:

import axios from 'axios'

function myaxios () {
    axios.defaults.xsrfHeaderName = 'X-CSRFTOKEN'
    axios.defaults.xsrfCookieName = 'csrftoken'
    axios.defaults.withCredentials = true
    return axios
}

export default myaxios

Does not work. 不起作用。 What am I doing wrong? 我究竟做错了什么?

You also have to call the function. 您还必须调用该函数。 Or just do it outside the function. 或者只是在函数之外执行。

import axios from 'axios'

function myaxios() {
    axios.defaults.xsrfHeaderName = 'X-CSRFTOKEN'
    axios.defaults.xsrfCookieName = 'csrftoken'
    axios.defaults.withCredentials = true
    return axios
}

export default myaxios()

OR 要么

import axios from 'axios'

axios.defaults.xsrfHeaderName = 'X-CSRFTOKEN'
axios.defaults.xsrfCookieName = 'csrftoken'
axios.defaults.withCredentials = true

export default axios

When you're doing that, you're simply exporting the function myaxios() without actually invoking/calling it. 在执行此操作时,您仅导出函数myaxios()而不实际调用/调用它。 You simply have to do it once, and probably in your main app.js file (or the first entry point of your app): 您只需执行一次,并且可能在主app.js文件(或应用程序的第一个入口点)中:

In the module, you can export the function as you have done. 在模块中,您可以按完成的方式导出功能。 In your entry file, simply import the module: 在您的条目文件中,只需导入模块:

// Import module
import myaxios from '/path/to/myaxious/module';

And then you have to invoke it so that the global settings will be set properly: 然后,您必须调用它,以便正确设置全局设置:

// Invoke module's default exported function
myaxios();

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

相关问题 将我的代码保持为$ onInit和$ onChanges方法的DRY - Keep my code DRY for `$onInit` and `$onChanges` methods 如何在YUI模块中运行代码? - How do I run the code in a YUI module? 如何在React Static Boilerplate中创建webpack导入别名? - How do I create a webpack import alias in React Static Boilerplate? Function 有条件地运行异步代码,但其他时候需要运行同步。 如何保持干燥? - Function conditionally runs async code, but needs to be run sync at other times. How to keep it DRY? Mocking 笑话网络 - 我如何不干燥我的格式化程序? - Mocking jest network - how do I not DRY my formatter? 如何干掉CouchDB视图? - How do I DRY up my CouchDB views? 对于 2 个不同的元素,我有相同的 ngStyle。 如何覆盖它们以保持代码干燥? - I have the same ngStyle for 2 different elements. How to overwrite them to keep the code DRY? 同一循环内有2种方法,我如何保持代码干燥而不重复两次? - 2 methods inside with same loop, how can i keep the code dry not repeating it twice? 当初始化器也是循环设置器时,如何保持循环干燥? - How do I keep a loop DRY when the initializer is also the loop setter? 如何在一行中干掉我的jQuery代码 - How to DRY my jQuery code in a single line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM