Angular 条路线无法通过 POSTMAN 访问
Angular routes not accessible through POSTMAN
我正在使用 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(应用程序编程接口)开发工具,有助于构建、测试和修改 APIs.It 能够发出各种类型的 HTTP 请求(GET,POST、PUT、PATCH),保存环境供以后使用,将 API 转换为各种语言的代码。
所以用它来测试您的 API,而不是调用您的 HTML 页面。
希望对您有所帮助。 :)
这行不通,因为 Postman 将 URL localhost:4200/class-ended
请求发送到您的服务器 - 而您的服务器对此一无所知 URL.
相比之下:当您在浏览器中输入相同的 URL 时,您的 angular 应用程序将处理该请求。
Angular 构建 Single-Page applications,因此在您的服务器上,根目录中只有一个 index.html
文件:即当您访问 localhost:4200
时,您的网站-server returns index.html
到您的浏览器(这个 html 文件包含用于加载您的 javascript 源代码的脚本标签)。
当您在浏览器中输入 localhost:4200/class-ended
时,angular 将收到通知 URL 已更改并检查路由配置是否存在路由 /class-ended
。如果是这样,angular-router 将 "navigate" 到此路由:即它将在路由器出口中显示该组件。
但是这个 不会 向服务器发送任何请求(如上所述,这是行不通的,因为 class-ended
在您的服务器上不存在。
我正在使用 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(应用程序编程接口)开发工具,有助于构建、测试和修改 APIs.It 能够发出各种类型的 HTTP 请求(GET,POST、PUT、PATCH),保存环境供以后使用,将 API 转换为各种语言的代码。
所以用它来测试您的 API,而不是调用您的 HTML 页面。
希望对您有所帮助。 :)
这行不通,因为 Postman 将 URL localhost:4200/class-ended
请求发送到您的服务器 - 而您的服务器对此一无所知 URL.
相比之下:当您在浏览器中输入相同的 URL 时,您的 angular 应用程序将处理该请求。
Angular 构建 Single-Page applications,因此在您的服务器上,根目录中只有一个 index.html
文件:即当您访问 localhost:4200
时,您的网站-server returns index.html
到您的浏览器(这个 html 文件包含用于加载您的 javascript 源代码的脚本标签)。
当您在浏览器中输入 localhost:4200/class-ended
时,angular 将收到通知 URL 已更改并检查路由配置是否存在路由 /class-ended
。如果是这样,angular-router 将 "navigate" 到此路由:即它将在路由器出口中显示该组件。
但是这个 不会 向服务器发送任何请求(如上所述,这是行不通的,因为 class-ended
在您的服务器上不存在。