Having trouble compiling Typescript files on Angular 2 using Visual Studio 2015?

I recently followed Zach's advice and set up a new project in Visual Studio 2015 using .NET 5 to run Angular 2 with Typescript. Everything seemed to be working fine, but I encountered a small issue:

MyApp.ts :

import { Component } from "angular2/core";

@Component({
    selector: "my-app",
    template: `
        <div>Hello from Angular 2</div>
    `
})
export class MyApp {
    public constructor() {
    }
}

After updating the template to display different text, such as

<div>Some text here</div>
, recompiling the project, and running it in the browser, the old template text - Hello from Angular 2 - still appeared. Upon checking the compiled MyApp.js file, the changes were not reflected there either.

(I attempted to rebuild the project multiple times, as well as closing and reopening Visual Studio - the MyApp.ts file was only compiled once, when it was initially created.)

Additional files: (that may help in resolving the issue)

tsconfig.json :

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ]
}

Where could the problem be originating from? Angular? Typescript? Visual Studio 2015? In my opinion, it might have something to do with Typescript and its compilation process. Any suggestions?

P.S: Please note that this implementation does not function properly in Internet Explorer!

Progress: I have tried cleaning, rebuilding, and building the project again - while it does compile the .ts files, it doesn't provide a solution to the issue...

Answer №1

Using visual studio for my project and I had to tweak my tsconfig file. I found that adding the "compileOnSave" attribute made a big difference. Here's what my updated tsconfig looks like:

{
  "compileOnSave": true,    <---- This new line did the trick 
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

Answer №2

Setting up Angular 2 Template in Visual Studio 2015 with Update 3

Quick guide to getting started:

  • Begin by creating an empty ".net core or ASP.Net" project.
  • Your project structure should resemble the following...

https://i.sstatic.net/l64Vw.png

  • Include a package.json file and add all necessary dependencies.

package.json

{
  "version": "0.0.1",
  "name": "vs2015-angular2-template",
  "dependencies": {
    "angular2": "2.0.0-beta.15",
    "systemjs": "^0.19.27",
    "es6-promise": "^3.1.2",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "^0.6.12",
    "bootstrap": "^3.3.6",
    "jquery": "^2.2.3"
  },
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-clean": "^0.3.2"
  }
}
  • Create a "scripts" folder where all *.ts files and tsconfig.json reside (all resulting *.js files will be moved to "../wwwroot/appScripts/")

tsconfig.json

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "outDir": "../wwwroot/appScripts/",
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",    
    "inlineSources": true
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}
  • Add a Gulp file to handle moving necessary files from node_modules to the lib folder under wwwroot
  • Create an index.html file and include references to required files from the lib folder under wwwroot

If you prefer manual package installation:

npm install

Important: Ensure all updates related to Visual Studio 2015, VS2015 Update 3, and "DotNetCore.1.0.0.RC2-VS2015Tools.Preview1" are installed to run this solution successfully.


Download the "Angular2 Template for Visual Studio 2015 with Update 3" from:

https://github.com/narottamgoyal/Vs2015Angular2Template.git

Answer №3

To enable automatic compilation of TypeScript files not included in the project in Visual Studio, navigate to options and click on typescript-> project. Check the box that says "Automatically compile typescript files that are not a part of project."

Additionally, select the radio button option that says "Use AMD code generation for modules that are not part of the project."

By following these steps, new JavaScript files should be generated for you.

If you encounter similar issues, refer to this thread for help: How to resolve TypeScript not generating JavaScript in Visual Studio Community Edition 2015

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Stop any modifications to the input data

