我怎么才能得到4号角的当前url呢?我在网上找了很久,但还是找不到解决方案.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Router } from '@angular/Router';

import { AppComponent } from './app.component';
import { TestComponent } from './test/test.component';
import { OtherComponent } from './other/other.component';
import { UnitComponent } from './unit/unit.component';

@NgModule ({
  declarations: [
    AppComponent,
    TestComponent,
    OtherComponent,
    UnitComponent
  ],

  imports: [
    BrowserModule,
    RouterModule.forRoot([
        {
            path: 'test',
            component: TestComponent
        },
        {
            path: 'unit',
            component: UnitComponent
        },
        {
            path: 'other',
            component: OtherComponent
        }
    ]),
  ],

  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }

app.component.html

<!-- The content below is only a placeholder and can be replaced -->
<div>
    <h1>Welcome to {{title}}!!</h1>

    <ul>
        <li>
            <a routerLink="/test">Test</a>
        </li>
        <li>
             <a routerLink="/unit">Unit</a>  
        </li>
        <li>
            <a routerLink="/other">Other</a>
        </li>
    </ul>
</div>
<br/>
<router-outlet></router-outlet>

app.component.ts

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

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

export class AppComponent{
    title = 'Angular JS 4';
    arr = ['abcd','xyz','pqrs'];
}

other.component.ts

import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';

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

export class OtherComponent implements OnInit {

    public href: string = "";
    url: string = "asdf";

    constructor(private router : Router) {}

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

test.component.ts

import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';

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

export class TestComponent implements OnInit {
    route: string;
    currentURL='';

    constructor() { 
        this.currentURL = window.location.href; 
    }

    ngOnInit() { }
}

现在我在点击other链接后发现了控制台问题

ERROR Error: Uncaught (in promise): Error: No provider for Router!

推荐答案

使用纯JavaScript:

console.log(window.location.href)

使用Angular :

this.router.url

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

普朗克在这里:https://plnkr.co/edit/0x3pCOKwFjAGRxC4hZMy?p=preview

Javascript相关问答推荐

如何使用合并排序方法编写数组排序Polyfill

在JavaScript中检索一些文本

我在这个黑暗模式按钮上做错了什么?

按钮未放置在html dis位置

react/redux中的formData在expressjs中返回未定义的req.params.id

如何修复(或忽略)HTML模板中的TypeScript错误?'

Angular 17—每当一个布尔变量变为真时触发循环轮询,只要它保持为真

为什么promise对js中的错误有一个奇怪的优先级?

在Three JS中看不到补间不透明度更改效果

无法读取未定义错误的属性路径名''

制作钢琴模拟器,并且在控制台中不会执行或显示该脚本

Chart.js-显示值应该在其中的引用区域

如何在ASP.NET JavaScript中使用Google Charts API仅对绘制为负方向的条形图移动堆叠条形图标签位置

从页面到应用程序(NextJS):REST.STATUS不是一个函数

JavaScript是否有多个`unfined`?

触发异步函数后不能显示数据

expo 联系人:如果联系人的状态被拒绝,则请求访问联系人的权限

AddEventListner,按键事件不工作

postman 预请求中的hmac/sha256内标识-从js示例转换

如何修复错误&语法错误:不能在纯react 项目中JEST引发的模块&之外使用导入语句?