文章内容

2021/6/12 10:13:55,作 者: 黄兵

Angular获取当前地址

在Javascript中获取当前Uri的方式如下:

console.log(window.location.href)

在Angular中如果使用Typescript获取当前Uri方法如下:

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
    template: 'The href is: {{href}}'
    /*
    Other component settings
    */
})
export class Component {
    public href: string = "";

    constructor(private router: Router) {}

    ngOnInit() {
        this.href = this.router.url;
        console.log(this.router.url);
    }
}


参考资料:

1、Get current url in Angular [duplicate]

分享到:

发表评论

评论列表