我这里有这个小代码:
if (this._error.code == 0) {
localStorage.setItem('pinVouchers', JSON.stringify(this._filters.pin));
$('.loading-wrapper').addClass('hidden');
this._router.navigate(['/dashboard/voucher/list']);
}
else {
this._error.getErrorMessage(this._error.code);
$('.loading-wrapper').addClass('hidden');
alert(this._error.message);
}
在浏览器上调试时,如果错误!= 0,它显然会执行else语句中的部分代码。但是,在触发警报后,它会导航到if语句中列出的页面&#34; this._router.navigate([&#39; / dashboard / voucher / list&#39;]);&#34;。< / p>
我已经调试了整个ts文件,但我无法找到为什么在if语句不是真的情况下触发该行代码。
请帮助,谢谢!
修改
以下是整个代码:
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES, Router } from '@angular/router';
import { CORE_DIRECTIVES, ControlGroup, FORM_DIRECTIVES, Control, FormBuilder, Validators } from '@angular/common';
import { Services } from '../../../services/services';
import { Filters } from '../../../model/Filters';
import { Error } from '../../../model/Error';
@Component({
selector: 'voucher',
templateUrl: 'www/templates/voucher-template.html',
directives: [ CORE_DIRECTIVES, FORM_DIRECTIVES, ROUTER_DIRECTIVES ],
styleUrls: ['www/css/voucher.css'],
providers: [Services]
})
export class voucher {
_PinVouchers: ControlGroup;
_filters: Filters;
_error: Error;
constructor(private services: Services, fb: FormBuilder, public _router: Router) {
this._error = new Error();
this._filters = new Filters();
fb = new FormBuilder();
this._PinVouchers = fb.group({
'pin': new Control(this._filters.pin)
});
}
ngOnInit() {
$('.menu.active').removeClass("active");
$('#vouchersMenu').addClass("active");
$('.submenu.active').removeClass("active");
localStorage.removeItem("pinVouchers");
$(document).ready(() => {
$('#inputPin').keypress(function(key) {
if((key.charCode < 48 || key.charCode > 57) && (key.charCode < 97 || key.charCode > 122) && (key.charCode < 65 || key.charCode > 90) && (key.charCode != 45)) return false;
});
});
}
savePin(filters: Filters){
$('.loading-wrapper').removeClass('hidden');
this.services.getVisitsByPin(this._filters);
this._error.code = JSON.parse(sessionStorage.getItem('error'));
if (this._error.code == 0) {
localStorage.setItem('pinVouchers', JSON.stringify(this._filters.pin));
$('.loading-wrapper').addClass('hidden');
this._router.navigate(['/dashboard/voucher/list']);
}
else {
this._error.getErrorMessage(this._error.code);
$('.loading-wrapper').addClass('hidden');
alert(this._error.message);
}
}
}
答案 0 :(得分:0)
我刚看到错误,路由器也在html模板中的锚标签内被调用。