溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用 Electron-Vue 開發(fā)的桌面應(yīng)用

發(fā)布時(shí)間:2020-07-09 03:05:16 來源:網(wǎng)絡(luò) 閱讀:370 作者:sq5c9c705f23bff 欄目:web開發(fā)

這是由 Electron & Vue.js 編寫的,為程序員服務(wù)的編程工具

目前提供了四個(gè)版塊:

  • 正則表達(dá)式
  • 時(shí)間戳轉(zhuǎn)化
  • 顏色盒子
  • Json 轉(zhuǎn)化
    在這幾個(gè)模塊中,可以發(fā)現(xiàn)使用組件化的好處,處理多個(gè)組件之間各種數(shù)據(jù)變化非常方便!

使用 Electron-Vue 開發(fā)的桌面應(yīng)用

項(xiàng)目地址:

Github 地址:https://github.com/TsaiKoga/it-tools

感興趣的朋友可以關(guān)注一下,或者貢獻(xiàn)代碼;

下面介紹一下我寫 正則表達(dá)式內(nèi)容,寫的不好,望見諒...

electron-vue

克隆項(xiàng)目,從 electron-vue 克隆項(xiàng)目,然后開始編寫代碼;

https://github.com/SimulatedGREG/electron-vue.git
通過"正則表達(dá)式"這個(gè)模塊,來了解 Vue 組件通信;

electron-vue 一開始已經(jīng)為你生成一些文件頁面,我們可以按照他的方法創(chuàng)建我們自己的頁面;

創(chuàng)建路由:
src/renderer/router/index.js 文件中添加路由:

export default new Router({
  routes: [
    {
      path: '/',
      name: 'landing-page',
      component: require('@/components/LandingPage').default
    },
    {
      path: '/regex-page',
      name: 'regex-page',
      component: require('@/components/RegexPage').default
    }
]
});

這里我們的 url 為 /regex-page,并且 require 了 RegexPage 組件,這個(gè)組件要放置在 components 目錄下,所以我創(chuàng)建了文件:src/renderer/components/RegexPage.vue

編寫組件:
可以通過復(fù)制 LandingPage.vue 組件,將它改成新組件即可:

要實(shí)現(xiàn)這個(gè)頁面,頭部兩個(gè)輸入框,輸入后都能與下面的 textarea 內(nèi)容進(jìn)行比較處理,得出結(jié)論;

這個(gè)用 組件化 vue 比純粹用 js jquery 的 dom 操作要方便太多了;

通過 template 包裹寫成 vue 組件:

<template>
  <div id="regex-page">
    <div class="regex-inner" v-show="currentTab === 'Home'">
            <div class="regex-top">
                <div class="regex-top-label">
                    <label>Your regular expression:</label>
                </div>
                <div class="regex-top-fields">
                    <div class="regex-diagonal">/</div>
                    <div class="regex-diagnoal-input">
                        <input type="text" name="regex-exp" @input='execRegex' :value='regexExp' />
                    </div>

         <div class="regex-diagonal">/</div>
                    <div>
                        <input type="text" name="regex-opt" @input="execRegex" :value="regexOpt" />
                    </div>
                </div>
             </div>

      <div class="regex-bottom">
                <div class="regex-content">
                    <label>Your test string: </label>
                    <textarea class="regex-textarea" name="regex-content" @input="execRegex" :value='regexCont'></textarea>
                </div>

       <div class="result-content result-init" v-if="regexResult['status'] == 0">
                    {{ regexResult['content'] }}
                </div>

                <div class="result-content result-match" v-if="regexResult['status'] == 1">
                    <div>
                        <div class="regex-match-btn">
                            <label>Match Result:</label>
                            <a href="javascript:void(0)" class="clean-fields" @click="cleanAllFields">Clean Fields</a>
                        </div>
                        <div class="result-item">
            <span v-for="(cont, indx) in regexResult['matchedContext']" :class="indx%2 !== 0 ? 'match' : null">{{ cont }}</span>
                        </div>
                    </div>
                    <ul v-if="regexResult['content'].length > 0">
                        <label>Match Groups:</label>
                        <div class="match-groups">
                            <li v-for="(itemGroup, index) in regexResult['content']">
                                <div class="group-item">
                                  <label>Match Group {{ index + 1 }}:</label>
                                  <ul>
                                      <li v-if="i !== 0" v-for="(item, i) in itemGroup">{{ i }}: {{ item }}</li>
                                  </ul>
                                </div>
                            </li>
                        </div>
                    </ul>
                  </div>

         <div class="result-content result-not-match" v-if="regexResult['status'] == -1">
                      {{ regexResult['content'] }}
                  </div>
              </div>
          </div>
</div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  name: 'regex-page',
  computed: {
    ...mapState('Regex', {
      regexExp: state => state.regexExp,
      regexOpt: state => state.regexOpt,
      regexCont: state => state.regexCont,
      regexResult: state => state.regexResult})
  },

    methods: {
    ...mapActions('Regex', [
      'setNav',
      'cleanFields',
      'regexMatch'
    ]),
    cleanAllFields () {
      this.cleanFields()
    },
    execRegex (event) {
      this.regexMatch(event)
    },
    updateNav (title, index) {
      this.setNav({ title: title, index: index })
    }

  }

}
</script>
<style lang="scss" scoped>
 * {

  }
</style>

至于,輸入框之間的交互,我使用 vuex 來實(shí)現(xiàn)他們之間數(shù)據(jù)的傳遞;

使用 Vuex 管理狀態(tài): 一、創(chuàng)建 store 目錄,并創(chuàng)建 modules 目錄用來管理不同的命名空間的 State, Actions, Mutations 創(chuàng)建 src/renderer/store/modules/Regex.js 文件:

const state = {
  regexExp: '',
  regexOpt: '',
  regexCont: '',
  regexResult: { status: 0, content: "Here's result." }
}

const mutations = {
  REGEX_MATCH (state, target) {
    if (target.name === 'regex-exp') {
      state.regexExp = target.value
    }
    if (target.name === 'regex-opt') {
      state.regexOpt = target.value
    }
    if (target.name === 'regex-content') {
      state.regexCont = target.value
    }
    ...
}

const actions = {
  cleanFields ({ commit }) {
    commit('CLEAN_FIELDS')
  },
  regexMatch ({ commit }, payload) {
    commit('REGEX_MATCH', payload.target)
  }
}

export default {
  state,
  mutations,
  actions
}
  • state 給默認(rèn)狀態(tài);

  • mutations 更改對(duì)應(yīng) state ;

  • actions 用于寫異步來改變狀態(tài)或提交 mutations 的更改;

  • state 的方法被我寫在 computed,這樣組件中可以使用;

在 methods 方法中使用 mapActions,并定義其他方法來調(diào)用這些 action ;

二、main.js 加入 store 容器

import App from './App'
import router from './router'
import store from './store'

if (!process.env.IS_WEB) Vue.use(require('vue-electron'))
Vue.http = Vue.prototype.$http = axios
Vue.config.productionTip = false

new Vue({
  components: { App },
  router,
  store,
  template: '<App/>'
}).$mount('#app')

三、組件中通過 computed 或 data 使用 State,通過 methods 觸發(fā) Actions 方法

import { mapState, mapActions } from 'vuex'

export default {
  name: 'regex-page',
  computed: {
    ...mapState('Regex', {
      regexExp: state => state.regexExp,
      regexOpt: state => state.regexOpt,
      regexCont: state => state.regexCont,
      regexResult: state => state.regexResult})
    },

    methods: {
    ...mapActions('Regex', [
      'setNav',
      'cleanFields',
      'regexMatch'
    ]),
    cleanAllFields () {
      this.cleanFields()
    },
    execRegex (event) {
      this.regexMatch(event)
    },
    updateNav (title, index) {
      this.setNav({ title: title, index: index })
    }

  }
}

在組件文件中引用了

mapState, mapActions 方法,他可以獲取這個(gè) store 里的 state 和 action 方法,

不過要注意命名空間的使用,此處使用了 Regex 作為命名空間,所以要在 mapState 和 mapActions 中加 命名空間;

命名空間定義文件在:src/renderer/store/modules/index.js 文件;

const files = require.context('.', false, /\.js$/)
const modules = {}

files.keys().forEach(key => {
  if (key === './index.js') return
  modules[key.replace(/(\.\/|\.js)/g, '')] = files(key).default
  modules[key.replace(/(\.\/|\.js)/g, '')]['namespaced'] = true
})

export default modules

但是直接 (‘ Regex ’, [regexExp: state => state.regexExp]) 是無法使用的,必須在 module 中聲明 namespaced: true 才可以;

… mapActions() 是將里面的對(duì)象 扁平化 到 外面的對(duì)象中;

直接 mapActions 只是打開了方法,還未執(zhí)行:

刪除 createSharedMutations() 的方法后,action 生效;

綁定到組件上

<input type="text" name="regex-exp" @input='execRegex' value='regexExp' />

生成桌面應(yīng)用

運(yùn)行命令:

npm run build:mas # 生成 mac 應(yīng)用
npm run build:linux # 生成 linux 應(yīng)用
npm run build:win32 # 生成 windows 應(yīng)用

可以在 /build 目錄中看到生成的應(yīng)用目錄

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI