【问题标题】:nsRouterLink Nativescript from dynamic data from servicensRouterLink Nativescript 来自服务的动态数据
【发布时间】:2017-03-04 11:51:03
【问题描述】:

我有来自服务的数组

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>

以及点击链接时安卓模拟器上的结果:

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

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

谢谢。

【问题讨论】:

    标签: nativescript


    【解决方案1】:

    简单地做这样的路线:

    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>
    

    【讨论】:

    • 谢谢你的帮助,its answer my question. but before i read your answer, i decided to use code i found in documentation [here](http://docs.nativescript.org/angular/core-concepts/angular-navigation) and use this.routerExtensions.navigate()`
    【解决方案2】:

    所以您期望['/item.link'] 评估为['/batmanscreen']'/item.link' 只是一个字符串文字,所以它不会像你希望的那样被 'batmanscreen' 替换。

    试试这样吧:['/' + item.link]

    【讨论】:

    • 它是 Nativescript,而不是 javascript,并且会转换为 xml,因此 ['/'+ item.link] 将导致解析错误。我之前试过
    • 相信我,我知道 NativeScript 是什么。在发布答案之前,我实际上验证了我的建议。只是想帮忙。
    • 我认为它应该 [attributeName ="propertyName".. 如果我能捕捉到错误,我会告诉你结果。我只是再次尝试确保我之前已经尝试过
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2010-11-15
    • 2012-12-18
    • 2015-07-10
    • 2017-04-16
    相关资源
    最近更新 更多