# Setting ion-segment value dynamically/reactively

**URL:** <https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023>\
**Category:** Ionic Vue\
**Created:** [March 6, 2021, 4:33pm UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023 "2021-03-06T16:33:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [March 6, 2021, 4:33pm UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/1 "2021-03-06T16:33:10Z")

</div>

I am wondering if we are suppose to be able to set the `value` of `ion-segment` dynamically through a reactive variable. My goal is to be able to pass in a route param or query string to select a specific `ion-segment-button` on page load or `onIonViewDidEnter`.

The `ion-segment` honors my default value and selects the appropriate button, but if I change the value of `selectedSegment` the selected button doesn’t change (is not reactive).

I am currently on Ionic 5.6.0.

**Result**  
The result is the button staying on Button2 even though `selectedSegment` is set to `button3`.

![image](https://global.discourse-cdn.com/ionicframework/original/3X/2/e/2e2114fbdbe1dd5a468c7214c5a96411f47ca70c.png)

**Options API**

```javascript
<template>
    <ion-page>
        <ion-header>
            <ion-toolbar mode="md">
                <ion-title>Test selecting segment buttons</ion-title>
            </ion-toolbar>
            <ion-segment :value="selectedSegment" @ionChange="segmentChanged">
                <ion-segment-button value="button1">Button1</ion-segment-button>
                <ion-segment-button value="button2">Button2</ion-segment-button>
                <ion-segment-button value="button3">Button3</ion-segment-button>
            </ion-segment>
        </ion-header>
        <ion-content>
            <div style="text-align: center; margin-top: 15px">
                Button selected: {{ selectedSegment }}
            </div>
        </ion-content>
    </ion-page>
</template>

<script lang="ts">
import {
    IonContent,
    IonHeader,
    IonPage,
    IonTitle,
    IonToolbar,
    IonSegment,
    IonSegmentButton,
} from "@ionic/vue";
import { defineComponent } from "vue";

export default defineComponent({
    name: "Home",
    components: {
        IonContent,
        IonHeader,
        IonPage,
        IonTitle,
        IonToolbar,
        IonSegment,
        IonSegmentButton,
    },
    data() {
        return {
            selectedSegment: "button2",
        };
    },
    methods: {
        segmentChanged(e: CustomEvent) {
            this.selectedSegment = e.detail.value;
        },
    },
    mounted() {
        this.selectedSegment = "button3";
        //this.selectedSegment = this.$route.params.segment.toString();
    },
});
</script>

```

**Composition API**

```javascript
<template>
    <ion-page>
        <ion-header>
            <ion-toolbar mode="md">
                <ion-title>Test selecting segment buttons</ion-title>
            </ion-toolbar>
            <ion-segment
                :value="selectedSegment"
                @ionChange="segmentChanged"
                ref="myIonSegment"
            >
                <ion-segment-button value="button1">Button1</ion-segment-button>
                <ion-segment-button value="button2">Button2</ion-segment-button>
                <ion-segment-button value="button3">Button3</ion-segment-button>
            </ion-segment>
        </ion-header>
        <ion-content>
            <div style="text-align: center; margin-top: 15px">
                Button selected: {{ selectedSegment }}
            </div>
        </ion-content>
    </ion-page>
</template>

<script lang="ts">
import {
    IonContent,
    IonHeader,
    IonPage,
    IonTitle,
    IonToolbar,
    IonSegment,
    IonSegmentButton,
} from "@ionic/vue";
import { defineComponent, onMounted, ref } from "vue";

export default defineComponent({
    name: "Home",
    components: {
        IonContent,
        IonHeader,
        IonPage,
        IonTitle,
        IonToolbar,
        IonSegment,
        IonSegmentButton,
    },
    setup() {
        const myIonSegment = ref();
        const selectedSegment = ref("button2");

        onMounted(() => {
            selectedSegment.value = "button3";
            console.log(myIonSegment.value.$el.value); // Outputs: button2

            myIonSegment.value.$el.value = selectedSegment.value;
            console.log(myIonSegment.value.$el.value); // Outputs: button3
        });

        const segmentChanged = (e: CustomEvent) => {
            selectedSegment.value = e.detail.value;
        };

        return {
            myIonSegment,
            selectedSegment,
            segmentChanged,
        };
    },
});
</script>

```

---

<div class="post-metadata">

**Author:** ![enricodeleo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/enricodeleo/32/84666_2.png) [@enricodeleo](https://forum.ionicframework.com/u/enricodeleo)\
**Post date:** [March 8, 2021, 2:23pm UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/2 "2021-03-08T14:23:21Z")

</div>

I have the very same problem.

Oddly enough, it used to work back with @ionic/vue 5.4.0. I think vue changed some internal propagation methods but had no clue so far.

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [March 8, 2021, 2:32pm UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/3 "2021-03-08T14:32:29Z")

</div>

Thanks @enricodeleo for confirming! I just tried in `5.4.0` and `5.5.0` and it does in fact work. I will create a bug on GitHub. I was waiting to do that to see if it was even suppose to be possible (it seemed like it should work). I’ll link to the issue here once created.

---

<div class="post-metadata">

**Author:** ![ldebeasi](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ldebeasi/32/147501_2.png) [@ldebeasi](https://forum.ionicframework.com/u/ldebeasi)\
**Post date:** [March 8, 2021, 2:41pm UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/4 "2021-03-08T14:41:00Z")

</div>

Can you try using `v-model` instead of `:value`?

If that still doesn’t work, I can take a look if you provide a GitHub repo.

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [March 8, 2021, 2:48pm UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/5 "2021-03-08T14:48:02Z")

</div>

That totally works! Thank you sooooooo much. I don’t know how many hours I spent on this 🙂 I always say the smallest bug/issue/fix always takes the longest time…lol.

---

<div class="post-metadata">

**Author:** ![ldebeasi](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ldebeasi/32/147501_2.png) [@ldebeasi](https://forum.ionicframework.com/u/ldebeasi)\
**Post date:** [March 8, 2021, 3:00pm UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/6 "2021-03-08T15:00:45Z")

</div>

I think the issue is that when you set `:value` you are setting it to be the ref object itself, whereas `v-model` knows how to listen for changes/get the value of the ref.

I can work on some docs for this, since it is confusing at first glance.

---

<div class="post-metadata">

**Author:** ![a2d0](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/a2d0/32/136913_2.png) [@a2d0](https://forum.ionicframework.com/u/a2d0)\
**Post date:** [March 19, 2021, 12:52pm UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/7 "2021-03-19T12:52:29Z")

</div>

That would be useful, I’ve been stuck on this for ages, but using v-model worked fine.  
I thought I’d already tried that earlier to no avail, but some other code changes must have made it work.  
Awesome!

---

<div class="post-metadata">

**Author:** ![lazmeister](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/lazmeister/32/140254_2.png) [@lazmeister](https://forum.ionicframework.com/u/lazmeister)\
**Post date:** [July 18, 2022, 11:49pm UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/8 "2022-07-18T23:49:06Z")

</div>

Having this issue as well in Angular 13, had to resort to using ngModel but its also a form so running into the warning in Angular 6+ does not allow ngModel in forms.

---

<div class="post-metadata">

**Author:** ![TheRealMaN](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/therealman/32/145581_2.png) [@TheRealMaN](https://forum.ionicframework.com/u/TheRealMaN)\
**Post date:** [November 20, 2023, 5:16am UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/9 "2023-11-20T05:16:33Z")

</div>

Can you post a code example of using ion-segment with v-model please?

---

<div class="post-metadata">

**Author:** ![dolthead](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dolthead/32/93571_2.png) [@dolthead](https://forum.ionicframework.com/u/dolthead)\
**Post date:** [November 20, 2023, 5:44am UTC](https://forum.ionicframework.com/t/setting-ion-segment-value-dynamically-reactively/206023/10 "2023-11-20T05:44:26Z")

</div>

There are several examples on this website:

[https://ionic-vue-components.web.app/home](https://ionic-vue-components.web.app/home)

The code is here:

> <https://github.com/dolthead/vue-components/tree/main/src/components/segments>
>
> //github.com/dolthead/vue-components/tree/main/src/components/segments
