electron-vue-運(yùn)行cmd命令

在electron中調(diào)用cmd命令,可以通過(guò)node-cmd來(lái)實(shí)現(xiàn):
安裝node-cmd模塊

npm install node-cmd

安裝完成后,可以通過(guò)兩種方式來(lái)調(diào)用

1)將node-cmd定義為插件,通過(guò)插件進(jìn)行調(diào)用,如果應(yīng)用有多個(gè)頁(yè)面需要調(diào)用到cmd,可以通過(guò)插件方式

在renderer下新建plugins/cmd/index.js

/**
 * 自定義cmd插件
 * @author LiQun
 * @date 2019/4/14
 */
const cmdPlugin = {
  install (Vue) {
    Vue.prototype.$cmd = require('node-cmd')
  }
}

export default cmdPlugin

在main.js中添加插件

import cmd from './plugins/cmd/index'

Vue.use(cmd)

需要調(diào)用的頁(yè)面

<template>
  <div style="margin-top: 50px;">
    <button @click="testCmd">testCmd</button>
  </div>
</template>

<script>
  /**
   * 測(cè)試頁(yè)面
   * @author LiQun
   * @date 2019/3/25
   */
  export default {
    name: 'home',
    data () {
      return {}
    },
    methods: {
      testCmd () {
        this.$cmd.get(
          `
            ls
          `,
          function (err, data, stderr) {
            console.log(stderr)
            if (!err) {
              console.log('the node-cmd cloned dir contains these files :\n\n', data)
            } else {
              console.log('error', err)
            }
          }
        )
      }
    },
    computed: {},
    mounted () {
      console.log(this.$electron)
    }
  }
</script>

<style scoped lang="scss">
</style>

2)頁(yè)面直接調(diào)用,html部分相同,不再額外列舉:

<script>
  const cmd = require('node-cmd')
  /**
   * 測(cè)試頁(yè)面
   * @author LiQun
   * @date 2019/3/25
   */
  export default {
    name: 'home',
    data () {
      return {}
    },
    methods: {
      testCmd () {
        cmd.get(
          `
            ls
          `,
          function (err, data, stderr) {
            console.log(stderr)
            if (!err) {
              console.log('the node-cmd cloned dir contains these files :\n\n', data)
            } else {
              console.log('error', err)
            }
          }
        )
      }
    },
    computed: {},
    mounted () {
      console.log(this.$electron)
    }
  }
</script>

結(jié)果:


結(jié)果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容