Skip to content

Commit f6db035

Browse files
authored
Merge pull request #52 from xianshenglu:feat_vue3
chore(vue@3): migrate vue2 script to vue3 script setup
2 parents 56fb750 + 715dcbf commit f6db035

31 files changed

+911
-1158
lines changed

.vscode/settings.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

demo/vue@3/jsconfig.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"@/*": ["src/*"]
6+
},
7+
"target": "ESNext",
8+
"module": "ESNext",
9+
"moduleResolution": "Node",
10+
"strict": true,
11+
"jsx": "preserve",
12+
"sourceMap": true,
13+
"resolveJsonModule": true,
14+
"isolatedModules": true,
15+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
16+
"skipLibCheck": true
17+
},
18+
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.vue"],
19+
"exclude": ["node_modules", "dist"]
20+
}

demo/vue@3/src/App.vue

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,29 @@
1717
/>
1818
</template>
1919

20-
<script>
21-
import { defineComponent } from 'vue';
20+
<script setup>
21+
import { ref, onMounted, computed } from 'vue'
22+
import { useStore } from 'vuex'
2223
2324
import AppHeader from '@/components/AppHeader'
24-
import AppNav from './components/AppNav'
2525
import AppLoading from './components/AppLoading'
2626
import PlayerMed from './views/player/PlayerMed'
27-
import { mapState, mapMutations } from 'vuex'
2827
29-
export default defineComponent({
30-
name: 'App',
28+
const audioEl = ref(null)
29+
const store = useStore()
3130
32-
components: {
33-
AppLoading,
34-
AppHeader,
35-
AppNav,
36-
PlayerMed
37-
},
31+
const isPlayerMedShow = computed(() => store.state.player.isPlayerMedShow)
32+
const isPlayerMedSmall = computed(() => store.state.player.isPlayerMedSmall)
33+
const song = computed(() => store.state.player.song)
34+
const vh = computed(() => store.state.device.vh)
3835
39-
mounted() {
40-
document.documentElement.style.setProperty('--vh', this.vh / 100 + 'px')
41-
this.findAudioEl(this.$refs.audioEl)
42-
},
36+
const findAudioEl = (el) => store.commit('player/findAudioEl', el)
37+
const togglePlay = (status) => store.commit('player/togglePlay', status)
4338
44-
computed: {
45-
...mapState('player', {
46-
isPlayerMedShow: 'isPlayerMedShow',
47-
isPlayerMedSmall: 'isPlayerMedSmall',
48-
music: 'music',
49-
song: 'song'
50-
}),
51-
...mapState('device', {
52-
vh: 'vh'
53-
})
54-
},
55-
56-
methods: {
57-
...mapMutations('player', {
58-
findAudioEl: 'findAudioEl',
59-
togglePlay: 'togglePlay'
60-
})
61-
},
62-
});
39+
onMounted(() => {
40+
document.documentElement.style.setProperty('--vh', vh.value / 100 + 'px')
41+
findAudioEl(audioEl.value)
42+
})
6343
</script>
6444

6545
<style lang="less" scoped>

demo/vue@3/src/components/AppHeader.vue

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,23 @@
2020
</header>
2121
</template>
2222

23-
<script>
24-
import { defineComponent } from 'vue';
25-
26-
import { mapState, mapMutations } from 'vuex'
23+
<script setup>
2724
import bus from '@/eventBus'
28-
export default defineComponent({
29-
name: 'AppHeader',
25+
import { computed } from 'vue';
26+
import { useStore } from 'vuex';
3027
31-
data() {
32-
return {
33-
bus: bus
34-
}
35-
},
28+
const store = useStore();
29+
const logo__text = computed(() => store.state.images.logo__text);
30+
const curPlayerId = computed(() => store.state.player.curPlayerId);
3631
37-
computed: {
38-
...mapState('images', ['logo__text']),
39-
...mapState('player', ['curPlayerId'])
40-
},
32+
function goBack() {
33+
if (curPlayerId.value === 1) {
34+
store.commit('player/togglePlayers', 1);
35+
return;
36+
}
37+
history.go(-1);
38+
}
4139
42-
methods: {
43-
...mapMutations('player', ['togglePlayers']),
44-
goBack() {
45-
if (this.curPlayerId === 1) {
46-
this.togglePlayers(1)
47-
return
48-
}
49-
history.go(-1)
50-
}
51-
},
52-
});
5340
</script>
5441

5542
<style lang="less" scoped>

demo/vue@3/src/components/AppLoading.vue

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66
</div>
77
</template>
88

