Skip to content

Commit 04d35eb

Browse files
committed
chore(playground): upgrade to script setup syntax and TypeScript
Upgrade the playground package to use the `<script setup>` syntax and TypeScript. This change makes the playground more consistent with recent Vue.js practices and improves type safety. Keep the purpose of each example identical to the original examples as much as possible. Therefore, components using `beforeRouteEnter()`, [which does not have a `<script setup>` equivalent](#1517), are not converted to `<script setup>` syntax but are only converted to use TypeScript for improved type safety (Home.vue and ComponentWithData.vue). Converting these to use the [recommended data loaders or `defineMacro()`](#1517 (comment)) could be an option in the future. Update `RouteNamedMap` in main.ts to improve type safety and better demonstrate the newer typed routes feature (introduced in [email protected]) in LongView.vue, Nested.vue, and RepeatedParams.vue. Fix type issue in AppLink.vue where `RouterLinkProps` replaces `RouterLink.props`. Remove the `name` option from the `defineComponent` calls that used it, because this option is [only useful when the component recursively calls itself](https://v2.vuejs.org/v2/api/#name), but there are not components in this playground project that do so.
1 parent 707608e commit 04d35eb

13 files changed

+98
-143
lines changed

packages/playground/src/AppLink.vue

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:class="classes"
77
target="_blank"
88
rel="noopener noreferrer"
9-
:href="to"
9+
:href="String(to)"
1010
:tabindex="disabled ? -1 : undefined"
1111
:aria-disabled="disabled"
1212
>
@@ -26,38 +26,30 @@
2626
</a>
2727
</template>
2828

29-
<script>
30-
import { RouterLink, START_LOCATION, useLink, useRoute } from 'vue-router'
31-
import { computed, defineComponent, toRefs } from 'vue'
29+
<script setup lang="ts">
30+
import { START_LOCATION, useLink, useRoute } from 'vue-router'
31+
import { computed, useAttrs, } from 'vue'
32+
import type { RouterLinkProps } from 'vue-router'
3233
33-
export default defineComponent({
34-
props: {
35-
...RouterLink.props,
36-
disabled: Boolean,
37-
},
34+
const { replace, to, disabled } = defineProps<RouterLinkProps & {disabled?: boolean}>()
35+
const attrs = useAttrs()
3836
39-
setup(props, { attrs }) {
40-
const { replace, to, disabled } = toRefs(props)
41-
const isExternalLink = computed(
42-
() => typeof to.value === 'string' && to.value.startsWith('http')
43-
)
37+
const isExternalLink = computed(
38+
() => typeof to === 'string' && to.startsWith('http')
39+
)
4440
45-
const currentRoute = useRoute()
41+
const currentRoute = useRoute()
4642
47-
const { route, href, isActive, isExactActive, navigate } = useLink({
48-
to: computed(() => (isExternalLink.value ? START_LOCATION : to.value)),
49-
replace,
50-
})
51-
52-
const classes = computed(() => ({
53-
// allow link to be active for unrelated routes
54-
'router-link-active':
55-
isActive.value || currentRoute.path.startsWith(route.value.path),
56-
'router-link-exact-active':
57-
isExactActive.value || currentRoute.path === route.value.path,
58-
}))
59-
60-
return { attrs, isExternalLink, href, navigate, classes, disabled }
61-
},
43+
const { route, href, isActive, isExactActive, navigate } = useLink({
44+
to: computed(() => (isExternalLink.value ? START_LOCATION : to)),
45+
replace,
6246
})
47+
48+
const classes = computed(() => ({
49+
// allow link to be active for unrelated routes
50+
'router-link-active':
51+
isActive.value || currentRoute.path.startsWith(route.value.path),
52+
'router-link-exact-active':
53+
isExactActive.value || currentRoute.path === route.value.path,
54+
}))
6355
</script>

packages/playground/src/main.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ export interface RouteNamedMap {
4545
{ path: ParamValue<true> },
4646
{ path: ParamValue<false> }
4747
>
48+
long: RouteRecordInfo<
49+
'long',
50+
'/long-:n(\\d+)',
51+
{ n: ParamValue<true> },
52+
{ n: ParamValue<false> }
53+
>
54+
Nested: RouteRecordInfo<
55+
'Nested',
56+
'/nested',
57+
Record<never, never>,
58+
Record<never, never>
59+
>
60+
'absolute-child': RouteRecordInfo<
61+
'absolute-child',
62+
'/nested/also-as-absolute',
63+
Record<never, never>,
64+
Record<never, never>
65+
>
66+
repeat: RouteRecordInfo<
67+
'repeat',
68+
'/rep/:a*',
69+
{ a: ParamValue<true> },
70+
{ a: ParamValue<false> }
71+
>
4872
}
4973

5074
declare module 'vue-router' {

packages/playground/src/views/ComponentWithData.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
</div>
66
</template>
77

8-
<script>
8+
<script lang="ts">
99
import { defineComponent, toRefs, reactive } from 'vue'
1010
import { getData, delay } from '../api'
1111
import { onBeforeRouteUpdate } from 'vue-router'
1212
13-
export default defineComponent({
13+
const ComponentWithData = defineComponent({
1414
name: 'ComponentWithData',
1515
async setup() {
16-
const data = reactive({ other: 'old', fromApi: null })
16+
const data = reactive<{ other: string, fromApi: null | {message: string, time: number }}>({ other: 'old', fromApi: null })
1717
1818
onBeforeRouteUpdate(async (to, from, next) => {
1919
data.fromApi = await getData()
@@ -30,9 +30,12 @@ export default defineComponent({
3030
console.log('this in beforeRouteEnter', this)
3131
await delay(300)
3232
next(vm => {
33-
console.log('got vm', vm)
34-
vm.other = 'Hola'
33+
console.log('got vm', vm);
34+
// Workaround for https://github.com/vuejs/router/issues/701
35+
(vm as InstanceType<typeof ComponentWithData>).other = 'Hola'
3536
})
3637
},
3738
})
39+
40+
export default ComponentWithData
3841
</script>

packages/playground/src/views/Dynamic.vue

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,4 @@
22
<div>This was added dynamically</div>
33
</template>
44

5-
<script>
6-
import { defineComponent } from 'vue'
7-
8-
export default defineComponent({
9-
name: 'Dynamic',
10-
})
11-
</script>
5+
<script setup lang="ts"></script>

packages/playground/src/views/Generic.vue

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,4 @@
55
</section>
66
</template>
77

8-
<script>
9-
import { defineComponent } from 'vue'
10-
11-
export default defineComponent({
12-
name: 'Generic',
13-
})
14-
</script>
8+
<script setup lang="ts"></script>

packages/playground/src/views/GuardedWithLeave.vue

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,18 @@
55
</div>
66
</template>
77

8-
<script>
9-
// @ts-check
10-
import { defineComponent } from 'vue'
8+
<script setup lang="ts">
9+
import { ref } from 'vue'
1110
import { onBeforeRouteLeave } from 'vue-router'
1211
13-
export default defineComponent({
14-
name: 'GuardedWithLeave',
15-
data: () => ({ tries: 0 }),
12+
const tries = ref(0)
1613
17-
setup() {
18-
console.log('setup in cant leave')
19-
onBeforeRouteLeave(function (to, from, next) {
20-
if (window.confirm('Do you really want to leave?')) next()
21-
else {
22-
// @ts-ignore
23-
this.tries++
24-
next(false)
25-
}
26-
})
27-
return {}
28-
},
14+
console.log('setup in cant leave')
15+
onBeforeRouteLeave((to, from, next) => {
16+
if (window.confirm('Do you really want to leave?')) next()
17+
else {
18+
tries.value++
19+
next(false)
20+
}
2921
})
3022
</script>

packages/playground/src/views/Home.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</div>
99
</template>
1010

11-
<script>
11+
<script lang="ts">
1212
import { defineComponent, getCurrentInstance, ref } from 'vue'
1313
1414
export default defineComponent({
@@ -22,7 +22,7 @@ export default defineComponent({
2222
setup() {
2323
const me = getCurrentInstance()
2424
25-
function log(value) {
25+
function log(value: any) {
2626
console.log(value)
2727
return value
2828
}

packages/playground/src/views/LongView.vue

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,8 @@
1313
</section>
1414
</template>
1515

16-
<script>
17-
import { defineComponent } from 'vue'
16+
<script setup lang="ts">
1817
import { useRoute } from 'vue-router'
1918
20-
export default defineComponent({
21-
name: 'LongView',
22-
setup() {
23-
const route = useRoute()
24-
return { route }
25-
},
26-
})
19+
const route = useRoute<'long'>()
2720
</script>

packages/playground/src/views/Nested.vue

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,9 @@
6060
</div>
6161
</template>
6262

63-
<script>
64-
import { defineComponent, inject, provide } from 'vue'
63+
<script setup lang="ts">
64+
import { inject, provide } from 'vue'
6565
66-
export default defineComponent({
67-
name: 'Nested',
68-
setup() {
69-
const level = inject('level', 1)
70-
provide('level', level + 1)
71-
72-
return {
73-
level,
74-
}
75-
},
76-
})
66+
const level = inject('level', 1)
67+
provide('level', level + 1)
7768
</script>

packages/playground/src/views/NestedWithId.vue

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,11 @@
1717
</div>
1818
</template>
1919

20-
<script>
20+
<script setup lang="ts">
2121
import { defineComponent, inject, provide } from 'vue'
2222
23-
export default defineComponent({
24-
props: ['id'],
25-
name: 'NestedWithId',
26-
setup(props) {
27-
const level = inject('level', 1)
28-
provide('level', level + 1)
23+
const props = defineProps<{id: string}>()
2924
30-
return {
31-
props,
32-
level,
33-
}
34-
},
35-
})
25+
const level = inject('level', 1)
26+
provide('level', level + 1)
3627
</script>

packages/playground/src/views/NotFound.vue

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@
22
<div>Not Found: {{ route.fullPath }}</div>
33
</template>
44

5-
<script>
6-
import { defineComponent } from 'vue'
5+
<script setup lang="ts">
76
import { useRoute } from 'vue-router'
87
9-
export default defineComponent({
10-
name: 'NotFound',
11-
setup() {
12-
const route = useRoute()
13-
return { route }
14-
},
15-
})
8+
const route = useRoute()
169
</script>

packages/playground/src/views/RepeatedParams.vue

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,24 @@
99
</div>
1010
</template>
1111

12-
<script>
13-
import { defineComponent, computed } from 'vue'
12+
<script setup lang="ts">
13+
import { computed } from 'vue'
1414
import { useRoute } from 'vue-router'
1515
16-
export default defineComponent({
17-
name: 'RepeatedParams',
16+
const route = useRoute<'repeat'>()
1817
19-
setup() {
20-
const route = useRoute()
18+
const lessNesting = computed(() => {
19+
const a = [...(route.params.a || [])]
20+
a.pop()
2121
22-
const lessNesting = computed(() => {
23-
const a = [...(route.params.a || [])]
24-
a.pop()
25-
26-
return { params: { a } }
27-
})
28-
29-
const moreNesting = computed(() => {
30-
const a = [...(route.params.a || [])]
31-
a.push('more')
22+
return { params: { a } }
23+
})
3224
33-
return { params: { a } }
34-
})
25+
const moreNesting = computed(() => {
26+
const a = [...(route.params.a || [])]
27+
a.push('more')
3528
36-
return { lessNesting, moreNesting }
37-
},
29+
return { params: { a } }
3830
})
31+
3932
</script>

packages/playground/src/views/User.vue

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@
22
<div>User: {{ id }}</div>
33
</template>
44

5-
<script>
6-
import { defineComponent } from 'vue'
5+
<script setup lang="ts">
6+
import { onBeforeRouteUpdate } from 'vue-router'
77
8-
export default defineComponent({
9-
name: 'User',
10-
props: {
11-
id: String,
12-
},
8+
defineProps<{ id: string }>()
139
14-
beforeRouteUpdate(to, from, next) {
15-
console.log('in beforeRouteUpdate this', this)
10+
onBeforeRouteUpdate((to, from, next) => {
11+
console.log('in beforeRouteUpdate this', this)
1612
next(vm => {
1713
console.log('in next callback', vm)
1814
})
19-
},
2015
})
2116
</script>

0 commit comments

Comments
 (0)