【发布时间】:2016-10-28 08:18:07
【问题描述】:
您好,我有一个使用 Aurelia CLI 运行的 Aurelia Web 应用程序。
之前,我使用 SystemJS 作为模块加载器,但由于我想强制执行我的应用程序的 Content-Security-Policy 以不允许“不安全评估”,因此我按照 @ 中的建议更改为 Aurelia CLI 987654322@.
我的应用是以前的 Durandal 应用,我将其转换为 Aurelia,因此它广泛使用了 Knockout。我正在使用 aurelia-knockout 插件(您可以在我的 main.js 中看到)
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.plugin('aurelia-knockout');
return aurelia.start().then(() => aurelia.setRoot());
}
我已经安装了 knockout 和 knockout-secure-binding npm 包,并在 aurelia.json 文件中配置了它们:
"dependencies": [
"aurelia-binding",
"aurelia-bootstrapper",
"aurelia-dependency-injection",
"aurelia-event-aggregator",
"aurelia-framework",
"aurelia-history",
"aurelia-history-browser",
"aurelia-loader",
"aurelia-loader-default",
"aurelia-logging",
"aurelia-logging-console",
"aurelia-metadata",
"aurelia-pal",
"aurelia-pal-browser",
"aurelia-path",
"aurelia-polyfills",
"aurelia-route-recognizer",
"aurelia-router",
"aurelia-task-queue",
"aurelia-templating",
"aurelia-templating-binding",
{
"name": "text",
"path": "../scripts/lib/text"
},
{
"name": "aurelia-templating-resources",
"path": "../node_modules/aurelia-templating-resources/dist/amd",
"main": "aurelia-templating-resources"
},
{
"name": "aurelia-templating-router",
"path": "../node_modules/aurelia-templating-router/dist/amd",
"main": "aurelia-templating-router"
},
{
"name": "aurelia-knockout",
"path": "../node_modules/aurelia-knockout/dist/amd",
"main": "aurelia-knockout"
},
{
"name": "knockout",
"path": "../node_modules/knockout/build/output",
"main": "knockout-latest"
},
{
"name": "knockout-secure-binding",
"path": "../node_modules/knockout-secure-binding/dist",
"main": "knockout-secure-binding.min"
},
{
"name": "aurelia-testing",
"path": "../node_modules/aurelia-testing/dist/amd",
"main": "aurelia-testing",
"env": "dev"
}
]
我知道我必须使用以下代码(就像我在旧的 Durandal 应用中所做的那样)来激活淘汰赛安全绑定:
var options = {
attribute: "data-bind", // default "data-sbind"
globals: window, // default {}
bindings: ko.bindingHandlers, // default ko.bindingHandlers
noVirtualElements: false // default true
};
ko.bindingProvider.instance = new ko.secureBindingsProvider(options);
但我不知道如何在这个新的 Aurelia 应用程序中使用它,或者一般来说如何使 aurelia-knockout 插件使用安全绑定版本的敲除。 我试过像这样修改我的 main.js 文件:
export function configure(aurelia) {
var options = {
attribute: "data-bind", // default "data-sbind"
globals: window, // default {}
bindings: ko.bindingHandlers, // default ko.bindingHandlers
noVirtualElements: false // default true
};
ko.bindingProvider.instance = new ko.secureBindingsProvider(options);
aurelia.use
.standardConfiguration()
.developmentLogging()
.plugin('aurelia-knockout');
return aurelia.start().then(() => aurelia.setRoot());
}
甚至调整 npm 模块中的 aurelia-knockout.js 文件,在此行之前插入安全绑定定义
ko.applyBindings(executionContext, this.element);
但是,即使我没有遇到任何构建错误(我使用“au build”命令构建)以上都不起作用。
更新
我直接在里面插入了ko安全绑定初始化代码
node_modules\aurelia-knockout\dist\amd\aurelia-knockout-custom-attribute.js
我在第一行添加了对安全绑定模块的引用
define(['exports', 'aurelia-dependency-injection', 'aurelia-templating', 'knockout-secure-binding'], function (exports, _aureliaDependencyInjection, _aureliaTemplating, _knockoutSecureBinding) {
现在我收到此错误:
Unhandled rejection TypeError: ko.secureBindingsProvider is not a constructor
好像没有找到/加载安全绑定模块。
无论如何,我不认为使用 node_module 的“内置”版本是理想的解决方案,我只是在寻找一种使其工作的方法。仍然期待更好的建议。
【问题讨论】:
标签: knockout.js aurelia