NativeScript/NativeScript Angular - 如何为页面设置背景图片?
问题描述:
我试图创建一个背景图像,但我似乎无法要能做到这一点。我看了一下这个问题:NativeScript/NativeScript Angular - 如何为页面设置背景图片?
Nativescript background-image fullscreen
,但没有任何运气。它没有显示足够的标记,而且OP似乎也没有使用nativescript-angular
,因为当您按照建议使用<Page></Page>
时会出现错误。
和this issue表示Page
不应该与角度一起使用,而且我很积极我在文档中阅读过相同的内容。
我使用这个CSS:
.login-page {
width: 100%;
height: 100%;
padding: 40px 60px;
background-image: ~/assets/img/login.jpg;
background-position: 50% 50%;
background-size: cover;
background-color: blue;
}
有了这个模板:
<StackLayout class="login-page text-center" orientation="vertical" [formGroup]="loginForm">
<StackLayout class="input-field m-b-20">
<TextField class="input input-rounded" formControlName="email" hint="Email"></TextField>
</StackLayout>
<StackLayout class="input-field">
<TextField class="input input-rounded" formControlName="password" hint="Password" secure="true"></TextField>
</StackLayout>
</StackLayout>
的图像是在app/assets/img/login.jpg
。任何想法为什么这不起作用?
答
在组件Login.ts:
import { Page } from "ui/page";
export class LoginComponent {
constructor(private page:Page) {
page.backgroundImage = '~/images/login.jpg';
}
}
必须创建应用程序根目录的文件夹命名相似图片的Nativescript文档说。
您的代码应该按预期工作,或者至少完全相同的场景在我的身边工作 - 这里是测试项目https://github.com/NickIliev/NativeScript-Issues-2017/tree/master/*/backgroundImage –
您不能直接使用“页面”,但可以通过编程方式。在组件的'ngOnInit'中,你可以'this.page.backgroundImage =“res:// login”' – jalal