我使用angular2.0.0-beta.7.将组件加载到/path?query=value1
重定向到的路径上时/path
.为什么要删除GET参数?如何保存参数?
我在路由器中有错误.如果我有像这样的主要路线
@RouteConfig([
{
path: '/todos/...',
name: 'TodoMain',
component: TodoMainComponent
}
])
Run Code Online (Sandbox Code Playgroud)
和我的孩子一样的路线
@RouteConfig([
{ path: '/', component: TodoListComponent, name: 'TodoList', useAsDefault:true },
{ path: '/:id', component: TodoDetailComponent, name:'TodoDetail' }
])
Run Code Online (Sandbox Code Playgroud)
然后我无法在TodoListComponent中获取参数.我能得到
params("/my/path;param1=value1;param2=value2")
Run Code Online (Sandbox Code Playgroud)
但我想要经典
query params("/my/path?param1=value1¶m2=value2")
Run Code Online (Sandbox Code Playgroud) 假设我当前在具有URL的页面上/user/:id
.现在从这个页面我导航到下一页:id/posts
.
现在有办法,以便我可以检查以前的URL是什么,即/user/:id
.
以下是我的路线
export const routes: Routes = [
{
path: 'user/:id', component: UserProfileComponent
},
{
path: ':id/posts', component: UserPostsComponet
}
];
Run Code Online (Sandbox Code Playgroud) 我试图从url获取startdate.网址看起来像http:// sitename/booking?startdate = 28-08-2017 下面是我的代码
aap.module.ts
import {...};
@NgModule({
declarations: [
AppComponent, ModalComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
JsonpModule,
ReactiveFormsModule,
RouterModule.forRoot([{
path: '',
component: AppComponent
},
]),
],
providers: [ContactService, AddonService, MainService],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
aap.component.ts
import {...}
import {Router, ActivatedRoute, Params} from '@angular/router';
constructor(private activatedRoute: ActivatedRoute) {
// subscribe to router event
this.activatedRoute.queryParams.subscribe((params: Params) => {
console.log(params);
});
}
Run Code Online (Sandbox Code Playgroud)
它通过以下错误给出
未处理的承诺拒绝:没有基本的href设置.请提供APP_BASE_HREF标记的值或向文档添加基本元素.; 区域:; 任务:Promise.then; 值:错误:没有设置基本href.请提供APP_BASE_HREF标记的值或向文档添加基本元素.
角度应该如何知道基础href?
我有一个主要组件,其中有一个路由器插座.在路由器插座中加载的组件中,我抓住url参数,如下所示:
ngOnInit(): void {
// _route is injected ActivatedRoute
this._route.params.subscribe((params: Params) => {
if(params['url']){
this.testUrl = params['url'].replace(new RegExp('\%2[fF]', 'g'), '/');
}
});
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但是当我在我的顶级组件上尝试它时,params对象总是空的.我不明白为什么嵌套组件param对象中有数据,我试图以完全相同的方式访问它.没有错误继续,param对象只是空的.
为什么我的父组件不能从ActivatedRoute获取正确的Params对象?
编辑:
按要求的完整父组件
import { OnInit, Component, Directive } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
})
export class AppComponent {
public testUrl: string;
constructor(private router: Router, private _route: ActivatedRoute) {
}
ngOnInit(): void{
this._route.queryParams.subscribe((params: Params) => {
console.log(params);
if (params['url']) { …
Run Code Online (Sandbox Code Playgroud) 例子
http://localhost:4200/login?aUasas129198
Run Code Online (Sandbox Code Playgroud)
决心
http://localhost:4200/login
Run Code Online (Sandbox Code Playgroud)
如果我想要'?'后面的值该怎么办?
我尝试做
{ path: 'login/:id', component: LoginComponent },
Run Code Online (Sandbox Code Playgroud)
但没有成功
我也尝试过
console.log(this.route.snapshot.queryParams);
console.log(this.route.snapshot.params);
Run Code Online (Sandbox Code Playgroud)
但它们都返回空对象。我现在该怎么办请帮忙
我有一个表单,它采用一个参数来构建并将客户链接到产品。现在我们添加了另一个参数以包含在 URL 中,但是我似乎无法从 URL 中获取它。
我究竟做错了什么?感谢帮助!
网址:http://localhost:4200/check/98712?Id=803
我可以设法从“98712”中获取第一个参数,但不能获取 Id。
在我的 app.module 下
RouterModule.forRoot([
{ path: 'check/:cust', component: CheckComponent},
Run Code Online (Sandbox Code Playgroud)
我试图在那里添加 ID,但它不起作用。
在我的 Check 组件中,我取出了客户和 Id,但 Id 仍然是空的。
this.customer = this.route.snapshot.paramMap.get('cust');
this.Ids = this.route.snapshot.paramMap.get('Id'); //Ids since it could be several, hence the add of Id in the URL
Run Code Online (Sandbox Code Playgroud)
我在 app-routing 中的路线
const routes: Routes = [
{ path: '', redirectTo: 'ms', pathMatch: 'full' },
{ path: 'ms', component: CheckComponent },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { …
Run Code Online (Sandbox Code Playgroud)