-3

Please help me with my homework.
I make a system with vueJS (vueCLI, vue-router).
I want to get the id from the url, but an error occurs in my writing. How can I solve it?

Error

Uncaught ReferenceError: $route is not defined

router.js

export default new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/s/:id",
      name: "Result",
      component: Result
    },

Result.vue

<script>
export default {

  methods: {
    window: (onload = function() {
      const id = this.$route.params.id;
    })
  }
};
</script>
byxor
  • 5,930
  • 4
  • 27
  • 44
kan
  • 45
  • 11

1 Answers1

0

Maybe you've to take a look on the lifecycle of vue. (https://v2.vuejs.org/v2/guide/instance.html)

When you want to do something onload, you should implement beforeCreate, created, beforeMount or mounted.

And this.$route is not reachable because this you're creating a function in a function and then the this is scoped to the inner function not the vue context anymore. To solve this you could use arrow functions (https://www.w3schools.com/js/js_arrow_function.asp)

tony19
  • 125,647
  • 18
  • 229
  • 307
dreijntjens
  • 4,577
  • 26
  • 28