Javascript 从中的url获取ID
我试图从图像中获取第一个id,但实际上我在分号后面接收id。(“id”=1)。根据我的代码,我不知道如何解决这个问题。任何建议。我真的不知道如何完成这项任务。我尝试过寻找不同的资源,但运气不佳 “”Javascript 从中的url获取ID,javascript,angular,Javascript,Angular,我试图从图像中获取第一个id,但实际上我在分号后面接收id。(“id”=1)。根据我的代码,我不知道如何解决这个问题。任何建议。我真的不知道如何完成这项任务。我尝试过寻找不同的资源,但运气不佳 “” ngOnInit(){ const id$=this.route.paramMap.pipe(map((params)=>params.get('id')| | 0),map(n=>Number(n)); id$.subscribe(id=>{ this.activeId=id; log(“Act
ngOnInit(){
const id$=this.route.paramMap.pipe(map((params)=>params.get('id')| | 0),map(n=>Number(n));
id$.subscribe(id=>{
this.activeId=id;
log(“ActiveId”,this.ActiveId);
this.testing=this.graphService.getTestingGraphs(id);
this.image=this.imageService.getImage(id);
});
这个.route.params
.订阅(参数=>{
常量id=+params['id'];
log(`Current param ID为:${ID}`);
});
}
订阅路由事件,以便具有相同url段的路由中的任何更改都将自动反映出来
使用[window.location.pathname],因为参数的返回值总是{id:'1',category:'testing'}
试试这个
this.route.params.subscribe(response => {
//Base in your url "http://localhost:4200/image/1;id=1;catergory=testing"
//when we access window.location.pathname we get '/image/1;id=1;catergory=testing'
let path = window.location.pathname.split('/'); // <- output would be ['', 'image', '1;id=1;catergory=testing']
let myParams = path[2].split(';'); // <- output would be ["1", "id=1", "catergory=testing"]
//So to get your id
let id = myParams[0];
});
this.route.params.subscribe(响应=>{
//“在您的url中创建基”http://localhost:4200/image/1;id=1;类别=测试“
//当我们访问window.location.pathname时,我们得到“/image/1;id=1;catergory=testing”
让path=window.location.pathname.split(“/”);//查询参数的结构不正确。这可能是导致您心痛的一个主要原因。比如,应该有什么不同?我已经用不同的方法对它们进行了结构设计。但是,我仍然不能正确地进行结构设计。但是,为了解决我之前的评论,OP需要实际构建一个查询字符串。我不确定我是否理解您的代码以及它的功能。谢谢你知道我怎么能在日志中看到activeId的输出吗?我们可以在私人聊天中聊天吗,因为我对这些东西有点陌生,不知道该怎么做。那么在拆分url后,我如何获得我想要的id呢?