9-
<script>
10-
import { defineComponent } from 'vue';
9+
<script setup>
10+
import { computed } from 'vue';
11+
import { useStore } from 'vuex';
1112
12-
import { mapState } from 'vuex'
13-
export default defineComponent({
14-
name: 'AppLoading',
13+
const store = useStore();
1514
16-
computed: {
17-
...mapState('loading', ['isShow', 'sizeClassName']),
18-
className() {
19-
let className = this.isShow ? 'loading' : 'loading loading--fade_out'
20-
return className + ' ' + this.sizeClassName
21-
}
22-
},
15+
const isShow = computed(() => store.state.loading.isShow);
16+
const sizeClassName = computed(() => store.state.loading.sizeClassName);
17+
18+
const className = computed(() => {
19+
let name = isShow.value ? 'loading' : 'loading loading--fade_out';
20+
return name + ' ' + sizeClassName.value;
2321
});
2422
</script>
2523

demo/vue@3/src/components/AppMusicList.vue

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,24 @@
1414
</ul>
1515
</template>
1616

17-
<script>
18-
import { defineComponent } from 'vue';
17+
<script setup>
18+
import { useStore } from 'vuex';
1919
20-
export default defineComponent({
21-
name: 'AppMusicList',
20+
const props = defineProps({
21+
musicList: {
22+
type: Array,
23+
default: () => []
24+
}
25+
});
2226
23-
props: {
24-
musicList: {
25-
type: Array,
26-
default() {
27-
return []
28-
}
29-
}
30-
},
27+
const store = useStore();
3128
32-
methods: {
33-
wantPlay(music) {
34-
this.$store.commit('player/wantPlay', {
35-
music,
36-
musicList: this.$props.musicList
37-
})
38-
}
39-
},
40-
});
29+
const wantPlay = (music) => {
30+
store.commit('player/wantPlay', {
31+
music,
32+
musicList: props.musicList
33+
});
34+
};
4135
</script>
4236

4337
<style lang="less" scoped>

demo/vue@3/src/components/AppNav.vue

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,37 @@
1313
</section>
1414
</template>
1515

16-
<script>
17-
import { defineComponent } from 'vue';
16+
<script setup>
17+
import { ref, computed } from 'vue';
18+
import { useRoute } from 'vue-router';
1819
19-
export default defineComponent({
20-
name: 'AppNav',
20+
const route = useRoute();
2121
22-
data() {
23-
return {
24-
navs: [
25-
{
26-
text: '新歌',
27-
name: 'new',
28-
path: '/'
29-
},
30-
{
31-
text: '排行',
32-
name: 'rank',
33-
path: '/rank/list'
34-
},
35-
{
36-
text: '歌单',
37-
name: 'song',
38-
path: '/song/list'
39-
},
40-
{
41-
text: '歌手',
42-
name: 'singer',
43-
path: '/singer/category'
44-
}
45-
]
46-
}
22+
const navs = ref([
23+
{
24+
text: '新歌',
25+
name: 'new',
26+
path: '/'
4727
},
48-
49-
computed: {
50-
activeIndex() {
51-
return this.navs.findIndex(nav => nav.path === this.$route.path)
52-
}
28+
{
29+
text: '排行',
30+
name: 'rank',
31+
path: '/rank/list'
32+
},
33+
{
34+
text: '歌单',
35+
name: 'song',
36+
path: '/song/list'
5337
},
38+
{
39+
text: '歌手',
40+
name: 'singer',
41+
path: '/singer/category'
42+
}
43+
]);
44+
45+
const activeIndex = computed(() => {
46+
return navs.value.findIndex(nav => nav.path === route.path);
5447
});
5548
</script>
5649

demo/vue@3/src/components/Main.vue

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,8 @@
55
</main>
66
</template>
77

8-
<script>
9-
import { defineComponent } from 'vue';
10-
8+
<script setup>
119
import AppNav from '@/components/AppNav'
12-
export default defineComponent({
13-
name: 'Main',
14-
15-
components: {
16-
AppNav
17-
},
18-
19-
methods: {},
20-
});
2110
</script>
2211

2312
<style lang="less" scoped>

demo/vue@3/src/components/PubModuleDes.vue

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,17 @@
1616
</div>
1717
</template>
1818

19-
<script>
20-
import { defineComponent } from 'vue';
21-
22-
export default defineComponent({
23-
name: 'PubModuleDes',
24-
25-
props: {
26-
description: {
27-
type: String,
28-
default() {
29-
return ''
30-
}
31-
}
32-
},
33-
34-
data() {
35-
return {
36-
showMore: false
37-
}
38-
},
39-
40-
methods: {},
19+
<script setup>
20+
import { ref } from 'vue';
21+
22+
const props = defineProps({
23+
description: {
24+
type: String,
25+
default: ''
26+
}
4127
});
28+
29+
const showMore = ref(false);
4230
</script>
4331

4432
<style scoped lang="less">

demo/vue@3/src/components/PubModuleHead.vue

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

11-
<script>
12-
import { defineComponent } from 'vue';
13-
14-
export default defineComponent({
15-
name: 'PubModuleHead',
16-
17-
props: {
18-
moduleHeadInfo: {
19-
type: Object,
20-
default() {
21-
return {}
22-
}
23-
}
24-
},
11+
<script setup>
12+
const props = defineProps({
13+
moduleHeadInfo: {
14+
type: Object,
15+
default: () => ({})
16+
}
2517
});
2618
</script>
2719

0 commit comments

Comments
 (0)