Я пытаюсь запустить тестовое приложение, чтобы понять, как работает маршрутизация в AngularJS2. Я попытался следующий код:Angular2 + Typcript1.5 Маршрутизация
/// <reference path="reference.ts" />
import { Component, View, bootstrap } from 'angular2/angular2'
import { Location, RouteConfig, RouterLink, Router, RouterOutlet } from 'angular2/router';
import { routerInjectables, Pipeline } from 'angular2/router';
import { Directive, Attribute, ElementRef, DynamicComponentLoader} from 'angular2/angular2';
import { DrinksService} from 'services'
import { Drinkers } from 'components/drinkers'
import { Drinks } from 'components/drinks'
import { Contact } from 'components/contact'
@Component({
selector: 'my-app'
})
@View({
directives: [RouterOutlet, RouterLink],
template: `
<nav>
<ul>
<li><a router-link="drinks">Drinks</a></li>
<li><a router-link="drinkers">Drinkers</a></li>
<li><a router-link="contact">Contact</a></li>
</ul>
</nav>
<main>
<router-outlet></router-outlet>
</main>
`
})
@RouteConfig([
{ path: '/', component: Contact, as: 'contact'},
{ path: '/drinks', component: Drinks, as: 'drinks'},
{ path: '/drinkers', component: Drinkers, as: 'drinkers'}
])
class MyAppComponent {
name:string
buttonName:string
showup:boolean
constructor(public router: Router) {
this.name = 'Alice and Bob'
this.buttonName = 'are going to see rabbit whole'
this.showup = true
}
goToDrink =() => {
alert('Sicher?')
this.showup = false
}
isVisible =() => {
return !this.showup
}
goToDrinkReally =() => {
this.router.parent.navigate('/home')
}
}
bootstrap(MyAppComponent, [routerInjectables])
index.html:
<!-- index.html -->
<html>
<head>
<title>Angular 2 Quickstart</title>
<script src="https://github.jspm.io/jmcriffey/[email protected]/traceur-runtime.js"></script>
<script src="https://jspm.io/[email protected]"></script>
<script src="angular25minutes/angular25minutes.js"></script>
</head>
<body>
<!-- The app component created in app.ts -->
<my-app></my-app>
<script>System.import('app')</script>
</body>
</html>
Все компилируется, но в браузере я получаю пустой экран. Как html я могу видеть только
<html><head>
<title>Angular 2 Quickstart</title>
<script src="https://github.jspm.io/jmcriffey/[email protected]/traceur-runtime.js"></script><style type="text/css"></style>
<script src="https://jspm.io/[email protected]"></script><script type="text/javascript" src="https://jspm.io/[email protected]" data-init="upgradeSystemLoader"></script>
<script src="angular25minutes/angular25minutes.js"></script>
</head>
<body>
<!-- The app component created in app.ts -->
<my-app></my-app>
<script>System.import('app')</script>
</body></html>
Что я делаю неправильно?
А иногда после компиляции в консоли появляется следующая проблема:
ОРИГИНАЛ ИСКЛЮЧЕНИЕ: Нет базы HREF набор. Либо укажите привязку к «appBaseHrefToken», либо добавьте базовый элемент.
, но это довольно случайный случай.
Смотрите https://github.com/angular/angular/pull/3122 –
Спасибо это работает Теперь. Кстати, этот маршрутизатор можно использовать и с угловым1, не так ли? –
Честно говоря, я не знаю:/ –