angular.js - angular 2 怎么获取当前地址栏的URL
世界只因有你
世界只因有你 2017-05-15 17:09:22
[AngularJS讨论组]

后台自动跳转的URL,请问ng2怎么获取地址栏的URL,以及其中的参数?
网上搜的答案是ng1的,方法是let aid=$location.search().aid;这样的。但是在ng2里找不到$location这个。

世界只因有你
世界只因有你

全部回复(7)
我想大声告诉你

window.location.href
好像不需要ng2的方法吧

淡淡烟草味

angular 2 不可能没有$location吧

//1.获取当前完整的url路径  
var absurl = $location.absUrl();  
//http://172.16.0.88:8100/#/homePage?id=10&a=100  
  
//2. 获取当前url路径(当前url#后面的内容,包括参数和哈希值):  
 var url = $location.url();  
 // 结果:/homePage?id=10&a=100  

Angular 通过注入 $location 获取与修改当前页面URL

黄舟

window.location.href //当前地址栏的URL
this.sub = this.route.params.subscribe(params => {

        let 变量 = +params['参数名'];
        });
        //当前地址参数
PHP中文网

官方例子:Location,

    import {Component} from '@angular/core';
    import {Location} from '@angular/common';
    @Component({selector: 'app-component'})
    class AppCmp {
      constructor(location: Location) {
        location.go('/foo');
      }
    }
给我你的怀抱

我也遇到了这个问题,确实找不到,同求

某草草
import { Component, OnInit, Inject } from '@angular/core';
import {ActivatedRoute, Params} from '@angular/router';

@Component({
    selector: 'demo',
    template: `<p></p>`
})
export class DemoComponent implements OnInit {
    constructor(@Inject(ActivatedRoute) private router: ActivatedRoute) { }

    ngOnInit() {
        this.router.params.subscribe((params: Params) => {
            // params 
        });
    }
}

https://angular.io/docs/ts/la...

漂亮男人

说一下获取参数,
import {ActivatedRoute} from '@angular/router';
官方非要你用rxjs,但是这样强行用也是没问题的:
this._activatedRoute.params'value'

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号