【发布时间】:2019-02-07 12:06:03
【问题描述】:
我是打字稿的新手。我一直在尝试对它使用 knockout.mapping,但是,我无法让它工作。
我已经安装了knockout和knockout.mapping的库,以及这两个库中的@types,即使这样也不能工作。
我在 laravel 项目中使用 typescript,我正在使用 laravel mix 来生成 javascript 文件。
我得到了下一个sn-p:
///<reference path="../../../../node_modules/@types/jquery/index.d.ts"/>
///<reference path="../../../../node_modules/@types/knockout/index.d.ts"/>
///<reference path="../../../../node_modules/@types/knockout.mapping/index.d.ts"/>
import * as ko from "knockout";
import * as $ from "jquery";
$(function(){
//this is only a test to check if ko.mapping exists on the ko object. And no, it doesn't appears.
console.log("Message from jQuery Done", (ko));
});
class MyModel {
_data: any;
constructor(the_data: object)
{
let self = this;
let example_observable = ko.observable(); //This is fine
ko.mapping.fromJS(the_data, self._data); // This fails with the error -> Cannot read property 'fromJS' of undefined
}
}
let myModel = new MyModel({"x": "y"});
ko.applyBindings(myModel);
我的 package.json 包含了 knockout 和 knockout.mapping 依赖项。
"@types/jquery": "^3.3.6",
"@types/knockout": "^3.4.58",
"@types/knockout.mapping": "^2.0.33",
"ajv": "^6.5.2",
"knockout": "^3.4.2",
"knockout.mapping": "^2.4.3",
我不明白我做错了什么。
感谢您的帮助。
提前致谢。
【问题讨论】:
标签: typescript knockout.js knockout-mapping-plugin laravel-mix