我在测试MVC3项目中尝试knockout.js。我有以下结构:
Project Root
Scripts
jquery-1.4.4.js
knockout-1.2.0.debug.js
etc...
Views
Home
Index.cshtml
Shared
_Layout.cshtml
HTMLPage1.htm
In my _Layout。在cshtml文件中,我有以下脚本块:
<script src="/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.tmpl.js" type="text/javascript"></script>
<script src="/Scripts/knockout-1.2.0.debug.js" type="text/javascript"></script>
在我的索引。cshtml文件,我试图在我的脚本块中使用ko对象。它运行得很好,但在VS设计器中,我没有得到ko对象本身的任何智能感知。
如果我在根项目级别打开一个纯HTML文件,并在其中放入一个脚本块,我将获得ko对象的智能感知。
我哪里错了?为什么不能智能感知看到我的。cshtml文件中的ko对象?
我注意到在VS的调试输出中,我得到以下内容:
Javascript智能感知消息:JSIntellisense:Internal/window(18:4): Function expected
Javascript智能感知消息:JSIntellisense:Internal/window(19:8): 'ko' is undefined
Javascript智能感知消息:JSIntellisense:Internal/window(18:4): Function expected
Javascript智能感知消息:JSIntellisense:Internal/window(19:8): 'ko' is undefined
Javascript智能感知消息:JSIntellisense:Internal/window(18:4): Function expected
Javascript智能感知消息:JSIntellisense:Internal/window(19:8): 'ko' is undefined
Javascript智能感知消息:JSIntellisense:Internal/window(18:4): Function expected
Javascript智能感知消息:JSIntellisense:Internal/window(19:8): 'ko' is undefined
等。
啊,我明白了。我需要我的脚本块引用我的。js文件在实际的。cshtml文件。
:
@if (false)
{
<script src="/Scripts/knockout-1.2.0.debug.js" type="text/javascript"></script>
<script src="/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui.js" type="text/javascript"></script>
<script src="/Scripts/jquery.tmpl.js" type="text/javascript"></script>
<script src="/Scripts/knockout-1.2.0.debug.js" type="text/javascript"></script>
}
@if (false)表示它们不会在运行时被包含(因此使用主.cshtml文件中的脚本包含)。