cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="app">
  <h1>Passing Route Parameters to Component Props</h1>
  
  <p>Using the User component on a route change:</p>
  <nav class="mainNav">
    <!-- use router-link component for navigation. -->
    <!-- specify the link by passing an object to the `to` prop. -->
    <router-link :to="{ name: 'userList'}">User List</router-link>
    <router-link :to="{ name: 'user', params: { userId: 123 } }">User 123</router-link>
    <router-link :to="{ name: 'user', params: { userId: 456, userData: { age: 37, name: 'Patrick'} } }">User 456</router-link>
    <router-link :to="{ name: 'named'}">Named Views Demo</router-link>
  </nav>
  
  <section class="mainBody">
    <!-- route outlet -->
    <!-- component matched by the route will render here -->
    <router-view></router-view>
  </section>
</div>
            
          
!
            
              #app {
  max-width: 800px;
  margin: auto;
}

nav.mainNav {
  > * {
    padding: 0 0.75rem;
    text-decoration: none;
  }
  
  > *:nth-last-child(n+2) {
    border-right: 1px solid #aaa;
  }
}

.mainBody {
  border-top: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  margin: 1rem 0;
  padding: 1rem 2rem;
  min-height: 55px;
}

.userList {
  display: flex;
  align-items: flex-start;
  
  > * {
    flex: 1;
    max-width: 50%;
    padding: 0 1rem;
  }
}

.userLink {
  &:hover {
    cursor: pointer;
  }
}
            
          
!
            
              const User = { 
  props: {
    userId: {
      type: Number,
      required: true
    },
    userData: {
      type: Object,
      required: false // User can accept a userData object on params, or not. It's totally optional.
    }
  }, 
  template: `
    <div>
      <h3>User ({{userId}})</h3>
      <div v-if="userData">
        <p>This route makes use of some option data that was passed in:</p>
        <p>{{userData.name}}({{userData.age}} yrs old)</p>
      </div>
    </div>
  `, 
  methods: {
  }
}

const UserList = {
  data() {
    return {
      selectedUser: {} // selected user data. Place this here to make sure it's reactive.
    }
  },
  template: `
    <div class="userList">
      <div class="left">
        <h2>User List</h2>
        <p>Click a user below and we'll use the User component now as a child component to display the data:</p>
        <ul>
          <li class="userLink" @click="loadUserData(123)">User 123</li>
          <li class="userLink" @click="loadUserData(456)">User 456</li>
        </ul>
      </div>
      <div class="right" v-if="selectedUser.userId">

        <!-- User is here all the time, and we just display it when we need to. 
              We give it the data it needs as a prop. -->
        <User :userId="selectedUser.userId" :userData="selectedUser.userData">
      </div>
    </div>
  `,
  methods: {
    loadUserData(id) {
      // this could be some ajax call to get user data from an API, but we'll just set some dummy data:
      this.selectedUser = id === 123
                ? { userId: 123 }
                : { userId: 456, userData: {age: 37, name: 'Patrick'}}
    }
  },
  /* 
    On the UserList component we use the User component as a child component 
    and pass data down via props.
  */
  components: {
    User
  }
}

const Sidebar = {
  template: `<div>This is the sidebar. No props here.</div>`
}

const NamedWrapper = {
  template: `
    <div>
      <h2>Users loaded into named views:</h2>
      <ul>
        <li><router-link :to="{ name: 'named_id', params: { userId: 123 } }">User 123</router-link></li>
        <li><router-link :to="{ name: 'named_id', params: { userId: 456, userData: { age: 37, name: 'Patrick'} } }">User 456</router-link></li>
      </ul>
      <div class="userList">
        <router-view class="left" name="user_details"></router-view>
        <router-view class="right" name="sidebar"></router-view>
      </div>
    </div>
  `, 
}




/* Router and App setup: */
const routes = [
  {
    path: '/user',
    name: 'userList',
    component: UserList
  },
  
  // use /named as the parent route to wrap our two named views:
  {
    path: '/named',
    name: 'named',
    component: NamedWrapper,
    children: [
      {
        path: '/user/:userId',
        name: 'named_id',
        components: { user_details: User, sidebar: Sidebar },
        props: { user_details: true, sidebar: false }
      }
    ]
  },
  {
    path: '/user/:userId',
    name: 'user',
    component: User,
    props: true,
  }
]

const router = new VueRouter({
  routes
})

const app = new Vue({
  router
}).$mount('#app')
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console