Electron-Vue 創建跨平台桌面應用程序 UI


0. 開發准備

    安裝好 vscode 、npm / yarn,安裝方法見百度。

 

1. Electron-vue 安裝

# 如果沒有 vue-cli 的話需要全局安裝
npm install -g vue-cli
# 然后使用 vue
-cli 來安裝 electron-vue 的模板 vue init simulatedgreg/electron-vue grpc-client-electron-vue
# 安裝依賴 cd grpc-client-electron-vue yarn # or npm
install
# 進入開發模式 yarn run dev # or npm run dev

 注1:運行完命令 vue init simulatedgreg/electron-vue hello-world 后,除了 ESLint 選項按 N 鍵選 no 外,其他一路按 Enter 鍵選擇默認配置即可。如下圖所示:

(EsLint 校驗,主要用來規范開發人員的代碼。但有些像縮進、空格、空白行之類的規范,會導致在開發過程中一直報錯,選擇關閉)

 

注2:當 node 版本高於 12 的時候,會報錯:

Html Webpack Plugin:

  ReferenceError: process is not defined

解決方法:打開.electron-vue 文件夾中的 webpack.web.config.js 和 .electron-vue/webpack.renderer.config.js,

在 HtmlWebpackPlugin,添加 templateParameters,如下圖所示:

templateParameters(compilation, assets, options) {
        return {
          compilation: compilation,
          webpack: compilation.getStats().toJson(),
          webpackConfig: compilation.options,
          htmlWebpackPlugin: {
            files: assets,
            options: options
          },
          process,
        };
      },

 

注:后面的內容(2/3/4/5)如果不用 BootStrap 則忽略。

2. 將 Bootstrap 和 BootstrapVue 添加到項目中

# With yarn
yarn add bootstrap-vue bootstrap axios
# With npm
npm install bootstrap-vue bootstrap axios

 

3. 設置 BootstrapVue

接下來,讓我們設置剛剛安裝的 BootstrapVue 包。轉到 src/render/main.js 文件並將這行代碼添加到頂部:

import BootstrapVue from 'bootstrap-vue'
Vue.use(BootstrapVue)

在這里做的事情非常簡單,我們導入了 BoostrapVue 包,然后用 Vue.use() 函數在程序中注冊它,以便 Vue 程序可以識別。

我們還需要將 Bootstrap CSS 文件導入到項目中。將這段代碼段添加到 main.js 文件中:

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
在將必要的模塊導入 Vue 程序后,你的 main.js 文件應該和下面的代碼段類似:
import Vue from 'vue'
import axios from 'axios'

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

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import BootstrapVue from 'bootstrap-vue'
Vue.use(BootstrapVue)

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

/* eslint-disable no-new */
new Vue({
  components: { App },
  router,
  store,
  template: '<App/>'
}).$mount('#app')
View Code

 

4.創建包含 Bootstrap 組件的 vue 組件

下面開始創建我們的第一個組件,第一個組件是 Navbar 組件在 components 目錄下創建一個名為 Navbar.vue 的文件,並使用以下代碼更新它:

//src/components/Navbar.vue
<template>
    <div>
      <b-navbar toggleable="lg" type="dark" variant="success">
        <b-container>
            <b-navbar-brand href="#">Mealzers</b-navbar-brand>
            <b-navbar-toggle target="nav-collapse"></b-navbar-toggle>
            <b-collapse id="nav-collapse" is-nav>
              <!-- Right aligned nav items -->
              <b-navbar-nav class="ml-auto">
                <b-nav-form>
                  <b-form-input 
                    size="sm" 
                    class="mr-sm-2" 
                    placeholder="Search for a meal"
                    v-model="meal"
                    ></b-form-input>
                  <b-button 
                    size="sm" 
                    class="my-2 my-sm-0" 
                    type="submit" 
                    @click.prevent="getMeal"
                    >Search</b-button>
                </b-nav-form>
                <b-nav-item-dropdown right>
                  <!-- Using 'button-content' slot -->
                  <template slot="button-content"><em>User</em></template>
                  <b-dropdown-item href="#">Profile</b-dropdown-item>
                  <b-dropdown-item href="#">Sign Out</b-dropdown-item>
                </b-nav-item-dropdown>
              </b-navbar-nav>
            </b-collapse>
          </b-container>
      </b-navbar>
    </div>  
</template>
<script>
export default {
    data() {
        return {
            meal: ''
        }
    },
  methods: {
    getMeal() {
      
    }
  }
}    
</script>
View Code

Navbar 組件中包含幾個 BootstrapVue 組件,其中一個是 b-navbar 組件。它是 Navbar 中其他組件的父組件。如果沒有這個組件,Navbar 中的所有其他組件將無法正確呈現。

