Javascript 以角度获取当前url
如何获取Angular 4中的当前url?我在网上搜索了很多,但找不到解决办法 应用程序模块.tsJavascript 以角度获取当前url,javascript,angular,typescript,url-routing,Javascript,Angular,Typescript,Url Routing,如何获取Angular 4中的当前url?我在网上搜索了很多,但找不到解决办法 应用程序模块.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { RouterModule, Router } from '@angular/Router'; import { AppComponent } from './app.compon
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 { }
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'];
}
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);
}
}
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() { }
}
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>
其他.component.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 { }
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'];
}
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);
}
}
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() { }
}
测试组件.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 { }
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'];
}
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);
}
}
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() { }
}
现在我在点击其他链接后发现了控制台问题
ERROR Error: Uncaught (in promise): Error: No provider for Router!
您可以使用@angular/common中提供的位置服务,通过下面的代码,您可以获得位置或当前URL
import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';
@Component({
selector: 'app-top-nav',
templateUrl: './top-nav.component.html',
styleUrls: ['./top-nav.component.scss']
})
export class TopNavComponent implements OnInit {
route: string;
constructor(location: Location, router: Router) {
router.events.subscribe((val) => {
if(location.path() != ''){
this.route = location.path();
} else {
this.route = 'Home'
}
});
}
ngOnInit() {
}
}
这里是参考链接,我从这里复制了这个东西来获取我项目的位置。
使用纯JavaScript时:
console.log(window.location.href)
使用角度:
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);
}
}
plunkr在这里:其他.component.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 { }
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'];
}
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);
}
}
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() { }
}
因此,最终正确的解决方案是:
import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';
/* 'router' it must be in small case */
@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) {} // make variable private so that it would be accessible through out the component
ngOnInit() {
this.href = this.router.url;
console.log(this.router.url);
}
}
哪个网址???浏览器的url,并请添加一些代码的东西,你尝试了,以便了解你的问题,在更多details@MaciejTreder:已通过此链接,但它不起作用。您可以从“@angular/core”作为导入{Component}”@组件({selector:'app root',templateUrl:'./app.Component.html',styleUrls:['./app.Component.css']})导出类AppComponent{title='app';currentURL='';constructor(){this.currentURL=window.location.href;}}您从“@angular/Router”导入{RouterModule Router}时输入了一个打字错误代码>应该是
@angular/router
。我试图在plunkr中重现您的问题。一切正常:出现错误:类型“”上不存在属性“router”:(您是否向组件构造函数提供了路由器?是否导入了RouterModule?检查此plunker:您是否检查了plunker?它是否在您的端工作?window.location.href
正在返回页面的绝对url(包括协议和域)当this.router.url
返回应用程序内部的相对路径时…我真的不明白这是一个被接受的答案,因为这个解决方案返回了两个完全不同的结果!!!我也不明白链接的问题询问的结果是如何被标记为重复的路由url当这里的问题是询问绝对路径时,您能解释一下为什么这是正确的解决方案吗?我更希望得到一个类型为urlsecgment[]
的数组,而不是使用路由器
@ndgeek最好是使用public href:string=“”
替换为get
属性。这样就不需要在ngOnInit
中分配值,同时还具有始终最新的路由器URL。因此:get href();字符串{返回This.router.URL;}
。也没有必要将其设置为public
,因为这是默认设置。比所有这些使用ActivatedRoute
等的技巧简单得多。当url更改时,此答案会更新当前url,其他选项您可以使用first()运算符发出第一个事件:router.events.pipe(first()).subscribe((val)=>{…});