Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ===========================================
  2.               Our Pretend User Data
  3. =========================================== */
  4.  
  5. const userList = {
  6.   1: {
  7.     userName: 'Peter Parker',
  8.     heroName: 'Spiderman'
  9.   },
  10.   2: {
  11.     userName: 'Bruce Banner',
  12.     heroName: 'Hulk'
  13.   },
  14.   3: {
  15.     userName: 'Tony Stark',
  16.     heroName: 'Ironman'
  17.   }
  18. }
  19.  
  20. /* ===========================================
  21.               Our Components
  22. =========================================== */
  23.  
  24. const Home = {
  25.   template: `
  26.     <section>This is the home screen.</section>
  27.   `
  28. }
  29.  
  30. const Foo = {
  31.   template: `
  32.     <section>You will not see this template when Foo is redirected to Home.</section>
  33.   `
  34. }
  35.  
  36. const Err = {
  37.   template: `
  38.     <section>
  39.       You will not see this template. The error is passed to our error handler.
  40.     </section>
  41.   `
  42. }
  43.  
  44. const Bar = {
  45.   template: `
  46.     <section>
  47.       <div>Bar - <span @click="testFunc" style="color: #cc0000; cursor: pointer;">testFunc()</span></div>
  48.     </section>
  49.   `,
  50.   // set guard on the component options object:
  51.   beforeRouteEnter ( to, from, next ) {
  52.     // console.log('Entering Bar')
  53.     // Pass a callback to next (optional)
  54.     next(vm => {
  55.       // this callback has access to component instance (ie: 'this') via `vm`
  56.       vm.testFunc('Some Message', true)
  57.       // console.log("Fully Entered Bar")
  58.     })
  59.   },
  60.   // set guard on the component options object:
  61.   beforeRouteLeave ( to, from, next ) {
  62.     // console.log('Leaving Bar')
  63.     next()
  64.   },
  65.   methods: {
  66.     testFunc(msg, fromGuard = false) {
  67.       fromGuard ? console.log(`This message called from the guard: ${msg}`) : console.log(`This message called from component.`)
  68.     }
  69.   }
  70. }
  71.  
  72. /* ===========================================
  73.         This User Component will be Reused
  74. =========================================== */
  75. const User = {
  76.   props: ["userId"],
  77.   template: `
  78.     <div>
  79.       <div v-if="user">User - {{user.userName}}({{userId}}) = {{user.heroName}}</div>
  80.     </div>
  81.   `,
  82.   data() {
  83.    return {
  84.      user: {
  85.        userName: '',
  86.        heroName: ''
  87.      }
  88.    }
  89.   },
  90.   mounted() {
  91.     this.user = this.getUser(this.userId)
  92.   },
  93.   methods: {
  94.     getUser(id) {
  95.       // console.log('Fetching User')
  96.       return userList[id]
  97.     }
  98.   },
  99.   // set guard on the component options object:
  100.   beforeRouteLeave ( to, from, next ) {
  101.     // console.log('Leaving User')
  102.     next(false)
  103.   },
  104.   /* Called when this component is reused.
  105.   Here we have a chance to update the component
  106.   since mounted() and other life-cycle hooks won't be called
  107.   when a component is reused. */
  108.   beforeRouteUpdate ( to, from , next ) {
  109.     // console.log('Reusing this component.')
  110.     this.user = this.getUser(to.params.userId)
  111.       // console.log('Entering User', to.params.userId)
  112.    
  113.     next()
  114.   }
  115. }
  116.  
  117. /* ===========================================
  118.               Per-route Guards
  119. =========================================== */
  120.  
  121. const routes = [
  122.   { path: '/', name: 'home', component: Home},
  123.   { path: "/bar", name: 'bar', component: Bar },
  124.   { path: "/foo", name: 'foo', component: Foo },
  125.   { path: "/error", name: 'error', component: Err },
  126.   {
  127.     path: "/user/:userId",
  128.     name: "user",
  129.     component: User,
  130.     props: true,
  131.     // set nav guard on the route definition object:
  132.     beforeEnter: (to, from, next) => {
  133.       console.log('Entering User', to.params.userId)
  134.       to.params.myCustomizations = {
  135.        
  136.       }
  137.       next()
  138.     }
  139.   }
  140.  
  141. ]
  142.  
  143. const router = new VueRouter({
  144.   routes // short for routes: routes
  145. })
  146.  
  147. /* ===========================================
  148.               GLOBAL GUARDS
  149.  =========================================== */
  150.  
  151. // GLobal BEFORE hooks:
  152. router.beforeEach((to, from, next) => {
  153.   // console.log('Global -- beforeEach - fired')
  154.  
  155.   // re-route
  156.   if (to.path === '/foo') {
  157.     next('/')
  158.   }
  159.   // Abort navigation based on some criteria:
  160.   // else if (to.path === '/user/1') {
  161.   //   next(false)
  162.   // }
  163.   else if (to.path === '/error') {
  164.     const err = new Error('My Error Message')
  165.    
  166.     // pass the error to onError() callback.
  167.     next(err)
  168.   }
  169.   else {
  170.     next()
  171.   }
  172. })
  173.  
  174. // Global beforeResolve
  175. router.beforeResolve((to, from, next) => {
  176.   // console.log('Global -- beforeResolve - fired.')
  177.   next()
  178. })
  179.  
  180. // GLobal AFTER hooks:
  181. router.afterEach((to, from) => {
  182.   // This fires after each route is entered.
  183.   // console.log(`Global -- afterEach - Just moved from '${from.path}' to '${to.path}'`)
  184. })
  185.  
  186. // Register an Error Handler:
  187. router.onError(err => {
  188.   console.error('Handling this error', err.message)
  189. })
  190.  
  191.  
  192. // mount the app
  193. const app = new Vue({
  194.   router
  195. }).$mount("#app")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement