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
.
Options API
<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
<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>