У меня проблема с NativeScript 2.0 CSS и пользовательскими компонентами. Кажется, в моих знаниях огромный пробел, и мне не хватает чего-то важного, что неочевидно.
Возьмите пустое приложение NS 2.0, созданное с помощью
$ tns create test --ng
Удалите содержимое app.css (для предотвращения побочных эффектов).
Измените app.component.ts:
import {Component} from "@angular/core";
@Component({
selector: "my-app",
template: `
<StackLayout orientation="vertical">
<Label text="Label in first StackLayout"></Label>
<StackLayout orientation="vertical"
style="width: 80%;background-color: red;">
<Label text="Label in second StackLayout"></Label>
</StackLayout>
</StackLayout>
`,
})
export class AppComponent {}
Довольно простые вещи. Дает следующий ожидаемый результат:
Давайте попробуем преобразовать этот внутренний StackLayout в компонент многократного использования.
custom.component.ts
import {Component} from "@angular/core";
@Component({
selector: "Custom",
template: `
<StackLayout orientation="vertical"
style="width: 80%;background-color: red;">
<Label text="Label in second StackLayout"></Label>
</StackLayout>
`,
})
export class CustomComponent {}
Измените app.component.ts
import {Component} from "@angular/core";
import {CustomComponent} from "./custom.component"
@Component({
selector: "my-app",
directives: [CustomComponent],
template: `
<StackLayout orientation="vertical">
<Label text="Label in first StackLayout"></Label>
<Custom></Custom>
</StackLayout>
`,
})
export class AppComponent {}
Теперь результат выглядит так:
Цвет фона применяется, а ширина - нет.
Я даже пробовал:
<Custom style="width: 80%;"></Custom> /* Does this even make sense? */
но безрезультатно.
Я понимаю, что проценты являются экспериментальными, но подозреваю, что ошибка в моем коде, а не в NativeScript .
Где я ошибся?