Typescript 1.5 allows you to import modules using the import statement.
import {DataService} from"../Modules/jQueryAjaxDataService";
In Visual Studio 2015, the Build: Cannot compile modules unless the '--module' flag is provided stops me from proceeding.
Here are the steps to take to user AMD module loading in VS 2015.
- Edit the TypeScript Build Module System options in the properties of the web project. Make sure to change it for debug and release.
- I found this answer on StackOverflow.
- Edit the project file with a text editor and “just edit the file manually, change "'$(Configuration)|$(Platform)' == 'Debug|Any CPU'" to "'$(Configuration)' == 'Debug'"> and it should work again.” There is an issue about this on TypeScript
- Include the AMD system of your choice for the browser to use. TypeScript will compile the JavaScript with define( in the files.
- RequireJs is a good option.
- You may have to change some code if you are using document ready then using the script. Especially if the JavaScript files were just included in the source or using BundleConfig.cs before.
- Look at my GitHub Ajax example: in the Home/Index.cshtml. This works, but the path may not always be correct, so figuring out setting the require.config({ baseUrl: ‘ // check out http://stackoverflow.com/questions/1288046/how-can-i-get-my-webapps-base-url-in-asp-net-mvc for some idea.
My example is:
<script type="text/javascript">
ready(function () {
// need to make sure indexTSViewModel is available for require first
// beware of the paths!
require(['Scripts/ViewModels/indexTsViewModel', 'Scripts/modules/jQueryAjaxDataService'],
function (IndexTsViewModel, JQueryAjaxDataService) {
var typeScriptVm = new IndexTsViewModel.IndexTsViewModel(new JQueryAjaxDataService.JQueryAjaxDataService($));
var tsDiv = document.querySelector("#knockoutAreaWithTS");
ko.applyBindings(typeScriptVm, tsDiv);
});
});
</script>
Hopefully this will help you get AMD and import in TypeScript working in Visual Studio 2015.