我的 Index.cshtml 文件中有以下内容(来自淘汰网站):
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>
<script type="text/javascript">
// Here's my data model
var ViewModel = function (first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.fullName = ko.computed(function () {
// Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
return this.firstName() + " " + this.lastName();
}, this);
};
ko.applyBindings(new ViewModel("Planet", "Earth")); // This makes Knockout get to work
</script>
我的布局有这一行:
@Scripts.Render("~/bundles/knockout")
在捆绑器配置中正确配置:
bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
"~/Scripts/Libraries/knockout-2.2.1.js"));
Chrome 看到该文件,而 VS 正在给我智能感知,所以我不确定发生了什么。任何淘汰功能都不起作用。
我在 MVC 之外进行了测试(仅使用 html/css),它运行良好。知道发生了什么吗?
编辑:我尝试使用没有捆绑器的直接引用,但它仍然不起作用:
<script type="text/javascript" src="~/Scripts/Libraries/knockout-2.2.1.js"></script>
我收到来自 chrome 的错误:
Uncaught ReferenceError: ko is not defined