Error: No templates were found for MyAppComponent

I have been following Angular 2 Quickstart , but getting the following error:

Error creating Token (AppView) !. ORIGINAL ERROR: Error: No template for MyAppComponent

This is the corresponding code:

import {Component, View} from 'angular2/core'; import {bootstrap} from 'angular2/platform/browser' // Note I've changed Template to View here as I think it is a typo .... @View({ template: '<h1>Hello {{ name }}</h1>' }) .... 

The following Duncan Booth suggestion is here http://blog.ionic.io/angular-2-series-introduction/ I made the changes below and it works:

 import {Component, Template} from 'angular2/core'; import {bootstrap} from 'angular2/platform/browser'; .... @Template({ inline: "<h1>Hello {{ name }}</h1>" }) .... 

But a yesimahuman comment in the same link mentions that โ€œView is a new syntaxโ€, which seems to be the case, as shown in Plunker from Angular 2 walkthrough .

So why does the Quickstart code throw an error and the correct fix?

+6
source share
2 answers

I think you need to check this out.

 import {Component, Template, bootstrap} from 'angular2/angular2'; @Component({ selector: 'my-app' }) @Template({ inline: '<h1>Hello {{ name }}</h2>' }) class MyAppComponent { constructor() { this.name = 'Alice'; } } bootstrap(MyAppComponent); 
+1
source

And just to clarify: if you need to include some directives, you should do it like this:

 import {Component, Template, View, CSSClass, NgFor, bootstrap} from 'angular2/angular2'; // Component annotations .. @Component({ selector: 'my-app' }) @Template({ url: 'templates/layout.html' }) @View({ directives: [CSSClass, NgFor] }) 

and then in the code you can use [class], * ng-for, etc. :)

0
source

Source: https://habr.com/ru/post/986073/


All Articles