Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Vue.component('l-link', {
  2.     template: `
  3.         <a v-bind:href="url" v-on:click="go"><slot></slot></a>
  4.     `,
  5.     props: {
  6.         url: {
  7.             type:String,
  8.             required: true
  9.         }
  10.     },
  11.     methods: {
  12.         go: function (event) {
  13.             event.preventDefault();
  14.             var stateObj = { foo: "bar" };
  15.             history.pushState(stateObj, "page 2", this.url);
  16.             this.$root.currentRoute = this.url;
  17.         }
  18.     }
  19. });
  20.  
  21. const NotFound = { template: '<p>Страница не найдена</p>' }
  22. const Home = { template: '<l-link url="/vsvue/about">About</l-link><p>главная</p>' }
  23. const About = { template: '<l-link url="/vsvue/">Home</l-link><p>о нас</p>' }
  24.  
  25. const routes = {
  26.     '/vsvue/': Home,
  27.     '/vsvue/about': About
  28. }
  29.  
  30. new Vue({
  31.     el: '#app',
  32.     data: {
  33.         currentRoute: window.location.pathname
  34.     },
  35.     computed: {
  36.         ViewComponent () {
  37.             return routes[this.currentRoute] || NotFound
  38.         }
  39.     },
  40.     render (h) { return h(this.ViewComponent) }
  41. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement