如何在 reactjs 中使用另一个 js 文件中的函数?
how to use a function from another js file in reactjs?
我有一个文件 something.js
,它有一个函数:
someFunction: function(arg1, arg2){
//code blocks
}
在我的 app.js
文件中,我想在 app
class 中调用此函数。我已经像这样 import { someFunction } from './something.js';
导入了 something.js
文件。现在我试图在 app
class
中的函数中使用它
var res = someFunction("abc", 2);
console.log(res);`
我遇到一个错误 Uncaught TypeError: (0 , _something.someFunction) is not a function
一些帮助将不胜感激。
为了导入某些东西,您需要从其他模块导出它。
例如,您可以 export class YourComponent extends React.Component
in something.js.
然后在另一个文件中你可以import { YourComponent } from './something'
例如,您可以在 something.js
中执行类似
的操作
const MyClass = {
methodName() { return true; }
}
export { MyClass as default } // no semi-colon
然后在另一个文件中你可以
import WhateverIWant from 'something';
WhateverIWant.methodName(); // will return true
编辑:
包含大量示例的深入解释 is available here.
你需要这样写:
something.js
文件 -
module.exports = {
A: funtion(){
},
B: funtion(){
}
}
然后像这样导入:
import {A} from 'something';
或者这样使用:
something.js
文件 -
export A(){
}
export B(){
}
然后像这样导入:
import {A} from 'something';
阅读这篇文章:https://danmartensen.svbtle.com/build-better-apps-with-es6-modules
您可以这样做:在您的 something.js
文件中:module.exports = function(){}..
在你的 app.js
文件中:
const functionName = require('./path_to_your_file');
或export somethingFunction = {}
并在app.js
中:
import { somethingFunction } from './path_to_your_file'
或最后一个:export default somethingFunction = {}
和 app.js
:
import whateverNameYouChoose from './path_to_your_file'
如果成功了,请告诉我! :)
在您的 something.js
文件中,您可以在文件底部附近添加 export someFunction
。这将允许您使用之前的 import { someFunction } from './something.js';
导入该函数。
我有一个文件 something.js
,它有一个函数:
someFunction: function(arg1, arg2){
//code blocks
}
在我的 app.js
文件中,我想在 app
class 中调用此函数。我已经像这样 import { someFunction } from './something.js';
导入了 something.js
文件。现在我试图在 app
class
var res = someFunction("abc", 2);
console.log(res);`
我遇到一个错误 Uncaught TypeError: (0 , _something.someFunction) is not a function
一些帮助将不胜感激。
为了导入某些东西,您需要从其他模块导出它。
例如,您可以 export class YourComponent extends React.Component
in something.js.
然后在另一个文件中你可以import { YourComponent } from './something'
例如,您可以在 something.js
中执行类似
const MyClass = {
methodName() { return true; }
}
export { MyClass as default } // no semi-colon
然后在另一个文件中你可以
import WhateverIWant from 'something';
WhateverIWant.methodName(); // will return true
编辑: 包含大量示例的深入解释 is available here.
你需要这样写:
something.js
文件 -
module.exports = {
A: funtion(){
},
B: funtion(){
}
}
然后像这样导入:
import {A} from 'something';
或者这样使用:
something.js
文件 -
export A(){
}
export B(){
}
然后像这样导入:
import {A} from 'something';
阅读这篇文章:https://danmartensen.svbtle.com/build-better-apps-with-es6-modules
您可以这样做:在您的 something.js
文件中:module.exports = function(){}..
在你的 app.js
文件中:
const functionName = require('./path_to_your_file');
或export somethingFunction = {}
并在app.js
中:
import { somethingFunction } from './path_to_your_file'
或最后一个:export default somethingFunction = {}
和 app.js
:
import whateverNameYouChoose from './path_to_your_file'
如果成功了,请告诉我! :)
在您的 something.js
文件中,您可以在文件底部附近添加 export someFunction
。这将允许您使用之前的 import { someFunction } from './something.js';
导入该函数。