可以用 type 屬性更改 Navbar 上的文本顏色。Navbar 的 background-color 也可以用 variant 屬性來改變。這些顏色可以是任何正常的 Bootstrap 默認顏色 —— infoprimarysuccess 等。

另一個是 b-navbar-brand 組件這是可以呈現網站徽標的地方。它還包含 variant type 屬性,它們可以分別用於改變 background-color text-color

其他 BootstrapVue 組件是:

  • b-nav-form
  • b-nav-item-dropdown
  • b-dropdown-item
  • b-navbar-toggle
  • b-collapse
  • b-nav-item(可以用“disabled”屬性禁用)
  • b-navbar-nav
  • b-nav-item.
  • 更多

BootstrapVue 組件的一個美妙之處在於它們默認是響應式的。所以你無需編寫額外的代碼或用外部庫來使其實現響應式。

還有一個組件是 Card 組件。card 組件允許我們在卡中顯示圖像、文本等。它寫做 b-card 為了演示它,讓我們在 components 目錄中創建一個 Cards.vue 文件然后用下面的代碼更新其內容:

<template>
  <b-container>
    <div v-if="meals.length">
      <b-row>
        <div v-bind:key="data.index" v-for="data in meals">
          <b-col l="4">
            <b-card
              v-bind:title="data.strCategory"
              v-bind:img-src="data.strCategoryThumb"
              img-alt="Image"
              img-top
              tag="article"
              style="max-width: 20rem;"
              class="mb-2">
              <b-card-text>{{ `${data.strCategoryDescription.slice(0,100)}...` }}</b-card-text>
              <b-button href="#" variant="primary">View food</b-button>
            </b-card>
          </b-col>
        </div>
      </b-row>
    </div>
    <div v-else>
      <h5>No meals available yet 😢</h5>
    </div>
  </b-container>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      meals: []
    };
  },
  mounted() {
    axios
      .get("https://www.themealdb.com/api/json/v1/1/categories.php")
      .then(response => {
        this.meals = response.data.categories;
      })
      .catch(err => {
        console.log(err);
      });
  }
};
</script>
View Code

 

5. 渲染包含 Bootstrap 組件的 vue 組件

為了渲染之前創建的 Navbar、Cards 組件,需要修改 LandingPage.vue 文件。打開它並使用以下代碼更新:

<template>
  <div id="wrapper">
    <img id="logo" src="~@/assets/logo.png" alt="electron-vue">
    <main>
      <div class="top-side">
        <span class="title">
          Welcome to your new project!
        </span>
        <system-information></system-information>
      </div>

      <div class="bottom-side">
        <navbar/>
        <cards />
      </div>
    </main>
  </div>
</template>

<script>
  import Navbar from './Navbar.vue'
  import Cards from './Cards.vue'
  import SystemInformation from './LandingPage/SystemInformation'

  export default {
    name: 'landing-page',
    components: { SystemInformation, Cards, Navbar },
    methods: {
      open (link) {
        this.$electron.shell.openExternal(link)
      }
    }
  }
</script>

<style>
  @import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro');

  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  body { font-family: 'Source Sans Pro', sans-serif; }

  #wrapper {
    background:
      radial-gradient(
        ellipse at top left,
        rgba(255, 255, 255, 1) 40%,
        rgba(229, 229, 229, .9) 100%
      );
    height: 100vh;
    padding: 60px 80px;
    width: 100vw;
  }

  #logo {
    height: auto;
    margin-bottom: 20px;
    width: 420px;
  }

  main {
    display: flex;
    justify-content: space-between;
  }

  main > div { flex-basis: 50%; }

  .left-side {
    display: flex;
    flex-direction: column;
  }

  .welcome {
    color: #555;
    font-size: 23px;
    margin-bottom: 10px;
  }

  .title {
    color: #2c3e50;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 6px;
  }

  .title.alt {
    font-size: 18px;
    margin-bottom: 10px;
  }

  .doc p {
    color: black;
    margin-bottom: 10px;
  }

  .doc button {
    font-size: .8em;
    cursor: pointer;
    outline: none;
    padding: 0.75em 2em;
    border-radius: 2em;
    display: inline-block;
    color: #fff;
    background-color: #4fc08d;
    transition: all 0.15s ease;
    box-sizing: border-box;
    border: 1px solid #4fc08d;
  }

  .doc button.alt {
    color: #42b983;
    background-color: transparent;
  }
</style>
View Code

在這里做的是創建一個 Navbar、Cards 組件並將其嵌入到 LandingPage.vue 文件中,這時再運行 yarn run dev 命令,可以看到我們的 Demo 程序運行如下:

 

 

參考:electron-vue從入門到精通(一)

   快速上手BootstrapVue

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM