【问题标题】:Nativescript-fresco with ng2+NatiivescriptNativescript-fresco 与 ng2+Nativescript
【发布时间】:2017-02-07 11:55:56
【问题描述】:

我遵循指南将 nativescript-fresco 与 ng2+nativescript 一起用于我的 android 应用程序,因为每次我向下滚动一次以上然后向上滚动时它都会崩溃。我使用它的组件是一个列表视图,它显示 20 个在线图像。 url 通过有效的 @Input 指令从父组件传递。但是,当我切换到 FrescoDrawee 时,会渲染列表视图,但不会渲染图像。

这是组件的html

<GridLayout columns="*,*,*,*,*,*,*,*,*" height="100" class="item"
            xmlns:nativescript-fresco="nativescript-fresco">
    <ios>
        <Image [src]="data.imageUrl" row="0" col="0" colspan="3" class="ios-product-image" (tap)="details(data)"></Image>
    </ios>
    <android>
        <!--<Image [src]="data.imageUrl" row="0" col="0" colspan="3" class="android-product-image" (tap)="details(data)"></Image>-->
        <nativescript-fresco:FrescoDrawee width="100" height="100"
                                          [imageUri]="data.imageUrl"
        ></nativescript-fresco:FrescoDrawee>
    </android>

    <StackLayout row="0" col="3" colSpan="5" (tap)="details(data)">
        <Label [text]="data.name" class="product-name"></Label>
        <Label [text]="data.description" textWrap="true" class="product-description"></Label>
        <GridLayout columns="*,*,*,*,*,*,*,*,*,*" class="increment-decrement" style="width: 100%; height: 20%; vertical-align: bottom">
            <Label [text]="data.price" class="product-price" col="0" colSpan="5"></Label>
            <ios>
                <Button text="-" col="5" colSpan="1" (tap)="dec()"></Button>
                <Label [text]="count" col="6" colSpan="3" class="product-amount" ></Label>
                <Button text="+" col="9" colSpan="1" (tap)="inc()"></Button>
            </ios>
            <android>
                <Label text="-" col="5" colSpan="1" class="inc-dec" (tap)="dec()"></Label>
                <Label [text]="count" col="6" colSpan="3" class="product-amount"></Label>
                <Label text="+" col="9" colSpan="1" class="inc-dec" (tap)="inc()"></Label>
            </android>

        </GridLayout>
    </StackLayout>


    <ios>
        <Button col="11" class="cart" colSpan="1"></Button>
    </ios>


    <android>
        <Label col="11" class="cart" colSpan="1"></Label>
    </android>

    <Image src="~/media/ic_add_shopping_cart_white_24dp.png" col="11"
           style="height: 20%; width: 20%;" (tap)="addToCart()"></Image>

</GridLayout>

这是打字稿

import { Component, OnInit, Input} from "@angular/core";
import LabelModule = require("ui/label");
import application = require("application");
import { RouterExtensions } from "nativescript-angular/router";
import { PublicVariables } from "../../shared/publicVariables";


@Component({
  selector:'product-list',
  templateUrl: 'pages/products/product_list.html',
  styleUrls: ['pages/products/product_list-common.css',      'pages/products/product_list.css']
})

export class ProductListComponent implements OnInit {
   @Input() data: any;
   private count=0;

constructor(private routerExtensions: RouterExtensions) {}

ngOnInit() {

}
details() {
    this.routerExtensions.navigate(["/product/:id"]);
    PublicVariables.currentProduct = this.data;
}
inc() {
    ++this.count;
}
dec() {
    if(this.count>0) {
        --this.count;
    }
}
}

我对原生脚本比较陌生,所以我的代码可能不是最干净的。 我已将 nativescript-fresco 插件添加到我的项目中,并在 AppModule 中导入并初始化它。我不知道是否需要向组件本身添加除 FrescoDrawee 标记之外的任何内容,因为我在文档中没有看到任何表明这一点的内容。

请帮我弄清楚我的代码有什么问题?

【问题讨论】:

    标签: android cross-platform nativescript angular2-nativescript fresco


    【解决方案1】:

    我认为问题出在前缀上,您可以将其用作:

    <FrescoDrawee width="100" height="100" [imageUri]="data.imageUrl"></FrescoDrawee>
    

    为了完整起见,这是在与 Angular 一起使用时需要添加到 app.module.ts 的内容:

    import { TNSFrescoModule } from "nativescript-fresco/angular";
    import fresco = require("nativescript-fresco");
    import application = require("application");
    
    if (application.android) {
      application.onLaunch = function (intent) {
        fresco.initialize();
      };
    }
    
    @NgModule({
      imports: [
        TNSFrescoModule
    

    【讨论】:

    • 谢谢艾迪。你的建议正是我需要做的。我完全按照您的建议进行了更改,现在我的应用程序可以工作了。你为我节省了数小时试图解决问题的时间。再次感谢
    • 我注意到此解决方案导致 iOS 应用出现问题,因为 nativescript-fresco 仅针对 android 进行了初始化。即使 FrescoDrawee 标签包含在 标签中,它也会破坏 iOS 应用程序。我必须添加 *ngIf=isAndroid 和相应的函数来告诉 iOS 忽略该元素。只是想我应该为使用此解决方案的任何人添加它
    • 是的,必须做类似的事情。我发现使用 2 个不同的模板(每个平台)是我的选择。
    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 2017-12-05
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2017-05-15
    相关资源
    最近更新 更多