Angular2-位置没有路由? [英] Angular2- Location without routing?

查看:56
本文介绍了Angular2-位置没有路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不进行路由的情况下监视和设置URL搜索参数?

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!

具有以下内容:

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?

推荐答案

我做了一次.要实现 HashLocationStrategy ,您必须实现它类似于您的 MainComponent:

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 })]
);

这篇关于Angular2-位置没有路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