Vicky Sultan
Vicky Sultan

Reputation: 73

Access other component data and method from another component

I have two components:

ContainerSidebar.vue

<!-- Sidebar -->
    <div id="b-sidebar">
      <div class="change-image">
        <img
          :src="profile.avatar != null ? profile.avatar+'#'+Date.now() : 'https://redacted.com/avatar/no-image.png'"
          alt=""
        >
      </div>
      <h6>{{profile.email}}</h6>
      <a-menu
        :default-selected-keys="['1']"
        mode="inline"
        theme="dark"
        style="height: 100%;"
      >
        <a-menu-item key="1" style="margin-top: 70px" v-if="this.$route.path !== '/profile'">
          <home-icon icon-name="home" style="margin-right: 10px"></home-icon>
          <span>Beranda</span>
        </a-menu-item>
        <a-menu-item key="1" style="margin-top: 30px" v-else>
          <a-icon type="user" style="margin-right: 10px; font-size: 18px;"/>
          <span>Profil Saya</span>
        </a-menu-item>

        <a-sub-menu key="sub1" v-if="this.$route.path !== '/profile'">
          <span slot="title">
            <book-icon icon-name="book" style="margin-right: 10px"></book-icon>
          <span>Kelas Saya</span></span>
          <a-menu-item key="5" style="padding-left: 20px">
            <a-icon type="calendar" style="font-size: 18px"/>
            Jadwal Webinar
          </a-menu-item>
          <a-menu-item key="6" style="padding-left: 20px">
            <award-icon icon-name="home" style="margin-right: 10px"></award-icon>
            Sertifikat Saya
          </a-menu-item>
        </a-sub-menu>



        <a-menu-item key="2" v-if="this.$route.path !== '/profile'">
          <bag-icon icon-name="bag" style="margin-right: 10px"></bag-icon>
          <span>Katalog Saya</span>
        </a-menu-item>
        <a-menu-item key="2" v-else>
          <a-icon type="lock" style="font-size: 18px"/>
          <span>Ubah Sandi</span>
        </a-menu-item>

        <a-menu-item key="3" v-if="this.$route.path !== '/profile'">
          <file-icon icon-name="file" style="margin-right: 10px"></file-icon>
          <span>Projek Saya</span>
        </a-menu-item>
        <a-menu-item key="3" v-else>
          <a-icon type="question-circle" style="font-size: 18px"/>
          <span>Panduan</span>
        </a-menu-item>

        <a-menu-item key="4" v-if="this.$route.path === '/profile'" @click="logout">
          <i class="fas fa-sign-out-alt" style="margin-right: 13px"></i>
          <span>Keluar</span>
        </a-menu-item>

      </a-menu>
    </div>

The other one is Profile.vue, which the part i want to show is only the js part

export default {
  data() {
    return {
      page: 'profile',
    };
  },
  methods: {
    change_page(page) {
      this.page = page;
    },
  },

Consider the ContainerSidebar.vue is the parent component and the Profile.vue is the child component. How do i access the change_page() method which manipulate the page data variable on the Profile.vue internally? What i expected from this is to put the change_page() method on this part

<a-menu-item key="2" v-else>
   <a-icon type="lock" style="font-size: 18px"/>
   <span>Ubah Sandi</span>
</a-menu-item>

which uses @click event. I found a several example in here as such Access data from another Component but i still couldn't figure out the similarity as my own code.

Upvotes: 0

Views: 196

Answers (2)

Vicky Sultan
Vicky Sultan

Reputation: 73

Solved this with Vuex:

store.js


    import Vue from 'vue';
    import Vuex from 'vuex';
    
    Vue.use(Vuex);
    
    const store = new Vuex.Store({
      state: {
        profile_page: 'profile',
      },
      mutations: {
        changePage(state, page) {
          state.profile_page = page;
        }
      },
    });
    
    export default store

And then commit the state on both parent and child component, i made a method with the commit statement inside the method:

    changePage(page) {
       this.$store.commit('changePage', page);
    },

and add a click event afterwards inside the html tag like this:


    @click="changePage("page_name")"

Problem solved.

Upvotes: 1

Andriy Yushko
Andriy Yushko

Reputation: 415

You need emit your parent method from child like this:

<parent-component class="container">
  <your-child-component @changePage="change_page"></your-child-component>
</parent-component>

And in your child component js code:

export default {
methods: {
    handleChangePage() {
        this.$emit('changePage', '/profile');
    },
},
};

After call handleChangePage inside child method will by executed parent method change_page

Upvotes: 0

Related Questions