Skip to content
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

vm arg in next callback in beforeRouteEnter there is an inconsistent result with <script setup> #1367

Closed
richex-cn opened this issue Apr 11, 2022 · 1 comment

Comments

@richex-cn
Copy link

richex-cn commented Apr 11, 2022

Version

4.0.14

Reproduction link

https://codesandbox.io/s/issue-vue3-router-beforerouteenter-q81re4
https://github.com/richex-cn/issue-vue-router-beforeRouteEnter

Steps to reproduce

Open reproduction links, and open console to view result.

In the previous reproduction, The result as follows:

<script>
export default {
  beforeRouteEnter: (to, from, next) => {
    console.log("home beforeRouteEnter");
    next((vm) => {
      console.log("vm.now", vm.now); // undefined
      vm.$nextTick(() => {
        console.log("vm.now", vm.now); // undefined
      });
      vm.$nextTick(function () {
        console.log(this === vm); // true
        console.log("this.now", this.now); // undefined
        console.log("this.getName", this.getName); // undefined
      });
    });
  },
};
</script>

<script setup>
import { ref } from "vue";

console.log("home setup");

const now = ref(Date.now());
const getName = () => "Vue";
</script>

In the second example, The result as follows:

<script lang="ts">
export default {
  beforeRouteEnter: (to, from, next) => {
    console.log('home beforeRouteEnter')
    next(vm => {
      console.log('vm.now', vm.now) // undefined
      vm.$nextTick(() => {
        console.log('vm.now', vm.now) // undefined
      })
      vm.$nextTick(function () {
        console.log(this === vm) // false
        console.log('this.now', this.now) // 1649657580119
        console.log('this.getName', this.getName) // () => "Vue"
      })
    })
  }
}
</script>

<script lang="ts" setup>
import { ref } from 'vue'

console.log('home setup')

const now = ref(Date.now())
const getName = () => 'Vue'
</script>

If you remove <script setup>, all of them are normal, You can view the detail.vue file of two reproductions.

What is expected?

Keep consistent result

What is actually happening?

Got inconsistent result


BTW, The current beforeRouteEnter and TypeScript don't seem to work together. Some related issues:
vuejs/vue-router#1863
#701

@posva
Copy link
Member

posva commented Apr 11, 2022

Unfortunately this is inherent to how script setup works. You need to use defineExpose() if you want to use this.now and others. See the generated JS code and you will see nothing is returned inside setup.

@posva posva closed this as completed Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants