Ich versuche, eine Anwendung mit Hash-Standort Strategie zu erstellen, aber es fügt den Hash nicht zur URL hinzu. Wenn ich zum Beispiel auf eine Schaltfläche klicke, die mit {Pfad: '/ Umfragen', Name: 'Umfragen', Komponente: Umfrageinfo} verknüpft ist, lädt sie die Seite mit dieser URL: localhost: 3000/Umfragen.Hash Location Strategie in Angular 2
Was muss ich ändern, um die Hash-Standort-Strategie zu erhalten? Warum muss ich die Standard-Basis-URL festlegen, wenn ich die Hash-Standortstrategie verwenden möchte?
Dies ist das Routing in dem app.component.ts wo alle Routing definiert:
import {Component} from 'angular2/core'
import {HTTP_PROVIDERS, Http} from 'angular2/http';
import 'rxjs/Rx'; // load the full rxjs
import {ROUTER_PROVIDERS, RouteConfig , ROUTER_DIRECTIVES} from 'angular2/router';
import { ResultsComponent } from './results/results.component'
import { VotingCardsComponent } from './votingcards/votingcards.component'
import { DashBoardComponent } from './dash/dash.component'
import { PollsComponent } from './pollslist/pollslist.component'
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html',
directives: [ROUTER_DIRECTIVES, ResultsComponent, VotingCardsComponent, DashBoardComponent],
providers: [HTTP_PROVIDERS,
ROUTER_PROVIDERS]
})
@RouteConfig([
{ path: '/vote', name: 'VotePage', component: VotingCardsComponent },
{ path: '/votepoll/:id', name: 'VotePoll', component: VotingCardsComponent },
{ path: '/results', name: 'Results', component: ResultsComponent },
{ path: '/polls', name: 'Polls', component: PollsComponent },
{ path: '/', name: 'DashBoard', component: DashBoardComponent, useAsDefault: true }
])
export class AppComponent { }
Und das ist mein main.ts, wo ich die Basis-URL konfigurieren:
import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';
//this is to avoid the href empty issue
import {provide} from 'angular2/core';
import {APP_BASE_HREF, ROUTER_PROVIDERS} from 'angular2/router';
bootstrap(AppComponent, [
//this is to avoid the href empty issue
ROUTER_PROVIDERS,
provide(LocationStrategy, { useClass: HashLocationStrategy }),
provide(APP_BASE_HREF, { useValue: '/' })
]);
Das half mir, da dies besser geeignet für Angular 2 endgültig ist (ich bin auf 2.2.3). Alle anderen Antworten beziehen sich auf ältere Versionen von Angular 2. – Pradeep