Visual Studio Intellisense for Custom javascript f

2019-05-17 17:07发布

问题:

I've create a custom functions in JavaScript for ease coding cause its too repetitive to type those function again and again.

What I do is I created an external JavaScript and linked it to my _Layout.cshtml. I've successfully called them without any problem but what I wanted now is to have those custom function to have intellisense.

global_functions.js

function ZeroPrefixFormat(str, len) {
     str = str.toString();
     return str.length < len ? ZeroPrefixFormat("0" + str, len) : str;
     // OUTPUT : 10 -> 00010 (DIFFERS FROM THE GIVEN LENGTH)
}

function MoneyFormat(amount) {
     amount = amount.toString();
     return Number(amount).toLocaleString('en');
     // RETURN raw number to money format example. 123456789.10 -> 123,456,789.10
}

custom.cshtml

<script>
 console.log(MoneyFormat(123456789));
<script>

So when I try to type Money it shows intellisense.

回答1:

You can include Intellisense in following two ways,

  1. Add a JavaScript file to the global Visual Studio references
  2. Add the reference directly to the top of the Javascript file

Add a .js file to the global references

Add a reference to the JS file in Tools -> Options like this,

Make sure to chose Implicit (Web) in the Reference Group dropdown. Otherwise it won't take effect for web projects.

Reference Link: http://madskristensen.net/post/improved-javascript-intellisense-in-visual-studio

Add the reference directly to the top of the .js file

You can add the reference directly to the top of the Javascript file with the relative path as below.

/// <reference path="../scripts/jaydata.js" />