标签: module.exports

NextJS 在 next.config.js 文件中定义后重定向而不重定向 url

我尝试在我的 NextJS 应用程序中定义重定向。但它不起作用。

这就是我尝试在 next.config.js 文件中执行此操作的方法:

const withImages = require('next-images')
const withPlugins = require("next-compose-plugins");
const optimizedImages = require("next-optimized-images");

module.exports = withPlugins(
    [
        [optimizedImages, {
            inlineImageLimit: 512
        }]
    ],
    {
        async redirects() {
            return [
                {
                    source: "/sales/guest/form",
                    destination: "/",
                    permanent: true
                }
            ]
        },
        env:{
            testEnvVar: 'vallll'
        }
    }
);
Run Code Online (Sandbox Code Playgroud)

这是如何执行此操作的文档: https ://nextjs.org/docs/api-reference/next.config.js/redirects

node.js reactjs next.js module.exports

11
推荐指数
3
解决办法
2万
查看次数

使用模块导出将模板文字与逻辑分开

我正在试验 HTML 字符串模板文字(使用 NodeJS 的电子)。

它运作良好,但看起来一团糟。我想念的是 HTML 和逻辑之间的分离。

这是我所拥有的

module.exports = {
  snippet: (value1, value2) => {
    return `
<div>
  <h1>Hello ${value1 + ' - ' + value2}</h1>
</div>
`;
  },
};
Run Code Online (Sandbox Code Playgroud)

建议输出

我希望我能做的是如下(这不起作用):

module.exports = {
  snippet: (value1, value2) => {
    return require('snippet.html')(value1, value2);
  },
};
Run Code Online (Sandbox Code Playgroud)
<div>
  <h1>Hello ${value1 + ' - ' + value2}</h1>
</div>
Run Code Online (Sandbox Code Playgroud)

为什么它不起作用

字符串是扁平的,不能带参数

我可以从 HTML 文件中以字符串形式获取 HTML。问题是它将是“扁平的”,我不能再使用模板文字中的逻辑了。

eval 邪恶而缓慢

我读过可以将 HTML 作为字符串运行eval以使其再次工作。我还读到它不利于安全并且速度很慢。

其他注意事项

  • 我使用 nodeJS 所以我需要module.exports在 js 文件中使用。
  • 我使用嵌套的片段,所以模板文字仍然可以正常工作很重要。它应该能够接受变量、函数等。
  • 我不介意新的 …

html javascript templates literals module.exports

9
推荐指数
1
解决办法
225
查看次数

有没有办法从 JavaScript 文件中获取所有导出的名称?

假设我有一个 JavaScript 文件(lib.js),它导出一些类、函数等。

export class Employment {
    id = "";
    startDate = "";
    endDate = "";
    companyEmailAddress = "";
};


export function HolidayPopupContainer() {
    return (<div id="#HolidayPopupContainer" />);
}

export default withStyles(styles)(Button);

export default class DayView extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            readOnly: false
        }


    };
export const Status = [
    "Select Status",
    "Pending",
    "Approved",
    "Rejected",
    "All"
]
Run Code Online (Sandbox Code Playgroud)

现在该文件作为 lib.js 保存在其中一个文件夹中。由于该文件具有导出功能,因此导出的文件可以由其他 JavaScript 文件导入。

有没有一种方法可以让我们以编程方式从 JavaScript 文件中找到导出的完整列表。

预期的输出应该是列表/数组/json等。上述js文件的输出将是

Employment
HolidayPopupContainer
withStyles
DayView 
Status 
Run Code Online (Sandbox Code Playgroud)

Object.keys在这里有什么帮助吗?

javascript object module.exports

7
推荐指数
1
解决办法
6500
查看次数

在 React ES6 中使用 module.exports

我正在尝试使用 module.exports 创建一个“全局”函数,我可以在我的 React 项目中使用它来使用 Axios 进行 API 调用。

我尝试了以下...

import axios from 'axios';

module.exports = {
    fetchApi: function (endPoint) {
        var encodedURI = window.encodeURI('http://localhost:3001/' + endPoint);

        return axios.get(encodeURI)
            .then(function (response) {
                return response.data
            })
    }
}

//call function in other components like this
componentDidMount () {
    fetchApi.fetchApi(this.setState.endPoint)
    .then(function (endPoint) {
        console.log("API endpoint: " + endPoint)
    })
}
Run Code Online (Sandbox Code Playgroud)

这将返回错误... 'fetchApi' 未定义 no-undef

我知道通常您可以使用导出默认值导入和导出组件,但我认为如果使用 module.export,则不需要这样做。

另外,如果我尝试像这样导入...

import fetchApi from '../../../utils/api'
Run Code Online (Sandbox Code Playgroud)

我收到以下错误...尝试导入错误:“../../../utils/api”不包含默认导出(作为“fetchApi”导入)。

任何帮助表示赞赏。谢谢。

编辑:

从 'axios' 导入 axios;

解决方案(编辑)...

api.js

const api …
Run Code Online (Sandbox Code Playgroud)

api ecmascript-6 reactjs axios module.exports

5
推荐指数
1
解决办法
1万
查看次数

不清楚为什么 Data.Ratio 中的函数没有公开以及如何解决

我正在使用Data.Ratio(连分数的收敛)实现一个算法。但是,我遇到了两个障碍:

  • 该算法从分数开始1%0- 但这会引发零分母异常。
  • 我想模式匹配构造函数 a :% b

我正在探索hackage。一个特别是源似乎正在使用这些功能(例如定义infinity = 1 :% 0,或模式匹配numerator)。

作为初学者,我也很困惑在哪里确定(%),numerator等暴露给我,但不是infinity和(:%)。

我已经使用整数元组做了一个肮脏的解决方法,但是为如此微不足道的事情重新发明轮子似乎很愚蠢。也很高兴了解如何阅读公开哪些功能的源代码。

haskell module numeric module.exports

5
推荐指数
1
解决办法
80
查看次数

JavaScript:未定义模块/要求

I'd like to have Jest testing for some JavaScript code that I'm using in the frontend of my web app. From what I understand, I need to export and import functions from my module so I can test them in Jest, and Jest (only) supports CommonJS module syntax out of the box. As a result, my code looks as follows:

<!-- index.html -->
<!DOCTYPE html>
<html><body><div>Hello World</div>
<script src="./utils.js">
    console.log('foo: ' + foo());
</script>
</body></html>
Run Code Online (Sandbox Code Playgroud)
// utils.js
function foo() {return …
Run Code Online (Sandbox Code Playgroud)

javascript commonjs jestjs module.exports

5
推荐指数
1
解决办法
1742
查看次数

在循环依赖 NodeJS 中访问 module.exports 不存在的属性

我在 NodeJS 中使用 module.exports 时遇到了一些问题,并且我遵循了多个指南,并且我几乎可以肯定我做得正确。我必须脚本、main.js 和 event.js。我试图将 main.js 的函数共享到 event.js,但它不起作用。这是代码:

Main.js

function Scan(){
    if(fs.readdirSync('./events/').length === 0){
        console.log(colors.yellow('Events Folder Empty, Skipping Scan'))
    } else {
        var events = fs.readdirSync('./events/').filter(file => file.endsWith('.json'))
                for(const file of events){
                    let rawdata = fs.readFileSync('./events/' + file);
                    let cJSON = JSON.parse(rawdata);
                }
                events.sort()
                tevent = events[0]
                StartAlerter()
                }
}

module.exports = { Scan };
Run Code Online (Sandbox Code Playgroud)

事件.js

const main = require('../main')

main.Scan;
Run Code Online (Sandbox Code Playgroud)

这会返回错误:

(node:19292) Warning: Accessing non-existent property 'Scan' of module exports inside circular dependency
(Use `node --trace-warnings ...` …
Run Code Online (Sandbox Code Playgroud)

javascript function node.js module.exports

4
推荐指数
1
解决办法
3万
查看次数

导出默认新对象

是否保证export default new object(),当对象是某种类型(例如日期)时,始终返回相同的对象?

// date.js
export default new Date()
// foo.js
import date from './date'
// bar.js
import date from './date'
Run Code Online (Sandbox Code Playgroud)

是否可以预期dateinfoo.js和dateinbar.js是等价的。那么import date from './date'是单例吗?

上面的内容export default new Date()相当于module.exports = new Date()es6 之前的时代吗?

javascript export node.js module.exports

3
推荐指数
1
解决办法
1411
查看次数

导出的类中可以有私有成员吗?

我知道 Node.js 中的所有内容都是私有的,除非您将其导出。

我找不到导出整个类的其他方法,但我的目标是使privateMethod()类导入和实例化的位置不可访问。

#!/usr/bin/env node

'use strict'

module.exports = class DBConnector {
    constructor() {}
    publicMethod() {}
    privateMethod() {}
}
Run Code Online (Sandbox Code Playgroud)

有没有解决方案,或者我必须将其公开?

预先感谢您的任何帮助。

javascript private class node.js module.exports

2
推荐指数
1
解决办法
2836
查看次数