Search code examples
vue.jsstylesnativescriptbind

Nativescript Vue - binding a string to Label style attribute does not work


I just created a blank Nativescript Vue project (Typescript) and tried to bind a string to the type attribute of a Label - does not work (style gets completely ignored). I also tried in Nativescript Playground, same result.

Example Code is:

<template>
    <Page>
        <ActionBar title="Example" />
        <StackLayout>
            <Label style="font-size: 34" text="WORKS" />
            <Label :style="myStyle" text="DOES NOT WORK" />
        </StackLayout>
    </Page>
</template>

<script>
    export default {
        data() {
            return {
                myStyle: "font-size: 34"
            };
        }
    };
</script>
<style scoped>
</style>

You can reproduce in Nativescript Vue Playground. On my Mac I'm using Nativescript 8.1.2 with nativescript-vue 2.9.0.

I would expect it should be possible to do something like this. Any help appreciated, thank you.


Solution

  • In order to use a computed style with the bind mechanic, you need a styleObject consisting of the rule-name as key, and the value as... well the value.

    <template>
        <Page>
            <ActionBar title="Example" />
            <StackLayout>
                <Label style="font-size: 34" text="WORKS" />
                <Label :style="myStyle" text="DOES NOT WORK" />
            </StackLayout>
        </Page>
    </template>
    
    <script>
        export default {
            data() {
                return {
                    myStyle: {
                      font-size: '34px'
                    },
                };
            }
        };
    </script>
    <style scoped>
    </style>