我在玩 Aurelia,看起来很不错,我在一些项目中使用 Durandal,这绝对适合我的需求。
使用 EC6 中的新类定义非常棒。但现在我正在准备一些东西,其中我需要使用带有 requireJs 的经典 AMD 模块,如下所示:
define("testModule",
[],
function() {
"use strict";
console.log('testModule loaded');
var testModule = function() {
var that = this;
this.variable = 10;
that.getVariable = function(){
alert('function executed ' + that.variable);
};
}
return testModule;
});
根据 Aurelia 的文档,我发现可以使用 testModule 之类的东西作为 ViewModel,事实上 viewModel 已在 Durandal 应用程序中使用。
但经过一番尝试后,我无法使其正常工作。
有人遵循什么想法或方法来做到这一点? 最重要的是,这可能吗?我认为只是为了确认兼容。
谢谢。
最佳答案
我们已经对此进行了一些尝试。这就是我们的想法。该作品基于 Skeleton Navigation App:
- 在项目根目录中创建一个文件夹
amd
。 - 将您的原始 Durandal 虚拟机(来自您的示例)按原样复制到其中。
在
src
内创建一个 Wrapper VM,也命名为testmodule.js
,内容如下:export class TestModule { constructor() { } activate() { return System.import('../amd/testmodule').then( (result) => { if(typeof result === 'function') Object.assign(this, new result()); else Object.assign(this, result); }); } }
- 享受吧:)
这本质上是包装您的原始 DurandalVM 并将其公开为新的 AureliaVM。这只是一个开始,肯定有一些事情需要考虑,比如尊重 Durandals 生命周期等。但我想这是一个好的开始
关于javascript - 使用 AMD 模块作为 Aurelia ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28335502/