刷新 http://localhost:4200/user/products/ $%7Buserid%7D 后,页面显示:
{ “成功”:真, “数据”:[]}
代码 src / app / product / product.module.ts
const routes = [
{
path: `user/products`, canActivate: [AuthGuard], component: UserProductsComponent
}, {
path: `user/products/:id`, canActivate: [AuthGuard], component: UserProductsComponent
}
];
@NgModule({
imports: [
CommonModule,
FormsModule,
HttpModule,
ReactiveFormsModule,
RouterModule.forChild(routes)
],
declarations: [
UserProductsComponent,
],
exports: [
UserProductsComponent,
FormsModule]
})
export class ProductModule { }
代码 app.module.ts
const routes: Routes = [
{ path: '', component: NavbarComponent }
];
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
CoreModule,
UserModule,
ProductModule,
RouterModule.forRoot(routes)
],
providers: [AuthService, AuthGuard, UserService, ProductsService],
bootstrap: [AppComponent]
})
export class AppModule { }
代码 auth.guard.ts
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.checkLoggedIn(state.url);
}
checkLoggedIn(url: string): boolean {
if (this.authService.isLoggedIn_()) {
return true;
}
this.router.navigate(['/login']);
return false;
}
}
代码 src / app / product / user-products / user-products.ts:
ngOnInit() {
this.userObj = this.authService.currentUser;
this.route.params.subscribe((params: any) => {
if (params.id) {
this.productid = params['_id'];
this.getProducts(this.userObj.userid);
}
});
}
如何更正user/products/:id
页面的工作原理?这个页面的授权很糟糕吗?
答案 0 :(得分:0)
如果您的路径确实是
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python\Python36-32\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Python\Python36-32\lib\urllib\request.py", line 526, in open
response = self._open(req, data)
File "C:\Python\Python36-32\lib\urllib\request.py", line 544, in _open
'_open', req)
File "C:\Python\Python36-32\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Python\Python36-32\lib\urllib\request.py", line 1361, in https_open
context=self._context, check_hostname=self._check_hostname)
File "C:\Python\Python36-32\lib\urllib\request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "C:\Users\Python\Python36-32\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Python\Python36-32\lib\http\client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Python\Python36-32\lib\http\client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Python\Python36-32\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "C:\Python\Python36-32\lib\http\client.py", line 964, in send
self.connect()
File "C:\Python\Python36-32\lib\http\client.py", line 1392, in connect
super().connect()
File "C:\Python\Python36-32\lib\http\client.py", line 936, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Python\Python36-32\lib\socket.py", line 710, in create_connection
sock.settimeout(timeout)
TypeError: an integer is required (got type dict)
然后你必须在user-products.ts中获得没有下划线的'id'。你当然不会使用userId获取产品,对吗? ;)
user/products/:id
应该这样做。