Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app">
  <router-link to="/">Home</router-link>
  &nbsp;|&nbsp;
  <router-link to="/product/red-dress">/product/red-dress</router-link>
  &nbsp;|&nbsp;
  <router-link to="/product/blue-dress">/product/blue-dress</router-link>
  <br><br>
  <transition name="slide-left">
    <router-view class="view" :key="$route.fullPath" />
  </transition>
</div>

              
            
!

CSS

              
                #app {
  text-align: center;    
}
.view {
  position: absolute;
  width: 100%;
  transition: transform 1000ms linear;
}
.slide-left-enter, .slide-right-leave-active {
  transform: translate3d(300px, 0, 0);
}
.slide-left-leave-active, .slide-right-enter {
  transform: translate3d(-300px, 0, 0);
}

              
            
!

JS

              
                // Empty store we can register our modules into
const store = new Vuex.Store({});

// Module to be used for the /product/:slug route
const productModule = {
  namespaced: true,
  // State must be a function so we can re-use this module across multiple routes
  state: () => ({
    slug: null,
  }),
  mutations: {
    setSlug(state, slug) {
      state.slug = slug;
    },
  },
};

// Homepage component
const HomeRouteComponent = {
  render(h) {
    return h('h1', 'Homepage');
  },
};

function mapInstanceState(getModuleNameFn, mappers) {
    // Create an object of the same shape but with namespaced mapper functions
    const namespacedMappers = {};
    Object.entries(mappers).forEach((entry) => {
        const [name, mapper] = entry;
        // Note: Do not use an arrow function because we do _not_ want to
        // capture the `this` value at this point, we need the runtime 
        // component instance to be the `this` value
        namespacedMappers[name] = function (state) {
            // Determine the namespaced module state
            const namespace = getModuleNameFn(this);
            // "Walk" the state tree step by step,m just in case we have a 
            // deeply nested namespace
            const moduleState = namespace.split('/')
                .reduce((acc, p) => acc[p], state);
            // Call the original mapper function with the moduleState
            return mapper.call(this, moduleState);
        };
    });

    // Pass through our namespaced mappers to the normal mapState function
    return Vuex.mapState(namespacedMappers);
}

const getNamespace = cmp => `product--${cmp.$route.params.slug}`;

// This is the vue-router route-level component rendered for 
// a given product based on the slug, i.e., /product/red-dress 
// or /product/blue-dress
const ProductRouteComponent = {
  // Each instance of this rotue will use the following namespaced 
  // vuex module
  vuex: {
    moduleName: getNamespace,
    module: productModule,
  },
  // This method will load data for the given route into the 
  // namespaced vuex module
  async fetchData(route, store) {
    // Simulate something async
    await new Promise(r => setTimeout(r, 100));
    const namespace = getNamespace({ $route: route });
    store.commit(`${namespace}/setSlug`, route.params.slug);
  },
  computed: {
    ...mapInstanceState(getNamespace, {
      slug: state => state.slug,
    }),
  },
  render(h) {
    return h('h1', `Slug: ${this.slug}`);          
  },
};

const router = new VueRouter({
  routes: [{ 
    path: '/',
    component: HomeRouteComponent,
  }, {
    path: '/product/:slug', 
    component: ProductRouteComponent,
  }]    
});

router.onReady(() => {
  router.beforeResolve(async (to, from, next) => {
    // Note this is a simplistic approach for example purposes.  In 
    // a real-world app you will need to do some comparisons between 
    // prior routes and avoid double-registering modules
    
    // Find out the components that match our destination route.  This 
    // will only be more than one component if you re using nested routes
    const matched = router.getMatchedComponents(to);

    // Register moules for each destination component
    matched
      .filter(cmp => cmp.vuex)
      .forEach(cmp => store.registerModule(
        cmp.vuex.moduleName({ $route: to }), 
        cmp.vuex.module,
      ));
        
    // Execute fetchData for each destination component
    const promises = matched
      .filter(cmp => cmp.fetchData)
      .map(cmp => cmp.fetchData(to, store));

    await Promise.all(promises);
    next();
  });
});          

new Vue({
  el: '#app',
  router,
  store,
});
              
            
!
999px

Console