Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2- Location without routing?

Tags:

angular

How do I monitor and set URL search params without routing?

'Location' and seems to have what I want. However I get this error:

Error: No provider for Location!

With the following:

import { Component } from '@angular/core';
import { Location, HashLocationStrategy } from "@angular/common";

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  providers: [HashLocationStrategy]
})

export class AppComponent {
  constructor(private location: Location){}
}

How do I know which provider it wants? And is there a better way to accomplish this?

like image 935
x0a Avatar asked Mar 11 '23 03:03

x0a


1 Answers

I did it once. To implement HashLocationStrategy you will have to implements it like that in your MainComponent:

import {bootstrap}    from '@angular/platform-browser-dynamic';
import {
    Location,
    LocationStrategy,
    HashLocationStrategy,
    APP_BASE_HREF
} from '@angular/common';
import {provide} from '@angular/core';
import {ROUTER_PROVIDERS} from '@angular/router-deprecated'

import { HTTP_PROVIDERS } from '@angular/http';
import {AppComponent} from './app.component';

bootstrap(
    AppComponent
    , [
        HTTP_PROVIDERS
        ,ROUTER_PROVIDERS
        , provide(APP_BASE_HREF, { useValue: '/' })     
        , provide(LocationStrategy, { useClass: HashLocationStrategy })]
);
like image 169
Nathaniel Avatar answered Apr 01 '23 06:04

Nathaniel