来自服务动态数据的 nsRouterLink Nativescript

nsRouterLink Nativescript from dynamic data from service

我有来自服务的数组

private items = new Array<Item>(
        { id: 1, name: "Batman", link: "batmanscreen" },
        { id: 3, name: "doraemon", link: "dorascreen" },
        ...........

,

XML 组件如:

<ActionBar title="Details" class="action-bar"></ActionBar>
<FlexboxLayout flexDirection="column" class="page">
    <FlexboxLayout class="m-15">
        <Label class="h2" [text]="item.id + '. '"></Label>
        <Label class="h2" [text]="item.name"></Label>
    </FlexboxLayout>
    <Label class="h4" [text]="item.link"></Label>
    <Label [nsRouterLink]="['/item.link']" text="go TO Screen" ></Label>
</FlexboxLayout>

以及 android 模拟器上 link 获得点击时的结果:

EXCEPTION: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'item.link'

我希望成为nsRouterLink = "['/batmanscreen']",但在nativescript导航中找不到任何指南,请指导。 在 angular 2 web 中我可以处理它,但我不知道如何在 nativescript 中处理它..

谢谢。

所以您期望 ['/item.link'] 的计算结果为 ['/batmanscreen']'/item.link' 只是一个字符串文字,因此它不会像您希望的那样被 'batmanscreen' 替换。

改为这样尝试:['/' + item.link]

简单地做这样的路线:

private items = new Array<Item>(
        { id: 1, name: "Batman", link: "/batmanscreen" },
        { id: 3, name: "doraemon", link: "/dorascreen" },

并像往常一样在 Angular 的 HTML 中进行绑定(使用 {N} + Angular 时没有 XML):

<Label [nsRouterLink]="item.link" text="go TO Screen" ></Label>