2020年9月15日 星期二

在 Vue 應用 SweetAlert

      要做常見的點擊後跳出 moadl 的視窗提醒,用 alert 做關鍵字搜尋發現這個好看的套件, 
 先 npm 安裝 

 npm install -S vue-sweetalert2 

 在 main.js 作 import 

 import VueSweetalert2 from 'vue-sweetalert2'; Vue.use(VueSweetalert2); 

<template>
<button click="showAlert">Hello world</button>
</template>

<script>
export default {
  methods: {
    showAlert() {
      // Use sweetalert2
      this.$swal('Hello Vue world!!!');
    },
  },
};
</script> 

參考資料範例:

  @click 指向方法,然後在方法中簡單一行就可以使用 Sweetalert了,接下來就是更改

預設樣式, 作一些修改。 


 目前作品作的部分:

 addCart (id, quantity = 1) { const api = 

`${process.env.VUE_APP_APIPATH}api/${process.env.VUE_APP_UUID}/ec/shopping` 

 const cart = { product: id, quantity } this.$http.post(api, cart) .then((res) => { this.getCart() 

this.$swal({ icon: 'success', title: '加入購物車成功!', text: '可以繼續選購其他商品', button: 'OK' }) }) 

.catch((error) => {
 
this.$swal({ icon: 'error', title: '加入購物車失敗!', text: '請重新選購', button: 'OK' }) }) }, 


可以看到除了簡單輸入文字外,簡單的在{}內就可以設置icon、title、text

(彈出視窗中間的主要內容)、button(下面的按鈕)。 Sweetalert 內建 icon 有四種樣子,

分別是  "error"  叉叉、 "success"  打勾、 "info" 跟 "warning"  我沒用到,四種常見的 icon , 

除了簡單的樣式之外還可以加入星等評分,甚至一些更複雜的 JS 功能,不過有寫到再研究,

簡單的寥寥幾行就可以有一個漂亮的警示視窗已經很滿意了。 

 參考資料: 

https://sweetalert.js.org/guides/ 

https://www.npmjs.com/package/vue-sweetalert2

2020年9月6日 星期日

製作表單圓角框 input 前方帶 icon 的方法

 成品圖:




之前一直以為要用定位(position)的方法才能完成,直到今天在六角 slack 群看到回覆才學到

原來如此簡單。


首先基本的 HTML ,其中 all.css 是 FontAwesome 的資料夾裡面的 all.css , style 則是

自訂等等放其他設定的 CSS 檔案。


基礎表單設定,外面加個框避免太大太奇怪而已,一般的輸入框也就300-500px足以,<form>、

<button>、<input>標籤是表單的基礎內容。


placeholder="icon" 裡面是框框底下的提示字,一般就是請輸入XX或輸入N-M位數字之類的,

class 因為是簡單的例子就照位置隨便取。


<i class="fab fa-accusoft"></i> 是FontAwesome的圖案。


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/all.css">
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="formWidth">
  <form class="form">
    <button type="button" class="button"><i class="fab fa-accusoft"></i></button>
    <input placeholder="icon" type="text" class="input"
  </form>
</div>
</body>
</html>

.formWidth{
    width: 300px;
}
.form{
    display: flex;
    margin: 0 10px 50px;
    border-radius: 100px;
    border: 1px solid #000;
}
.button{
    width:15%;
    border: none;
    background-color: transparent;
    padding: 5px 10px;
}

.input{
   width:85%;
   border: none;
   padding: 5px 10px;
   background-color: transparent;
}
input:focus,
button:focus {
    outline: none;
  }


一、 display:flex將上下排列的<input>跟<button>排成橫線。

如果還是兩行先做完兩步再檢討


二、 調整比例 width 跟 padding 、 magin。


三、 設定圓角跟框線:

border-radius: 100px;
    border: 1px solid #000;

四、 將<input>跟<button>背景設成透明:

因為框線是由最外層產生的,因此裡面的兩個背景會吃掉部分框線,所以要設成透明避免蓋住框線。

五、 :focus效果:

focus 效果是滑鼠點一下之後會在外圍有一個框框跟一根的指示條告訴使用者現在正在輸入或點擊的

