简体   繁体   English

KnockoutJs和模板 - Visual Studio中没有代码突出显示/完成?

[英]KnockoutJs and Templates - No code highlighting / completion in visual studio?

I have been playing with the tutorials on the knockout site and have enjoyed working with it. 我一直在玩淘汰网站上的教程,并喜欢使用它。

So i decided to make a simple site with it. 所以我决定用它制作一个简单的网站。 I was saddened to notice that i lose a lot of the support from the IDE when working with the javascript templates (highlighting, code completion) 我很遗憾地注意到在使用javascript模板时我失去了IDE的大量支持(突出显示,代码完成)

Example template: 示例模板:

<script type="text/html" id="taskTemplate">
    <li>
        <input type="checkbox" data-bind="checked: isDone" />
        <input data-bind="value: title, enable: !isDone()" />
        <a href="#" data-bind="click: remove">Delete</a>
    </li>
</script>

Is this something you have to just swallow or is it avoidable / fixable? 这是你必须吞下的东西还是可以避免/可以修复的东西? Templates seem to be one of the most used ways of building up the page and so i would prefer to have the support from the IDE. 模板似乎是构建页面最常用的方法之一,所以我更希望得到IDE的支持。

To get around this I create two html helpers for the begining of my script tag and the end of my script tag. 为了解决这个问题,我创建了两个html助手,用于开始我的脚本标记和脚本标记的结尾。 Something like: 就像是:

<% Html.BeginTemplate(new { id = "features-template" }); %>
    <li>
        <input type="checkbox" data-bind="checked: isDone" />
        <input data-bind="value: title, enable: !isDone()" />
        <a href="#" data-bind="click: remove">Delete</a>
    </li>
<% Html.EndTemplate(); %>

Keith proposed very good and intelligent solution. 基思提出了非常好的智能解决方案。 However I would like to let you know KO 1.3 has native templating engine. 不过我想让你知道KO 1.3有本机模板引擎。 It is avaliable trough new bindings: 通过新绑定可以实现:

  • if 如果
  • ifnot 如果不
  • with
  • foreach! 的foreach!

you can read more in Steve Sanderson's announce: http://blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/ (1. Control flow bindings) 您可以在Steve Sanderson的宣布中阅读更多内容: http//blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/ (1。控制流绑定)

So your example will have this look: 所以你的例子会看起来像这样:

<ul data-bind="foreach: tasks">
<li>
    <input type="checkbox" data-bind="checked: isDone" />
    <input data-bind="value: title, enable: !isDone()" />
    <a href="#" data-bind="click: remove">Delete</a>
</li>
</ul>

It is the <script type="text/html"...> tag that is stopping Visual Studio highlight this section of your markup as html. 正是停止Visual Studio的<script type="text/html"...>标记突出显示标记的这一部分为html。 Therefore, this question/answer seems to be the closest you are going to get to an answer. 因此,这个问题/答案似乎是你最接近答案的答案。

Visual Studio - Markup syntax highlighting inside script[type:txt/html] Visual Studio - 在脚本内部突出显示标记语法[type:txt / html]

I used the accepted answer here, and since it helped me, if anyone wants actual code, here you go: 我在这里使用了接受的答案,因为它帮助了我,如果有人想要实际的代码,你可以去:

public string StartTemplate(string id)
{
    return ("<script type=\"text/html\" id=\"" + id + "\">\r\n");
}

public string EndTemplate()
{
    return ("</script>\r\n");
}

HTML: HTML:

<%= StartTemplate("WidgetTemplate")%>
 ...actual template HTML goes here...
<%= EndTemplate() %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio代码JavaScript代码完成 - visual studio code JavaScript code completion Swing中的代码完成和语法突出显示 - Code completion and Syntax Highlighting in Swing 在 Visual Studio Code 中设置正确的 React 代码突出显示? - Setting up proper React Code highlighting in Visual Studio Code? 构造函数方法的 Visual Studio Code Intellisense 代码完成 - Visual Studio Code Intellisense code completion for constructor function methods 使用JSDoc时在Visual Studio代码中突出显示注释 - Comment highlighting in Visual Studio Code when using JSDoc Visual Studio Code 语法突出显示不适用于 JavaScript 和 TypeScript - Visual Studio Code syntax highlighting is not working for JavaScript and TypeScript Visual Studio Code - 缺少 JavaScript 的大多数语法突出显示 - Visual Studio Code - Missing most syntax highlighting for JavaScript Visual Studio - 为HTML模板和语法突出显示使用自定义脚本标记类型 - Visual Studio - Using Custom Script Tag Type for HTML Templates and Syntax Highlighting 就像ReSharper for Visual Studio一样,在WebStorm中完成代码 - Code completion in WebStorm just like ReSharper for Visual Studio Discord JS 模块导出 Visual Studio 代码完成/建议 - Discord JS Module Export Visual Studio Code Completion / Suggestions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM