Angular 2,注入RouteData抛出异常'无法解析RouteData的所有参数'

时间:2016-01-08 16:06:00

标签: angular angular2-routing

我想使用Angular2根据当前路线显示标题。

我写了以下组件:

public static void main(String[] args) {
    String connectionURL = "jdbc:sqlite:C:/__tmp/cities.db";
    try (Connection conn = DriverManager.getConnection(connectionURL)) {
        String sql = "SELECT Country FROM CITIES";
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    String s = new String(rs.getBytes(1), "cp437");
                    System.out.println(s);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }       
}

我的路线配置方式如下:

import {Component} from 'angular2/core';
import {RouteData} from 'angular2/router';

@Component({
    selector: 'header-title',
    template: 'title: {{title}}',
    providers: [RouteData]
})
export class HeaderTitle {
    title: string;

    constructor(data: RouteData) {
        this.title = data.get('data');
    }
}

我的应用程序引导程序是:

@RouteConfig([
    {path: '/', component: Home, name: 'Index', data: {title: 'Index page'}},
    {path: '/home', component: Home, name: 'Home', data: {title: 'Welcome Home'}},
    {path: '/test', component: Home, name: 'Test', data: {title: 'Test page'}},
    {path: '/**', redirectTo: ['Index']}
])

我在HTML模板中添加document.addEventListener('DOMContentLoaded', function main() { bootstrap(App, [ ...('production' === process.env.ENV ? [] : ELEMENT_PROBE_PROVIDERS), ...HTTP_PROVIDERS, ...ROUTER_PROVIDERS, provide(LocationStrategy, { useClass: HashLocationStrategy }) ]) .catch(err => console.error(err)); }); 时收到错误Cannot resolve all parameters for RouteData

<header-title>

2 个答案:

答案 0 :(得分:2)

这与你在这里遇到的问题相同:

Angular 2, routerOnActivate method is never called for a component that implements OnActivate

您正在尝试接收HeaderTitle组件中的数据,但是您要将其发送到Home组件。

如果有帮助,我通过使用一个中间路由对象来完成同样的事情,该对象呈现收到的标题并在其中包含另一个router-outlet以进一步呈现视图的其余部分。

答案 1 :(得分:1)

我认为您必须从providers: [RouteData]

中删除@Component
@Component({
    selector: 'header-title',
    template: 'title: {{title}}'
})