欄位,但在此因為是外層是圓的,內圈一點之下沒改過方方的外型就露餡了,所以要改成 none。







參考資料:https://hsuchihting.github.io/css/20200723/3601848445/

2020年9月5日 星期六

上傳 Vue 網站到 github pages 時將 dist 資料夾上傳的方法

        自從VSCode自動幫我上傳到 github pages 之後,好一段時間都專注在自己的開發沒注意到

上傳運作的問題。


在 .gitignore 檔案中,會排除掉上傳的檔案,vue一開始會幫你把 node_modules 加入排除名單,


        因為是放不一定有用到的套件跟一些有的沒有的地方,如果整個上傳很佔空間,但是...它

竟然把編譯出來的dist 資料夾也加進了排除名單,因此要上傳編譯好的 dist 資料夾就要把它註解

掉。


.DS_Store

node_modules

# /dist


然後就可以順利上傳了,並且將首頁路由指定到 /dist 就能正常讀取 Vue 開發的網站了。




參考資料:https://gitbook.tw/chapters/using-git/ignore.html

2020年9月4日 星期五

陣列的再處理,以 Vue filter為例

<template>
  <div class="container">
<button type="button" @click="newarrayproduct">test</button>
 </div>
</template>

先做一個按鈕觸發條件作為測試用。

