我在Meteor和Angular2上遇到了一些问题。
我有一个名为Layout的父模块,其中我有一个带有用户名的标题栏,一个带有我的路由的左侧菜单和一个右侧为子项的空内容。
左侧菜单使用指令打开/关闭菜单(当用户点击时添加/删除类'打开')。 此外,在ngOnInit中,我订阅了通知。
他们将子HTML添加到父模板的方式是在布局HTML
中添加''这是路由
export const routes: Route[] = [
{path: '', component: LoginComponent, data: {title: 'Login'}, pathMatch: 'full'},
{
path: 'app', component: FullLayoutComponent, data: {title: 'Home'}, canActivate: [AuthGuard],
children: [
{path: '', redirectTo: 'dashboard', pathMatch: 'full'},
{path: 'dashboard', component: DashboardComponent, data: {title: 'Dashboard'}}
]
},
{
path: 'employees', component: EmployeesComponent, data: {title: 'Employees'},
children: [
{path: '', redirectTo: 'list', pathMatch: 'full'},
{path: 'list', component: EmployeesListComponent, data: {title: 'List'}},
{path: 'new', component: EmployeeNewComponent, data: {title: 'New'}},
{path: 'edit/:empId', component: EmployeeEditComponent, data: {title: 'Edit'}},
{path: 'view/:empId', component: EmployeeDetailsComponent, data: {title: 'details'}}
]
},
{path: '**', component: Error404Component, data: {title: 'Error'}}
];
想象一下,我的用户已经登录了。如果我使用此网址“http://localhost/#/”刷新页面,它会自动重定向到“http://localhost/#/app/dashboard”,并且指令和订阅会按预期工作。
FullLayout组件
@Component({
selector: 'app-dashboard',
template
})
@InjectUser("user")
export class FullLayoutComponent implements OnInit, OnDestroy {
user: Meteor.User;
private userNotification : Observable<Notification[]>;
private _userNotificationSub: Subscription;
public ready: boolean;
constructor(private router: Router) {}
ngOnInit()
{
this._userNotificationSub = MeteorObservable.subscribe('userNotifications', this.user.profile.empId ).subscribe(
() => {
this.userNotification = Notifications.find({}).zone();
console.log(this.userNotification);
}
);
}
ngOnDestroy() {
this._userNotificationSub.unsubscribe();
}
logout() {
var self = this;
Meteor.logout(function(){
self.router.navigate(['/']);
});
}
}
问题是,如果用户刷新或直接导航到“http://localhost/#/app/”,“http://localhost/#/app/dashboard”或“http://localhost/#/app/employees/list”,我不知道原因,但左侧菜单不是工作(点击事件正在运行,但它没有添加或删除该类),也没有订阅获取通知。
显然,我的所有孩子也都有订阅。
我该如何解决?
编辑:
我改变路由以使用它:
{path: 'login', component: LoginComponent, data: {title: 'Login'}, pathMatch: 'full'},
{
path: '', component: FullLayoutComponent, data: {title: 'Home'}, canActivate: [AuthGuard],
children: [
{path: '', redirectTo: 'dashboard', pathMatch: 'full'},
{path: 'dashboard', component: DashboardComponent, data: {title: 'Dashboard'}},]
},
{path: '**', component: Error404Component, data: {title: 'Error'}}
];
如果我去/ login然后插入我的用户名和密码,我仍然遇到同样的问题。它重定向到/仪表板,它不起作用。但如果我刷新/仪表板,一切都很完美。
Login.component.ts
@Component({
selector: 'app-login',
template
})
@InjectUser("user")
export class LoginComponent implements OnInit {
loginForm: FormGroup;
error: string;
user: Meteor.User;
public submitted: boolean;
constructor(private router: Router, private zone: NgZone, private formBuilder: FormBuilder) {
Meteor.logout();
}
ngOnInit() {
var self = this;
this.loginForm = this.formBuilder.group({
email: ['', Validators.required],
password: ['', Validators.required]
});
this.error = '';
}
login() {
var self = this;
this.submitted = true;
successLogin('test@test.com', '1234);
}
successLogin(email, password)
{
Meteor.loginWithPassword(email, password, (err) => {
if (err) {
this.zone.run(() => {
this.error = err;
console.log("error en login With password");
});
} else {
console.log('LoginWithPasword');
console.log(Meteor.user());
if(Meteor.user())
{
this.router.navigate(['/dashboard']);
}
else
{
alert("Error iniciando sesión");
}
}
});
}
}