我该怎么获取当前地址栏的URL,使用angular 2?

angular 2 怎么获取当前地址栏的URL


富国沪深
浏览 1071回答 4
4回答

阿波罗的战车

在Angular8中使用PlatformLocation类,此类保存url链接信息,如基地址,端口号等。使用如下:constructor(private platformLocation: PlatformLocation) {}ngOnInit() {console.log(this.platformLocation.href);//href属性即是当前url}

慕桂英3389331

import { Component, OnInit, Inject } from '@angular/core';import {ActivatedRoute, Params} from '@angular/router';@Component({selector: 'demo',template: `<div></div>`})export class DemoComponent implements OnInit {constructor(@Inject(ActivatedRoute) private router: ActivatedRoute) { }ngOnInit() {this.router.params.subscribe((params: Params) => {// params});}}

慕码人8056858

rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {$rootScope.previousState = from;$rootScope.previousParams = fromParams;});

慕桂英546537

import { Component, OnInit, Inject } from '@angular/core';import {ActivatedRoute, Params} from '@angular/router';@Component({selector: 'demo',template: `<div></div>`})export class DemoComponent implements OnInit {constructor(@Inject(ActivatedRoute) private router: ActivatedRoute) { }ngOnInit() {this.router.params.subscribe((params: Params) => {// params});}
打开App,查看更多内容
随时随地看视频慕课网APP