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

useRoute() return value will delay a moment #533

Closed
TimRChen opened this issue Oct 14, 2020 · 4 comments
Closed

useRoute() return value will delay a moment #533

TimRChen opened this issue Oct 14, 2020 · 4 comments

Comments

@TimRChen
Copy link

TimRChen commented Oct 14, 2020

Version

4.0.0-beta.13

Reproduction link

https://codesandbox.io/s/awesome-framework-yifz2?file=/src/App.vue

Steps to reproduce

  1. open console
  2. watch print content

What is expected?

// 1. "Home"
// 2. "Home"

What is actually happening?

// 1. undefined
// 2. "Home"


I have 'RTMD', but still don't know why this happend.

@TimRChen
Copy link
Author

//main.js
import { createApp } from "vue";
import App from "./App.vue";
import { createRouter, createWebHashHistory } from "vue-router";

const Home = { template: "<div>Home</div>" };
const About = { template: "<div>About</div>" };

const routes = [
  { path: "/", name: "Home", component: Home },
  { path: "/about", name: "About", component: About }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

createApp(App).use(router).mount("#app");
<!-- App.vue -- >
<template>
    <router-view />
</template>

<script>
import { onMounted } from 'vue'
import { useRoute } from 'vue-router'

export default {
  name: "App",
  setup() {
    const route = useRoute()
    onMounted(() => {
      console.log(1, route.name) // just output undefined
      setTimeout(() => console.log(2, route.name), 1000) // 'Home'!
    })
  },
};
</script>

<style>
</style>

@TimRChen
Copy link
Author

I've resolved this. ref: https://next.router.vuejs.org/api/#hasroute

// add this snippet in you main.js or main.ts
router.isReady().then(() => {
  app.mount('#app')
})

@fashen007
Copy link

还有其他解决方案吗

@ER-Tiylor
Copy link

router.isReady().then(() => doSomeThing() )
Ref: https://vue-land.github.io/faq/accessing-the-route

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

3 participants