I try to keep it short.
我尽量保持简短。
I want to "outsource" my logical functions wich i need over more than 3-4 classes. Therefore i created a .js file and entered the function.
But i did not create a class for it
. Instead i am using the following syntax:我希望“外包”我需要超过3-4个课程的逻辑功能。因此我创建了一个.js文件并输入了该函数。但我没有为它创建一个类。相反,我使用以下语法:
var myMainClassWithTheFunction = require('./myMain...');
var FunctionHandler = {
myFunction(){
myMainClassWiththeFunction.testFunction();
}
}
module.exports = FunctionHandler
Inside of this function i want to use the method of another class -> The class is structured like the following
在这个函数里面我想使用另一个类的方法 - >该类的结构如下
class MyMainClass.. extends Component {
constructor(props){
super(props);
}
static testFunction(
alert("Test");
)
render(){...}
}
module.exports = MyMainClass;
The class MyMainClass... is working (Checked through many other calls - But those just got from other classes)
MyMainClass类正在运行(通过许多其他调用检查 - 但是那些来自其他类的调用)
I also tested
myFunction
fromFunctionHandler
with a function testAlert() wich worked perfectly.我还使用函数testAlert()测试了来自FunctionHandler的myFunction,它完美地工作。
The main issue As far as i try to call the testFunction()
from the MainClass i get the following error:
主要问题就我试图从MainClass调用testFunction()而言,我得到以下错误:
undefined is not a function(evaluating 'myMainClass.testFunction()'
undefined不是一个函数(评估'myMainClass.testFunction()'
- I tried a static/non-static function
我尝试了静态/非静态功能
My question How can i solve this problem - or do you even know better ways to outsource the logical behaviour
我的问题我如何解决这个问题 - 或者你甚至知道更好的方法来外包逻辑行为
2 个解决方案
#1
0
You are exporting the class ListViewMain
instead of MyMainClass
. I assume you have multiple classes in the same file: ListViewMain
and MyMainClass
. Either move MyMainClass
to a separate file and export MyMainClass
or change your export to something like:
您正在导出类ListViewMain而不是MyMainClass。我假设你在同一个文件中有多个类:ListViewMain和MyMainClass。将MyMainClass移动到单独的文件并导出MyMainClass或将导出更改为:
module.exports = {ListViewMain, MyMainClass};
To use it
使用它
var {MyMainClass} = require('./myMainClass');
Alternatively you could use ES6 Syntax:
或者,您可以使用ES6语法:
export class MyMainClass extends Component {...}
To use it
使用它
import {MyMainClass} from './myMainClass';
#2
0
After hours of searching what my issue is here i found out that:
经过几个小时的搜索我的问题在这里,我发现:
If you fill your
js-file
with just avar
like i did. You cannot require otherclasses
in anothervar
and use thevar
in othervar's
(in my case my FunctionHandlervar
)如果你只用我喜欢的var填充你的js文件。你不能在另一个var中要求其他类,并在其他var中使用var(在我的例子中是我的FunctionHandler var)
Solution
I required the classes inside my var so i could use them.
我需要我的var里面的类,所以我可以使用它们。
var FunctionHandler = {
var myMainClassWithTheFunction = require('./myMain...');
myFunction(){
myMainClassWiththeFunction.testFunction();
}
}
module.exports = FunctionHandler
module.exports = FunctionHandler
I am sure many people now roll their eyes if they read this and think 'this is clear' but for me it was a timeintensive issue. Hopefully anyone can help this
我相信很多人现在都会睁大眼睛,如果他们读到这个并认为“这很清楚”,但对我来说这是一个时间密集的问题。希望任何人都可以帮助这个
#1
0
You are exporting the class ListViewMain
instead of MyMainClass
. I assume you have multiple classes in the same file: ListViewMain
and MyMainClass
. Either move MyMainClass
to a separate file and export MyMainClass
or change your export to something like:
您正在导出类ListViewMain而不是MyMainClass。我假设你在同一个文件中有多个类:ListViewMain和MyMainClass。将MyMainClass移动到单独的文件并导出MyMainClass或将导出更改为:
module.exports = {ListViewMain, MyMainClass};
To use it
使用它
var {MyMainClass} = require('./myMainClass');
Alternatively you could use ES6 Syntax:
或者,您可以使用ES6语法:
export class MyMainClass extends Component {...}
To use it
使用它
import {MyMainClass} from './myMainClass';
#2
0
After hours of searching what my issue is here i found out that:
经过几个小时的搜索我的问题在这里,我发现:
If you fill your
js-file
with just avar
like i did. You cannot require otherclasses
in anothervar
and use thevar
in othervar's
(in my case my FunctionHandlervar
)如果你只用我喜欢的var填充你的js文件。你不能在另一个var中要求其他类,并在其他var中使用var(在我的例子中是我的FunctionHandler var)
Solution
I required the classes inside my var so i could use them.
我需要我的var里面的类,所以我可以使用它们。
var FunctionHandler = {
var myMainClassWithTheFunction = require('./myMain...');
myFunction(){
myMainClassWiththeFunction.testFunction();
}
}
module.exports = FunctionHandler
module.exports = FunctionHandler
I am sure many people now roll their eyes if they read this and think 'this is clear' but for me it was a timeintensive issue. Hopefully anyone can help this
我相信很多人现在都会睁大眼睛,如果他们读到这个并认为“这很清楚”,但对我来说这是一个时间密集的问题。希望任何人都可以帮助这个