Skip to content

TypeError: this._routerRoot is undefined #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kerolloz opened this issue Jul 19, 2020 · 2 comments · Fixed by #434
Closed

TypeError: this._routerRoot is undefined #432

kerolloz opened this issue Jul 19, 2020 · 2 comments · Fixed by #434

Comments

@kerolloz
Copy link

example github repository: https://github.com/kerolloz/vue-class-component-bug

[Vue warn]: Error in data(): "TypeError: this._routerRoot is undefined"

<template>
  <div>
    <h1>{{ title }}</h1>
    <p>Path: {{ currentPath }}</p>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";

@Component
export default class HelloWorld extends Vue {
  // data() {
  //   return {
  //     title: "Bug",
  //     currentPath: this.$route.path,
  //   };
  // }
  title = "Bug";
  currentPath = this.$route.path;
}
</script>
@ktsn
Copy link
Member

ktsn commented Jul 20, 2020

Because we don't forward private properties when initializing this value in property initializers. We can just allow such properties to make it work.
https://github.com/vuejs/vue-class-component/blob/master/src/data.ts#L20

@kerolloz
Copy link
Author

kerolloz commented Jul 20, 2020

Thank you @ktsn for making that clear. I didn't see any notice in the README or documentation page about not binding the private properties of Vue components to the class. I tried some workarounds like defining currentPath as a computed property

@Component
export default class HelloWorld extends Vue {
  title = "Bug";
  get currentPath() { return this.$route.path }
}

or using the old-style data() method as the commented part in the code. But I believe that neither is a good workaround.

are there any PRs coming to fix this or should I get started making one?


Because we don't forward private properties when initializing this value in property initializers

out of curiosity: Is this a design decision for vue-class-component? or what?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants