4

我正在使用 NativeScript Angular 构建一个应用程序。我需要在页面加载时获取或设置 UI 组件的属性。但是,ngOnInitngAfterViewInit(onloaded)似乎不起作用。

我创建了一个 NativeScript 程序来演示这个问题。这个程序有两个页面,每个页面有两个垂直堆叠的标签。当我按下每一页上的标签时,程序会加载另一页。在控制台中,它打印出标签 ( label.getActualSize().width) 的宽度。当我切换到其他页面时,我可以看到标签的宽度没有问题。我得到的值是270。但是,我想要的是在加载页面时在控制台中看到打印出来的宽度。

我从我的.html文件中尝试了 ngAfterViewInit()(loaded),但它们似乎不起作用。它给了我0。我的猜测是我得到了这个值,因为 NativeScript 在加载所有组件之前给了我宽度值。我需要解决这个问题。

这是我的page_2.component.ts代码,其中包含主要测试部分:

import {Component, ViewChild, ElementRef, OnInit} from "@angular/core";
import {Router} from "@angular/router-deprecated";

import {Page} from "ui/page";

import {Label} from "ui/label";

@Component({
    selector: "page2",
    templateUrl: "pages/page_2/page_2.html",
    styleUrls: ["pages/page_2/page_2-common.css", "pages/page_2/page_2.css"]
})

export class Page2 implements OnInit {

    @ViewChild("label_in_2") label_in_2: ElementRef;

    constructor(private _router: Router, private page: Page) {}

    ngOnInit() {

    }

    ngAfterViewInit() {
        let label = <Label>this.label_in_2.nativeElement;

        console.log("width of the label 2 in ngAfterViewInit: " + label.getActualSize().width); // this gives me 0.
    }

    whatisthewidth(testLabel) {
        // let test = testLabel.nativeElement;

        // testLabel.

        console.log("width of the label 2 in whatisthewidth: " + testLabel.getActualSize().width); // this gives me 0.
    }

    to_page1() {
        let label = <Label>this.label_in_2.nativeElement;

        console.log("width of the label 2: " + label.getActualSize().width); // this gives me 270.

        console.log("to page 1");
        this._router.navigate(["Page1"]);
    }

}

为了完整起见,让我附上程序的其他部分。

main.ts

import {nativeScriptBootstrap} from "nativescript-angular/application";
import {AppComponent} from "./app.component";

nativeScriptBootstrap(AppComponent);

app.component.ts

import {Component} from "@angular/core";

import {HTTP_PROVIDERS} from "@angular/http";
import {RouteConfig} from "@angular/router-deprecated";
import {NS_ROUTER_DIRECTIVES, NS_ROUTER_PROVIDERS} from "nativescript-angular/router";

import {Page1} from "./pages/page_1/page_1.component";
import {Page2} from "./pages/page_2/page_2.component";

@Component({
    selector: "main",
    directives: [NS_ROUTER_DIRECTIVES],
    providers: [NS_ROUTER_PROVIDERS, HTTP_PROVIDERS],
    template: "<page-router-outlet></page-router-outlet>"
})
@RouteConfig([
    { path: "/Page1", component: Page1, name: "Page1" },
    { path: "/Page2", component: Page2, name: "Page2", useAsDefault: true }
])

export class AppComponent { }

应用程序.css

button, label, stack-layout {
    horizontal-align: center;    
}

button {
    font-size: 36;
}

.title {
    font-size: 30;
    margin: 20;
}

.message {
    font-size: 20;
    color: #284848;
    text-align: center;
    margin: 0 20;
} 

.button_label {
    background-color: gray;

    width: 75%;
    height: 25%;
}

page_1.component.ts

import {Component, ViewChild, ElementRef, OnInit} from "@angular/core";
import {Router} from "@angular/router-deprecated";

import {Page} from "ui/page";

import {Label} from "ui/label";

@Component({
    selector: "page1",
    templateUrl: "pages/page_1/page_1.html",
    styleUrls: ["pages/page_1/page_1-common.css", "pages/page_1/page_1.css"]
})

export class Page1 {

    @ViewChild("label_in_1") label_in_1: ElementRef;

    constructor(private _router: Router, private page: Page) {}

    to_page2() {
        let label = <Label>this.label_in_1.nativeElement;

        console.log("width of the label 1: " + label.getActualSize().width);

        console.log("to page 2");
        this._router.navigate(["Page2"]);
    }

}

page_1.html

<StackLayout>
    <Label text="to page 2" class="button_label" (tap)="to_page2()"></Label>
    <Label #label_in_1 text="second label" class="test_label"></Label>
</StackLayout>

page_1-common.css

Label.test_label {
    background-color: blue;

    width: 75%;
    height: 20%;
}

page_2.html

<StackLayout>
    <Label text="to page 1" class="button_label" (tap)="to_page1()"></Label>
    <Label #label_in_2 text="second label" class="test_label" (loaded)="whatisthewidth(label_in_2)"></Label>
</StackLayout>

page_2.common.css

Label.test_label {
    background-color: yellow;

    width: 75%;
    height: 20%;
}

任何建议或见解将不胜感激。

4

2 回答 2

3

我不知道 Github 上的问题是如何在问题仍然存在的情况下被解决方法关闭的。超时的解决方案就是等到DOM加载完毕,如果从templateURL加载,时间可能会更长,所以没有时间添加。

于 2017-01-03T03:26:54.013 回答
0

我认为这个问题与这个Github 问题有关

于 2016-06-29T12:29:19.470 回答