Thandor
Thandor

Reputation: 25

Nativescript Vue toggle button with v-bind

I'm new to nativescript vue and I have a question about a simple "toggle" i want to make. When i press a button, it should change the backgroundcolor. I tried to make this happen with v-bind. I know that it isnt the most beautiful code... ;-)

<template>
  <Page class="page">
    <ActionBar title="Einstellungen">
    </ActionBar>
    <StackLayout orientation="vertical" class="page-content">

      <StackLayout orientation="horizontal" class="nix">
      
        <StackLayout :class="{ active: isMaleActive }" ref='layoutMale' class="btn-img" orientation="vertical" padding="10" @tap="onTappedGender('male')" >
            <Image src="~/assets/images/male.png" />
            <Label text="Männlich" verticalAlignment="center"></Label>
        </StackLayout>

        <StackLayout :class="{ active: isFemaleActive }" ref='layoutFemale' class="btn-img" orientation="vertical" padding="10" @tap="onTappedGender('female')" >
            <Image src="~/assets/images/female.png" />
            <Label text="Männlich" verticalAlignment="center"></Label>
        </StackLayout>
      </StackLayout>

    </StackLayout>
  </Page>
</template>

<script>
    export default {
        data: {
            isMaleActive: false,
            isFemaleActive: false,
        },
        name: 'settings-view',
        methods: {
            onTappedGender(gender){
                //console.log(gender);
                if (gender == "male") {
                    this.isMaleActive = true;
                    this.isFemaleActive = false;
                } else {
                    this.isMaleActive = false;
                    this.isFemaleActive = true;
                }
                console.log(this.isMaleActive);
            }
        },
    }
</script>

<style scoped>
    ActionBar {
        background-color: #53ba82;
        color: #ffffff;
    }
    .btn-img{
        border-radius: 5;
        border-width: 1;
        color: white;
        margin: 10;
        font-size: 22;
        border-color: #2b3c6a;
        height: 80;
        width: 80;
    }

    .active{
        background-color: blue;
    }
</style>

So, what is wrong with the code? Thank you very much.

Upvotes: 0

Views: 1285

Answers (2)

Manoj
Manoj

Reputation: 21908

Within a Vue component, data should be a function that returns an object.

Playground sample

Upvotes: 1

Duhoux Pierre-Louis
Duhoux Pierre-Louis

Reputation: 199

You can not write this because you bind class attribute and set it as static. <StackLayout :class="{ active: isMaleActive }" ... class="btn-img" >

What you need to do is : <StackLayout :class="[{ active: isMaleActive }, 'btn-img']" >

Upvotes: 0

Related Questions