|
| 1 | +import Vue, { defineComponent, watch, ref, onUnmounted } from 'vue' |
| 2 | +import VueRouter from 'vue-router' |
| 3 | +import { useRoute, useRouter } from 'vue-router/composables' |
| 4 | + |
| 5 | +Vue.use(VueRouter) |
| 6 | + |
| 7 | +const Home = defineComponent({ |
| 8 | + setup () { |
| 9 | + const route = useRoute() |
| 10 | + const router = useRouter() |
| 11 | + |
| 12 | + // should be / |
| 13 | + const startRoute = route.fullPath |
| 14 | + |
| 15 | + console.log('got to Home', startRoute) |
| 16 | + |
| 17 | + const watchCount = ref(0) |
| 18 | + |
| 19 | + watch(() => route.query.n, () => { |
| 20 | + console.log('watched') |
| 21 | + watchCount.value++ |
| 22 | + }) |
| 23 | + |
| 24 | + onUnmounted(() => { |
| 25 | + console.log('unmounted') |
| 26 | + }) |
| 27 | + |
| 28 | + function navigate () { |
| 29 | + router.push({ query: { n: 1 + (Number(route.query.n) || 0) }}) |
| 30 | + } |
| 31 | + return { route, navigate, watchCount, startRoute } |
| 32 | + }, |
| 33 | + template: ` |
| 34 | +<div> |
| 35 | + <h2>Home</h2> |
| 36 | + <p id="start-route">{{ startRoute }}</p> |
| 37 | + <p id='watch-count'>{{ watchCount }}</p> |
| 38 | + <p id="fullpath">{{ route.fullPath }}</p> |
| 39 | + <button id="nav" @click="navigate">Navigate</button> |
| 40 | +</div> |
| 41 | + ` |
| 42 | +}) |
| 43 | + |
| 44 | +const About = defineComponent({ |
| 45 | + setup () { |
| 46 | + const route = useRoute() |
| 47 | + return { route } |
| 48 | + }, |
| 49 | + template: ` |
| 50 | +<div> |
| 51 | + <h2>About</h2> |
| 52 | + <p id="fullpath">{{ route.fullPath }}</p> |
| 53 | +</div> |
| 54 | + ` |
| 55 | +}) |
| 56 | + |
| 57 | +const router = new VueRouter({ |
| 58 | + mode: 'history', |
| 59 | + base: __dirname, |
| 60 | + routes: [ |
| 61 | + { path: '/', component: Home }, |
| 62 | + { path: '/about', component: About } |
| 63 | + ] |
| 64 | +}) |
| 65 | + |
| 66 | +new Vue({ |
| 67 | + router, |
| 68 | + template: ` |
| 69 | + <div id="app"> |
| 70 | + <h1>Basic</h1> |
| 71 | + <ul> |
| 72 | + <li><router-link to="/">/</router-link></li> |
| 73 | + <li><router-link to="/about">/foo</router-link></li> |
| 74 | + </ul> |
| 75 | + <router-view class="view"></router-view> |
| 76 | + </div> |
| 77 | + ` |
| 78 | +}).$mount('#app') |
0 commit comments