cssvue.jsvue-material

Vue Material Drawer (how to display file.vue inside of md-content)


I'm trying to build a website that contains 4 files.vue (navigation.vue, Home.vue, something1.vue something2.vue) each file has its own template and style. Inside of navigation.vue I have a temporary drawer in order to navigate on each files vue.

(documentation for temporary drawer: https://vuematerial.io/components/drawer (I'm using Vue material) I have use the same code).

inside of my drawer I do have the md-toolbar, md-drawer and md-content tags (in which it creates my navigation menu:

So, I was wondering if their is a way to load one of my template (by clicking on list of item in md-drawer) into the md-content tag?


Solution

  • You can use vue-router. To install vue-router, go to your root directory and type this in the terminal:

    npm install vue-router

    Sample code snippet for App.vue or where you want to render your .vue files:

    <template>
    <v-app>
      <!-- route outlet -->
      <!-- component matched by the route will render here -->
      <router-view></router-view>
    </v-app>
    </template>
    

    Sample code snippet for routes.js file or where you'll be registering your routes:

    // 0. If using a module system (e.g. via vue-cli), import Vue and VueRouter
    // and then call `Vue.use(VueRouter)`.
    
    // 1. Define route components.
    // These can be imported from other files
    import Home from './path/to/Home';
    import Something1 from './path/to/Something1';
    import Something2 from './path/to/Something2';
    
    const routes = [
      {
            path: '/',
            name: 'Home',
            component: Home
        },
        {
            path: '/something1',
            name: 'Something1',
            component: Something1
        },
        {
            path: '/something2',
            name: 'Something2',
            component: Something2
        },
    ]
    
    // 3. Create the router instance and pass the `routes` option
    const router = new VueRouter({
      routes // short for `routes: routes`
    })
    
    // 4. Create and mount the root instance.
    // Make sure to inject the router with the router option to make the
    // whole app router-aware.
    const app = new Vue({
      router
    }).$mount('#app')
    
    // Now the app has started!
    

    You have to put each of your md options inside the vue-router-link like this:

        <md-drawer :md-active.sync="showNavigation">
          <md-toolbar class="md-transparent" md-elevation="0">
            <span class="md-title">My App name</span>
          </md-toolbar>
    
          <md-list>
            <md-list-item>
              <!-- use router-link component for navigation. -->
              <!-- specify the link by passing the `to` prop. -->
              <!-- replace "/foo" and "/bar" with your route path (e.g., "/home", "/something1", "/something2") -->
              <router-link to="/foo">
                <md-icon>move_to_inbox</md-icon>
                <span class="md-list-item-text">Inbox</span>
              </router-link>
            </md-list-item>
    
            <md-list-item>
              <router-link to="/bar">
                <md-icon>send</md-icon>
                <span class="md-list-item-text">Sent Mail</span>
              </router-link>
            </md-list-item>
    
            <md-list-item>
              <router-link to="/foo">
                <md-icon>delete</md-icon>
                <span class="md-list-item-text">Trash</span>
              </router-link>
            </md-list-item>
    
            <md-list-item>
              <router-link to="/bar">
                <md-icon>error</md-icon>
                <span class="md-list-item-text">Spam</span>
              </router-link>
              </md-list-item>
          </md-list>
        </md-drawer>
    

    You can know more about vue-router here: Vue Router Documentation:

    Feel free to upvote my answer if it helped you. Hope this helps! :)