我在Angular 2中遇到router.parent.navigate
问题。
而且我不知道这是Angular的问题还是我的代码有问题。
当我有父组件App.ts
时,其内容配置为路由并托管所有视图。
///<reference path="../../node_modules/angular2/angular2.d.ts"/>
///<reference path="../../node_modules/angular2/router.d.ts"/>
///<reference path="./BusinessNameLookup/BusinessNameLookup.ts"/>
///<reference path="./LookupDetails/LookupDetails.ts"/>
///<reference path="./BusinessNames/BusinessNames.ts"/>
///<reference path="./BusinessWebAddress/BusinessWebAddress.ts"/>
///<reference path="./BusinessCategory/BusinessCategory.ts"/>
///<reference path="./ContactInformations/ContactInformations.ts"/>
///<reference path="./BusinessPurchaseAddress/BusinessPurchaseAddress.ts"/>
///<reference path="./BusinessImages/BusinessImages.ts"/>
///<reference path="./SurveyQuery/SurveyQuery.ts"/>
///<reference path="./ABN/ABN.ts"/>
import {bootstrap, Component, View, bind, Inject} from 'angular2/angular2';
import {
ROUTER_DIRECTIVES,
ROUTER_PROVIDERS,
RouteConfig
, ROUTER_BINDINGS, RouterLink, RouterOutlet, Route,
LocationStrategy, HashLocationStrategy} from 'angular2/router';
/* Importing Application Components */
import { BusinessNameLookup } from './BusinessNameLookup/BusinessNameLookup.ts';
import { ABN } from './ABN/ABN.ts';
import { LookupDetails } from './LookupDetails/LookupDetails.ts';
import { Businessnames } from './BusinessNames/BusinessNames.ts';
import { BusinessWebAddress } from './BusinessWebAddress/BusinessWebAddress.ts';
import { BusinessCategory } from './BusinessCategory/BusinessCategory.ts';
import { ContactInformations } from './ContactInformations/ContactInformations.ts';
import { BusinessPurchaseAddress } from './BusinessPurchaseAddress/BusinessPurchaseAddress.ts';
import { BusinessImages } from './BusinessImages/BusinessImages.ts';
import { SurveyQuery } from './SurveyQuery/SurveyQuery.ts';
/* Importing Application Components */
import { BusinessModel } from './DataModels/BusinessModel.ts';
@Component({
selector: 'app'
})
@View({
template: `<router-outlet></router-outlet>`,
directives: [RouterOutlet, RouterLink, ROUTER_DIRECTIVES]
})
@RouteConfig([
new Route({ path: '/', component: BusinessNameLookup }),
new Route({ path: '/ABN', component: ABN, name: 'ABN' }),
new Route({ path: '/LookupDetails', component: LookupDetails, name: 'LookupDetails' }),
new Route({ path: '/BusinessNames', component: Businessnames, name: 'BusinessNames' }),
new Route({ path: '/BusinessWebAddress', component: BusinessWebAddress, name: 'BusinessWebAddress' }),
new Route({ path: '/BusinessCategory', component: BusinessCategory, name: 'BusinessCategory' }),
new Route({ path: '/ContactInformations', component: ContactInformations, name: 'ContactInformations' }),
new Route({ path: '/BusinessPurchaseAddress', component: BusinessPurchaseAddress, name: 'BusinessPurchaseAddress' }),
new Route({ path: '/BusinessImages', component: BusinessImages, name: 'BusinessImages' }),
new Route({ path: '/SurveyQuery', component: SurveyQuery, name: 'SurveyQuery' })
])
class AppComponent {
}
bootstrap(AppComponent, [ROUTER_BINDINGS, bind(LocationStrategy).toClass(HashLocationStrategy)]);
在我写这篇文章时,我将导航到主页并在<router-outlet>
属性中查看它。
不,我一直在放this.router.parent.navigate(['/ABN']);
这行我把它放入按钮调用的功能中。
问题开始我按下这个按钮我已导航到另一页面,然后再返回到主页并在URL中显示问号符号
http://localhost:5000/?
它也会消失。
答案 0 :(得分:2)
当处理导航事件的<button>
位于<form>
元素
例如:
//Component function
gotoAnotherRoute() {
this._router.navigate(['/another-route']);
}
此模板将刷新页面并在URL中添加问号符号
<form>
<input [(ngModel)]="model.name"/>
<button (click)="gotoAnotherRoute()">Validate</button>
</form>
此模板将正常工作并按预期路由。
<form>
<input [(ngModel)]="model.name"/>
</form>
<button (click)="gotoAnotherRoute()">Validate</button>
答案 1 :(得分:1)
导航后只返回false。
//Component function
gotoAnotherRoute() {
this._router.navigate(['/another-route']);
return false;
}