【问题标题】:How to use nativescript-orientation plugin in typescript & Angular 2 in nativescript?如何在nativescript中使用nativescript-orientation插件和Angular 2?
【发布时间】:2017-01-25 07:26:09
【问题描述】:

我将在我的项目中使用 nativescript-orientation 插件。 https://github.com/NathanaelA/nativescript-orientation#argslandscape--true--false

我想在方向自动更改时更改所有布局。 所以我要使用这个功能。

exports.orientation(args)
args.landscape = true | false
args.page (depreciated) or args.object = the current page

但我不知道如何将它插入到我的 typescript & angluar 2 项目中。

    import { Component, OnInit } from "@angular/core";
    var Orientation = require( "nativescript-orientation" );
    @Component({
        selector: "my-app",
        templateUrl: "app.component.html",
    })
     export class AppComponent implements OnInit {
        public counter: number = 16;
         public get message(): string {
            if (this.counter > 0) {
                return this.counter + " taps left";
            } else {
                return "Hoorraaay! \nYou are ready to start building!";
            }
        }
        ngOnInit(){
            console.log(Orientation.getOrientation()+"--phone"); 
        }
        public onTap() {
            this.counter--;
        }
   }

export function orientation(args){
}

【问题讨论】:

    标签: javascript typescript nativescript angular2-nativescript


    【解决方案1】:

    您只需为横向/纵向模式添加 CSS 规则即可。

    r.g. app.css

    .btn {
        font-size: 18;
    }
    
    .landscape Button {
        background-color: red;
        color: white;
    }
    
    .landscape StackLayout {
        background-color: gray;
    }
    

    app.component.ts

    import { Component} from "@angular/core";
    var orientation = require('nativescript-orientation');
    
    @Component({
        selector: "my-app",
        templateUrl: "app.component.html",
    })
    export class AppComponent {
    
        public onTap() {
            console.log(orientation.getOrientation());  
        }
    }
    

    app.component.html

    <StackLayout class="p-20">
        <Label text="Tap the button" class="h1 text-center"></Label>
        <Button text="TAP" (tap)="onTap()" class="btn btn-primary btn-active"></Button>
        <Button text="Sample Button"></Button>
    </StackLayout>
    

    完整的工作演示here(一旦应用加载手动旋转屏幕)

    【讨论】:

    • 我想在横向和纵向相互更改时更改布局和样式
    • 你不能用这个插件改变布局运行时。事实上你不能在 NativeScript 中改变布局运行时。但是,您可以根据可以根据方向值设置的布尔变量简单地显示/隐藏布局。此外,如果您想根据方向构建时间加载布局,那么您可以使用屏幕限定符页面docs.nativescript.org/ui/…
    猜你喜欢
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 2020-05-13
    相关资源
    最近更新 更多