<script>
export default {
  data () {
    return {   
      products: [],
      newproduct:[],  
    };
  },
   created () {
    this.getProducts()
  },
  methods: {
    newarrayproduct(){
      this.newproduct = this.products;
      const salad = this.newproduct.filter(function(item, index, array){
        return item.category == '沙拉';
      });
      this.newproduct = salad;
      console.log(this.newproduct);
    },
  }
</script>


created 是起始從 API 先抓取要處理的陣列,在此非重點所以省去函式內容。

data 中應先定義要處理的資料內容,還沒載入實質內容所以用空的就好。


今天的故事是:餐廳裡有很多諸如前菜、主餐、套餐、甜點...等,在頁面呈現的時候如果只依序會很雜亂,並無法讓顧客找到最心儀的餐點,因此以沙拉為例如何用 JS 方法將"所有沙拉"從"所有餐點"中取出來。

一、methods 內寫要處理的函式,首先先複製原本產品內容到新的陣列上。


         this.newproduct = this.products;


二、定義一個變數

        const salad 


三、指向要處理的陣列

        this.newproduct


四、利用 filter 抓出資料內建的 category , category 內有沙拉、湯、主餐...等等分類

        .filter(function(item, index, array){
        return item.category == '沙拉';
      });

    經過處理之後 salad 內容就是所有沙拉形成的陣列。


五、要處理的陣列賦予結果的值

        this.newproduct = salad;

        這樣 newproduct 陣列裡就會有所有沙拉分類的菜,同理也可以處理金額 > x ...等等

        常見的分類方式,只要改 return 後面就好。


參考資料:

https://wcc723.github.io/javascript/2017/06/29/es6-native-array/

https://cn.vuejs.org/v2/guide/list.html


2020年9月3日 星期四

VeeValidate 表單驗證 與 BootstrapVue 框架的結合

     我是做到卡住看下面的參考資料學會的,但前半段 npm 是自己上兩邊官網 import 跟 use

首先是 npm 部分, VSCode 按 Ctrl+` 打開終端機輸入:

  npm install vee-validate --save

  npm install vue bootstrap-vue bootstrap

再來是在main.js import

  import Vue from 'vue'

  import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'

Vue.use(BootstrapVue)

Vue.use(IconsPlugin)

CSS部份我採用自訂的SCSS,所以在 APP.VUE

@import '/assets/css/all.scss';

  //下面是google字型

@import url('https://fonts.googleapis.com/css2?family=Coiny&family=Noto+Sans+TC:wght@100;400;900&display=swap');  

VeeValidate官方推薦採用自訂規則只導入用到的規則,不過第一次用就把預設的規則都載入

import { ValidationObserver, ValidationProvider, extend } from 'vee-validate';

import * as rules from 'vee-validate/dist/rules';

  

// 用這個迴圈載入所有的規則

Object.keys(rules).forEach(rule => {

  extend(rule, rules[rule]);

});


Vue.component('ValidationObserver', ValidationObserver);

Vue.component('ValidationProvider', ValidationProvider);

  

Vue.config.productionTip = false;

 元件裝好之後接下來就是重頭戲的表單設置部分:


slot-scope="{ validate }" 要放在 <b-form>

slot-scope="{ valid, errors}"放在 <b-form-group> , label可以省一行跟設定基於 Bootstrap 的格線

系統,在需要驗證的 input 加上 :state=" errors[0] ?  false : ( valid?true:null ) "

<b-form-invalid-feedback> 是驗證失敗時出現的文字, errors 的內容要在 node_modules\vee-

validate\dist\locale\zh_TW.json 做修改,改哪個 json 檔案由想使用的語言決定,更改語系請看

VeeValidate 官網,此處篇幅有限暫且擱下。

 

ValidationObserver ,是 VeeValidate 的特化表單功能,包在整個 <b-form> 外面,可以針對送出

表單按鈕做控制跟驗證或二次驗證...等等

ValidationProvider,放在 <b-form-group> 外面就好,會產生一個 <span> ,亂丟會導致畫面不見。



後半截的 Code 主要就是關一關,還有送出表單驗證功能與串接API的結合寫法。


頁面的部份如上,下面是 <script> 內容:








對於表單驗證的部分完全不用寫多餘的方法,下面參考資料步驟詳細標示清楚,我照著做才沒冒一堆錯誤,不然對這個套件的原理還有很多不熟的地方。

參考資料:https://www.positronx.io/vue-js-veevalidate-bootstrapvue-form-validation-example/

2020年9月1日 星期二

BootstrapVue 用法,Form select 跟 Form spinbutton

替自己有用到的地方做個紀錄。


    首先先導入,參考官網連結 https://bootstrap-vue.org/docs ,結合 Bootstrap 跟 Vue.js 的套件,

雖然不用 jQuery 的好處還不明顯,但在六角的課程都學了 Bootstrap 跟 Vue.js 就學一下。

在 VSCode中,用快捷鍵 Ctrl + ` 叫出終端機,在專案資料夾底下輸入:


npm install vue bootstrap-vue bootstrap


然後就裝完了,接著在 Vue 的專案裡面 main.js 裡面作 import


import Vue from 'vue'
import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'


通常上面那行會有,下面就是 import 剛剛安裝的 BootstrapVue,後面是附的 Icon,可加可不加。

Vue裡面的套件需要先use,不然會報錯,所以加入下面兩行,Icon一樣可有可無:


Vue.use(BootstrapVue)
Vue.use(IconsPlugin)


CSS 方法是用 SCSS,容後再提。


這個套件我覺得最大的優勢是元件不太需要大改就有蠻完整的功能,這邊介紹兩個我有用到的:

Form select 跟 Form spinbutton。


Form select 

在官網的文件中提供的範例:





 可以看到,簡單的使用 v-model 同步選項的 value 以方便送出表單,並且可以在 data 中設定預設值 value, 如:(多餘的code省略)

 透過預設值的方法可以提升使用者體驗。

Form spinbutton 


就是可以按箭頭跳數字的功能,下面是我複製貼上改的,自己乖乖寫要寫:減號按鈕、顯示值、加號按鈕,三個東西,但是這個套件複製貼上一行搞定。 

我寫的內容如下: 




抄回來之後可以看到可以對數量作 v-model 的即時更新,可以限制中間數字欄的最大最小值,並且一樣可以透過 v-model 設定預設值,

@change是我需要把按過按鈕更新過的數量立刻更新 API 上的資料。

最後要補充一個地方,這個東西的寬度預設是 width:100% ,但是可以自動變大變小,如果要手動調整大小,要用 HTML 標籤包起來作調整。

如: 

 

col-8 是 Bootstrap 中, CSS 的格線系統,寬度大約等於所在位置寬度的 8/12,當然也可以選擇固定值或其他。