search

Home  >  Q&A  >  body text

How to implement Vue application to transfer selected options to another page

<p>On the first page of my Vue app, I have a dropdown menu containing a list of email addresses. </p> <p>I want to save the selected value/text and use it as a parameter or variable on the inbox page I route to. </p> <p>This is the code for the dropdown menu using v-autocomplete: </p> <pre class="brush:html;toolbar:false;"><v-autocomplete dense filled label="Select Email" v-model="mailboxes" :items="mailboxes" item-text='mailbox' item-value='mailbox'> </v-autocomplete> </pre> <p>This is a button that acts as a v-btn and is used to route to the inbox page. </p> <pre class="brush:html;toolbar:false;"><v-btn rounded color="primary" @click="$router.push('Inbox')"> Load mailbox</v-btn> </pre> <p>How do I save the value of a selected mailbox for use on the inbox page I route to? </p>
P粉308089080P粉308089080480 days ago582

reply all(2)I'll reply

  • P粉083785014

    P粉0837850142023-08-29 00:49:28

    Pass the selected value as a routing parameter:

    $router.push({
        name: "Inbox",
        params: { selectedValue: YOUR_VALUE }
      });

    From the Inbox page you can access via:

    $route.params.selectedValue

    reply
    0
  • P粉038161873

    P粉0381618732023-08-29 00:36:29

    I suggest you start using Vuex :)

    This is a library that can share reactive data objects throughout your application.

    Here are examples of your possible code:

    // /store/index.js
    
    export state: () => {
       mailbox: '',
    }
    
    export mutation: () => {
       SET_MAILBOX(state, mailbox) {
          state.mailbox = mailbox
       }
    }
    
    // your-page.vue
    <template>
        <v-autocomplete
            v-model="mailboxes"
            dense
            filled
            label="选择邮箱"
            :items="mailboxes"
            item-text='mailbox'
            item-value='mailbox'>
          </v-autocomplete>
    </template>
    
    <script>
    export default {
       computed: {
          mailboxes: {
             get() {
                this.$store.state.mailbox // 从Vuex存储中获取值
             },
             set(newMailbox) {
                this.$store.commit('SET_MAILBOX', newMailbox) // 更新Vuex存储
             },
          }
       }
    }
    </script>

    reply
    0
  • Cancelreply