We are currently in the process of building an angular application with Angular 5. Typically, our components have input and output parameters defined as shown below: export class MultipleItemPickerComponent implements OnInit { @Input() itemPickerHeader ...

Why do my messages from BehaviorSubject get duplicated every time a new message is received?

Currently, I am using BehaviorSubject to receive data and also utilizing websockets, although the websocket functionality is not relevant at this moment. The main issue I am facing is why I keep receiving duplicated messages from BehaviorSubject. When exa ...

Exploring how Django utilizes sessions in conjunction with Angular's cookies while integrating with Django Rest

Looking for a comprehensive example demonstrating how Django Rest Framework can send a session key to Angular for storage as a cookie. I've been trying to figure this out for days... I have Django running on port 8000 and in Angular's ng serve, ...

What is the best way to disable a collapsed section in VS Code using comments?

I'm wondering how to properly comment out a "folded" section of code using VS Code. For instance, I want to comment out the collapsible region: if (a == b) { dance(); } I am familiar with the keyboard shortcut for folding regions: Ctrl + Shift + ...

Tips for sending parameters in Next.js without server-side rendering

I followed the documentation and tried to pass params as instructed here: https://nextjs.org/docs/routing/dynamic-routes However, I encountered a strange issue where the received params are not in string format. How is it possible for them to be in an arr ...

Unleashing the Power of RXJS: Discovering the Magic of connecting Events and Tapping into Executions with retrywhen

Whenever Angular attempts to establish a connection, I aim to display "Connecting". Although I can achieve this on the initial connection, I am uncertain about how to accomplish it when using retryWhen(). It is essential for me to intercept the actual exec ...

Disabling dates in Kendo Date Time Picker (Angular): An easy guide

<input id="startDate" kendo-date-time-picker k-ng-model="vm.startDate" k-on-change="vm.updateStartDate()" required /> Can someone please explain how to incorporate disabled dates into this date picker without utilizi ...

In response to resolving an HTTP header issue with a status of 200 ok during API testing with Postman, what steps can be taken

Hello everyone, I am new to the world of Angular and facing some issues while learning. Following a tutorial on YouTube, I tried to replicate the process with a few modifications. Initially, my get API worked fine when tested with Postman, and the post API ...

Ways to retrieve data object within an HTMLElement without relying on jQuery

Within my web application, I have successfully linked a jQuery keyboard to a textbox. Now, I am seeking a way to explicitly call the keyboard.close() function on the keyboard as I am removing all eventListeners from the text field early on. To achieve thi ...

In TypeScript, at what level should the timeout be specified?

I'm currently working on writing a debounce function in TypeScript, but I'm feeling uncertain about the type that should be assigned to a variable used with setTimeout. This is the snippet of my code: function debounced(func: () => void, wait ...

Inform the PHP backend that the browser has been closed by the frontend Angular application

Currently, I am working on an Angular project that is interacting with a backend project created using PHP and ZF3. I am trying to figure out the most efficient method of informing the backend project when the user closes the browser window. Initially, I ...

Exploring .ENV Variables using Angular 2 Typescript and NodeJS

After completing the Angular2 Quickstart and tutorials, I'm now venturing into connecting to a live REST Api. I need to include authentication parameters in my Api call, and from what I've gathered, it's best practice to store these paramete ...

Enhance Summernote functionality by creating a custom button that can access and utilize

Using summernote in my Angular project, I am looking to create a custom button that can pass a list as a parameter. I want to have something like 'testBtn': this.customButton(context, listHit) in my custom button function, but I am unsure how to ...

Retrieve information from an XML document

I have some XML content that looks like this: <Artificial name="Artifical name"> <Machine> <MachineEnvironment uri="environment" /> </Machine> <Mobile>taken phone, test when r1 100m ...

react-router: The 'history' type is not found in the current context

Here is some code snippet from App.tsx: interface AppProps{ history: any } export default class App extends React.Component<AppProps,...> { public state = { target: this.props.history.push('/') }; private route() { if (! ...

Retrieving the component's values when utilizing the `<ng-content>` directive

Seeking a technique for accessing the values of a component when utilizing <ng-content>: import {Component} from '@angular/core'; @Component({ selector: 'home-page', template: `<person-box>{{name}}</person-box> & ...

What is the process for connecting an Angular .ts file with an existing HTML page?

As I finish up the html pages for my website, I find myself in need of integrating Angular to complete the project. My experience with Angular so far has been with ionic apps, where the CLI generates the html, ts, and css pages. However, I am curious if ...

Using getter functions and Visual Studio for TypeScript

In my TypeScript classes in Visual Studio, I have been implementing getter functions. I find that using getter functions helps to clean up my code, although there is one issue that I would like to address. class Foo { doWork(){ console.log(this.bar ...

Limiting the data types of array elements applies to variables, not to indexes

type Soccer = { ball: string } type Basketball = { jump: string } type Data = Soccer[] | Basketball[] if ('ball' in data[index]) { // type guard not effective here. <MyComponent something={data[index]} /> // data: Soccer[] | Basketball[] ...

Is there a method to dynamically incorporate takeUntil into observables through programming?

I'm working on creating an AutoUnsubscribe class that will handle all subscriptions used in components with the @AutoUnsubscribe decorator and unsubscribe them in ngOnDestroy. I have attempted to retrieve all variables and add takeUntil, which somewh ...