gpt4 book ai didi

navigation - Angular 2 Router 从 alpha 34 版本开始就坏了

转载 作者:太空狗 更新时间:2023-10-29 18:27:20 26 4
gpt4 key购买 nike

我使用 Angular2 已经有一段时间了。所有代码都是最前沿的,并使用最新的 TypeScript Nightly 1.6 和 Angular alpha 34。我无法从其中一个组件调用父路由器。如果可能,请帮助我。在此附上代码和导航尝试错误。

应用.ts

/// <reference path="../typings/angular2/angular2.d.ts" />
'use strict';
import {Component, View, bootstrap} from 'angular2/angular2';
import {routerInjectables, RouterOutlet, RouteConfig} from 'angular2/router';
import {LoginApp} from './login/login';
import {DashboardApp} from './dashboard/dashboard';
// Annotation section
@Component({
selector: 'inventman-app'
})
@View({
template: `<!-- The router-outlet displays the template for the current component based on the URL -->
<router-outlet></router-outlet>`,
directives: [RouterOutlet]
})
@RouteConfig([
{ path: '/', redirectTo: '/login' },
{ path: '/dashboard', as: 'dashboard', component: DashboardApp },
{ path: '/login', as: 'login', component: LoginApp }
])
// Component controller
export class InventmanApp {
constructor() {
}
}

// bootstrap the Main App
bootstrap(InventmanApp,
[
routerInjectables
]);

登录.ts

/// <reference path="../../typings/angular2/angular2.d.ts" />
'use strict';
import {Component, View, formDirectives} from 'angular2/angular2';
import {Router} from 'angular2/router';
import {HttpService} from '../../services/httpservice/httpservice';
@Component({
selector: 'login-app'
})
@View({
templateUrl: './jsts/components/login/login.html',
directives: [formDirectives],
styleUrls: ['./jsts/components/login/login.css']
})
export class LoginApp {
username: String;
password: String;
constructor(public router: Router) {
}
onSubmit() {
const username = this.username, password = this.password;
HttpService.serve('https://' + location.host + '/userapi/sessions/create', 'POST', {
'Accept': 'application/json',
'Content-Type': 'application/json'
}, JSON.stringify({ username, password }))
.then(response=> {
if (!response.id_token) {
// Alerts the actual message received from the server
alert(response.message);
// Removes any previous assigned JWT to ensure tighter security
localStorage.removeItem('jwt');
}
else {
// Valid Login attempt -> Assign a jwt to the localStorage
localStorage.setItem('jwt', response.id_token);
// route to the dashboard
this.router.parent.navigate('/dashboard');
}
});
}
}

错误

EXCEPTION: TypeError: Cannot read property 'parent' of undefined angular2.dev.js:22448 STACKTRACE: angular2.dev.js:22448 TypeError: Cannot read property 'parent' of undefined at eval (login.js:34) at Zone.run (angular2.dev.js:136) at Zone.execute.$__3._createInnerZone.zone.fork.fork.$run [as run] (angular2.dev.js:16437) at zoneBoundFn (angular2.dev.js:109) at lib$es6$promise$$internal$$tryCatch (angular2.dev.js:1359) at lib$es6$promise$$internal$$invokeCallback (angular2.dev.js:1371) at lib$es6$promise$$internal$$publish (angular2.dev.js:1342) at angular2.dev.js:187 at Zone.run (angular2.dev.js:136) at zoneBoundFn (angular2.dev.js:109)

最佳答案

这个问题的答案很简单。从 cli 运行时,新的 beta typescript 编译器似乎完全避免了 angular 的 DI。从 IDE (Sublime 3) 插件编译似乎已经解决了这个问题。

我进一步改进了代码,应该会尽快将其与屏幕截图一起推送到 GIT,以供其他人引用和使用。

关于navigation - Angular 2 Router 从 alpha 34 版本开始就坏了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31906566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com