使用 electron-vue 建立桌面應用程式

環境

  • macOS

安裝套件

1
2
npm install -g vue-cli
vue init simulatedgreg/electron-vue electron-vue

新增路由

修改 src\renderer\router\index.js 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
routes: [
{
path: '/',
name: 'landing-page',
component: require('@/components/LandingPage').default
},
{
path: '/slide',
name: 'slide',
component: () => import('@/components/Slide')
}
]
})

新增狀態管理

新增 src/renderer/store/modules/Slide.js 狀態管理。

1
2
3
4
5
6
7
const state = {
api_url: 'http://www.splashbase.co/api/v1/images/random'
}

export default {
state
}

新增元件

新增 src/renderer/components/Slide.vue 元件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<template>
<div class="container">
<Transition
name="fade"
enter-active-class="animated fadeIn"
leave-active-class="animated fadeOut"
>
<img
v-show="loaded"
:src="img_url"
class="image center"
@load="isLoaded"
>
</Transition>
<div v-show="!loaded">
<img
class="center"
src="../assets/loading.gif"
>
</div>
</div>
</template>

<script>
export default {
data () {
return {
img_url: '',
loaded: false
}
},
created () {
this.fetch()
},
methods: {
fetch () {
setInterval(function () {
this.$http.get(this.$store.state.Slice.api_url)
.then(({ data }) => {
this.img_url = data.url
})
}.bind(this), 3000)
},
isLoaded () {
this.loaded = true
}
}
}
</script>

<style lang="scss">
@import url('https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.css');
.container {
width: 100%;
height: 100%;
}
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.image {
animation-duration: 0.25s;
height: 100vh;
}
</style>

生成執行檔

1
npm run build

程式碼