Sho*_*v3_ 3 javascript jsx reactjs
我有一个something.js具有功能的文件:
someFunction: function(arg1, arg2){
//code blocks
}
Run Code Online (Sandbox Code Playgroud)
在我的app.js文件中,我想在app类中调用这个函数。我已经导入了something.js这样的文件import { someFunction } from './something.js';。现在我试图在app课堂上的一个函数中使用它
var res = someFunction("abc", 2);
console.log(res);`
Run Code Online (Sandbox Code Playgroud)
我收到一个错误 Uncaught TypeError: (0 , _something.someFunction) is not a function
一些帮助将不胜感激。
为了导入一些东西,你需要从另一个模块中导出它。例如,您可以export class YourComponent extends React.Component在 something.js 中。
然后在另一个文件中你可以 import { YourComponent } from './something'
例如,你可以something.js做类似的事情
const MyClass = {
methodName() { return true; }
}
export { MyClass as default } // no semi-colon
Run Code Online (Sandbox Code Playgroud)
然后在另一个文件中你可以
import WhateverIWant from 'something';
WhateverIWant.methodName(); // will return true
Run Code Online (Sandbox Code Playgroud)
编辑:此处提供了包含大量示例的深入解释。
你需要这样写:
something.js 文件 -
module.exports = {
A: funtion(){
},
B: funtion(){
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样导入:
import {A} from 'something';
Run Code Online (Sandbox Code Playgroud)
或者像这样使用它:
something.js 文件 -
export A(){
}
export B(){
}
Run Code Online (Sandbox Code Playgroud)
然后像这样导入:
import {A} from 'something';
Run Code Online (Sandbox Code Playgroud)
阅读这篇文章: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'
让我知道这是否有效!:)
| 归档时间: |
|
| 查看次数: |
19278 次 |
| 最近记录: |