我正在为在浏览器中正常工作的项目使用angular 9。我正在尝试通过POSTMAN访问路由,但出现错误。如何通过CLI使路由可达?
这是我的路线
const routes: Routes = [
{path:'', component: JoinClassComponent},
{path:'home', component: HomeComponent},
{path:'login', component: LoginComponent},
{path:'class-ended', component: ClassEndedComponent},
// otherwise redirect to home
{path:'**', component: JoinClassComponent},
];
这里是组件
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-class-ended',
templateUrl: './class-ended.component.html',
styleUrls: ['./class-ended.component.css']
})
export class ClassEndedComponent implements OnInit {
constructor(private route: ActivatedRoute) { }
ngOnInit(): void {
const classID: string = this.route.snapshot.queryParamMap.get('meetingID');
//rest of the logic
}
}
URL在POSTMAN中不起作用
URL在浏览器中工作
/路由在POSTMAN中工作
Postman是一个API(应用程序编程接口)开发工具,可帮助构建,测试和修改API。它具有发出各种类型的HTTP请求(GET,POST,PUT,PATCH)的能力,可以保存环境供以后使用,将API转换为各种语言的代码。
因此,使用它来测试您的API,而不是调用HTML页面。
希望对您有帮助。 :)
这不起作用,因为Postman将URL localhost:4200/class-ended
请求发送到您的服务器-您的服务器对这个URL一无所知。相比之下:当您在浏览器中输入相同的URL时,Angular应用程序将处理该请求。
Angular build Single-Page applications,因此在您的服务器上,根目录中将只有一个index.html
文件:即,当您访问localhost:4200
时,您的网络服务器会将index.html
返回到浏览器(并且该html文件包含用于加载您的javascript源代码的脚本标签。
当您在浏览器中输入localhost:4200/class-ended
时,将通知angular URL发生了更改,并检查路由配置是否存在路由/class-ended
。如果是这样,angular-router将“导航”至该路由:即它将在路由器出口中显示该组件。但这不向服务器发送任何请求(并且如上所述,这将不起作用,因为服务器上不存在class-ended
。