Vue CLI

腳手架的文件結構

|—— node_modules
|—— public
|     |—— favicon.ico 頁籤圖標
|     |—— index.html 主頁面
|
|—— src
|    |—— assets 存放靜態資源
|    |     |—— logo.png
|    |
|    |—— components 存放組件
|    |        |—— HelloWorld.vue
|    |
|    |—— App.vue 匯總所有組件
|    |—— main.js 入口文件
|
|—— .gitignore git版本管制忽略的配置
|—— babel.config.js babel的配置文件
|—— package.json 應用包配置文件
|—— README.md 應用描述文件
|—— package-lock.json 包版本控制文件

vue.config.js 配置文件

使用vue inspect > output.js可以查看到Vue腳手架的默認配置
使用vue.config.js 可以對腳手架進行個性化定制

ref屬性

  1. 被用來給 元素 或 子組件 註冊引用信息(id的替代者)

  2. 應用在html標籤上獲取的是真實DOM元素
    應用在組件標籤上則是組件的實例對象(vc)

  3. 使用方式:

    • 標識:<h1 ref="xxx"></h1> 或 <School ref="xxx"></School>
    • 獲取:this.$refs.xxx

混入mixin

  1. 功能:可以把多個組件共同的配置提取成一個混入對象

  2. 使用方式:

    1. 定義:
      1
      2
      3
      4
      5
      export default xxx{
      data(){...}
      methods:{...}
      ...
      }
    2. 使用:

      1. 全局混入:

        1
        2
        Vue.mixin(xxx)
        Vue.mixin(yyy)
      2. 局部混入:

        1
        mixins:['xxx', 'yyy']
    3. 備註:

      • 如果混入的屬性與函數和組件裡的重名,則使用組件的屬性與函數
      • 如果混入的是組件函數,則都會執行,例如:mounted()

插件plugins

  1. 功能:用於增強Vue

  2. 本質:包含install(Vue, options)方法的一個對象
    第一個參數:Vue
    第二個以後的參數:插件使用者傳遞的數據

  3. 定義插件:

    1
    obj.install = function(Vue, options){...}
  4. 使用插件:

    1
    Vue.use(xxx)

scoped樣式

  1. 作用:讓樣式在局部生效,防止衝突

  2. 寫法:<style scoped>

  3. 具體代碼;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <style scoped lang="less">
    .demo{
    background-color: orange;
    color: aqua;
    .less_test{
    font-size: 40px;
    }
    }
    </style>

配置項props

  1. 功能:讓組件接受外部傳進來的數據

  2. 傳遞數據:

    1
    <Demo :name="xxx"/>
  1. 接收數據:

    1. 只接收

      1
      props['name']
    2. 限制類型

      1
      2
      3
      props:{
      name:String
      }
    3. 限制類型、限制必要性、指定默認值

      1
      2
      3
      4
      5
      6
      7
      props:{
      name:{
      type: String // 類型
      required: true // 必要性
      default: 'yyy' // 默認值
      }
      }
  1. 備註:
    • props是只讀的,Vue底層會監測對props的修改,如果進行了修改,會發出警告
    • 若必須修改傳入數據,那麼可以複製props的內容到data中,去修改data中的數據
    • props傳過來的若是對象類型的值,修改對象中的屬性時Vue不會報錯,但不推薦這麼做

props實現組件間通信

  1. 父組件 -> 子組件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <!-- 父組件 -->
    <template>
    <School name="xxx" :age="19"/>
    </template>

    <!-- 子組件 -->
    <script>
    props:['name', 'age']
    </script>
  1. 子組件 -> 父組件
    • 思路:由於props只能父組件給子組件,因此父組件可以給子組件一個函數,在傳遞時子組件調用該函數
      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
      <!-- 父組件 -->
      <template>
      <div>
      <h1>Hello World, {{name}}</h1>
      <!-- 父給子一個函數 -->
      <Demo :changeNameFunction="changeNameFunction"/>
      </div>
      </template>
      <script>
      data(){
      return {
      name:''
      }
      },
      methods: {
      // 修改數據
      changeNameFunction(name){
      this.name = name
      }
      }
      </script>

      <!-- 子組件 -->
      <template>
      <div>
      <input type="text" v-model="name">
      <button @click="changeName">Click</button>
      </div>
      </template>
      <script>
      props:['changeNameFunction'],
      data() {
      return {
      name:'',
      }
      },
      methods: {
      changeName(){
      // 要修改數據時,調用父組件的方法,並通過參數傳遞數據
      this.changeNameFunction(this.name)
      }
      },
      </script>

瀏覽器本地存儲webStorage

  1. 儲存內容大小一般支持5MB左右
  1. 瀏覽器通過Window.sessionStorage 和 Window.localStorage 屬性來實現本地儲存機制
  1. 相關API:

    1. xxxxxStorage.setItem('key', 'value')

      • 該方法接收一個鍵和值為參數,會把鍵值添加到儲存中,如果鍵名存在,則更新其對應的值
      • 儲存的value統一變更為字符串:
        • 因此可以使用JSON.stringify()將對象以字符串形式保存
        • 再以JSON.parse()的方式將字符串轉變為對象
    2. xxxxxStorage.getItem('key')

      • 該方法接收一個鍵名作為參數,返回鍵名對應的值
    3. xxxxxStorage.removeItem('key')

      • 該方法接收一個鍵名作為參數,並把該鍵名從儲存中刪除
    4. xxxxxStorage.clear()

      • 該方法會清空存儲中所有數據
  1. 備註:
    1. SessionStorage存儲的內容會隨著瀏覽器窗口關閉而消失
    2. LocalStorage存儲的內容需要手動清除才會消失
    3. xxxxStorage.getItem('key')如果key對應的value獲取不到,則返回null
    4. JSON.parse(null)的結果依然是null

組件自定義事件

  1. 一種組件鍵的通信方式,適用於:子組件 -> 父組件

  2. 使用場景:子組件想給父組件傳遞數據,那麼就要在父組件中給子組件綁定自定義事件(事件的回調函數在A中)

綁定自定義事件

  1. 用@或v-on綁定自定義事件

    • 具體代碼:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      <template>
      <!-- 第一種方式:使用 @ 或 v-on 綁定自定義事件 -->
      <Student @xxx="demo"/>
      <Student v-on:xxx="demo"/>
      </template>
      <script>
      // 配置事件回調函數
      methods:{
      demo(para){ ... }
      },
      </script>
    • 一次性綁定自定義事件:

      1
      2
      3
      4
      <template>
      <Student @xxx.once="demo"/>
      <Student v-on:xxx.once="demo"/>
      </template>
  1. 使用ref綁定自定義事件

    • 注意:通過this.$refs.test.$on()綁定事件時,回調函數demo()要麼配置在methods中,要麼使用箭頭函數,否則this會指向子組件

    • 具體代碼:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      <template>
      <!-- 第二種方式:使用 ref 綁定自定義事件 -->
      <Student ref="student"/>
      </template>
      <script>
      // 配置事件回調函數
      methods:{
      demo(para){ ... }
      },
      mounted(){
      this.$refs.student.$on('xxx', this.demo)
      this.$refs.student.$on('xxx', () => { ... })
      }
      </script>
    • 優勢:更加靈活,能追加其他需求

      1
      2
      3
      4
      5
      6
      mounted(){
      // 實現3秒後再綁定自定義事件
      setTimeout(() => {
      this.$refs.student.$on('xxx', this.demo)
      }, 3000)
      }
    • 一次性綁定自定義事件:

      1
      2
      3
      4
      <script>
      // 更換 $on API為 $once
      this.$refs.test.$once('event', this.demo)
      </script>

觸發自定義事件

  • 使用$emit()觸發自定義事件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!-- 子組件 -->
    <template>
    <button @click="sendStudentName">Click</button>
    </template>
    <script>
    methods:{
    sendStudentName(){
    // 觸發子組件身上的xxx事件
    this.$emit('xxx', param1, param2, ...)
    }
    }
    </script>
  • 自定義事件回調函數接收參數

    1
    2
    3
    4
    5
    6
    methods:{
    // 接收一個參數
    demo(param){ ... }
    // 接收多個參數,封裝到 params[]
    demo(...params){ ... }
    }

解綁自定義事件

  • 使用$off解綁自定義事件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <!-- 子組件 -->
    <template>
    <button @click="unbind">Click</button>
    </template>
    <script>
    methods:{
    unbind(){
    this.$off('event') // 解綁單一事件
    this.$off(['e1','e2']) // 解綁多項事件
    this.$off() // 解綁全部事件
    }
    }
    </script>

綁定原生事件

  • 組件上也可以綁定原生DOM事件,需要使用native修飾符
    1
    2
    3
    <template>
    <Student @click.native="funct" />
    </template>

全局事件總線

  1. 全局事件總線(Global Event Bus)是一種組件間通信的方式,適用於任意組件間通信
  1. 在main.js安裝全局事件總線
    1
    2
    3
    4
    5
    6
    new Vue({
    ...
    beforeCreate(){
    Vue.prototype.$bus = this // 安裝全局事件總線
    }
    })
  1. 使用事件總線

    1. 接收數據:

      • A組件想接收數據,則在A組件中給$bus綁定自定義事件,事件在回調留在A組件身上
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        methods(){
        demo(data){...}
        },
        mounted(){
        // 配置在methods中
        this.$bus.$on('xxx', this.demo)
        // 使用箭頭函數
        this.$bus.$on('xxx', (data) => {
        ...
        })
        },
        beforeDestory(){
        this.$bus.$off('xxx')
        }
    2. 提供數據:

      1
      this.$bus.$emit('xxx', data)

消息訂閱與發布

  1. 消息訂閱與發布 (pubsub) 是一種組件間通信的方式,適用於任意組件間通信
  1. 安裝pubsub
    1
    npm i pubsub-js
  1. 在main.js引入
    1
    import pubsub from 'pubsub-js'
  1. 使用pubsub

    1. 接收數據:

      • A組件想接收數據,則在A組件中訂閱消息,訂閱的回調留在A組件自身中
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        methods(){
        demo(data){...}
        },
        ...
        mounted(){
        this.pid = pubsub.subscribe('xxx', this.demo)
        this.pid = pubsub.subscribe('xxx', (parameters) => {
        ...
        })
        },
        beforeDestory(){
        pubsub.unsubscribe(this.pid)
        }
    2. 提供數據:

      1
      pubsub.publish('xxx', data)

nextTick

  1. 解析順序的問題:
    • 使用v-show時,由於元素在調用函數時並沒有來到頁面上,因此對元素的操作無效
    • 案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      <template>
      <div>
      <!-- 點擊按鈕時出現input框並獲取焦點 -->
      <input type="text" v-show="item.isEdit" ref="inputTitle">
      <button @click="handleEdit(item)">Edit</button>
      </div>
      </template>
      <script>
      methods:{
      handleEdit(item){
      if(item.hasOwnProperty('isEdit')){
      item.isEdit = true
      }else{
      this.$set(item, 'isEdit', true)
      }
      // 由於調用函數時,input框還沒有來到頁面上,因此無法獲取焦點
      this.$refs.inputTitle.focus()
      }
      }
      </script>
  1. 使用nextTick()方法
    • 作用:在下一次DOM更新結束後,執行其指定代碼
    • 當改變數據後,要基於更新後的新DOM進行某些操作時,要在nextTick所制定的回調函數中執行
  1. 語法:
    1
    this.$nextTick(回調函數)
  1. 具體代碼:
    1
    2
    3
    4
    5
    // 在DOM更新完畢後執行函數
    this.$nextTick(function(){
    // DOM更新完畢,input框已經在頁面上,就可以獲取焦點了
    this.$refs.inputTitle.focus()
    })

過渡與動畫

  1. 作用:在插入、更新或移除DOM元素時,在合適的時候給元素添加樣式類名
  1. 語法:

    1. 樣式:

      • 元素進入的樣式:

        1. v-enter 進入的起點
        2. v-enter-active 進入過程中(進入的時候被激活)
        3. v-enter-to 進入的終點
      • 元素離開的樣式:

        1. v-leave 離開的起點
        2. v-leave-active 離開過程中
        3. v-leave-to 離開的終點
    2. 使用<transition>包裹要過度的元素,並配置name屬性:

      1
      2
      3
      <transition name="xxx">
      <h1 v-show="isShow">Hello<h1>
      </transition>

      樣式:

      1
      2
      xxx-enter{...}
      ...
    3. 備註:若有多個元素需要過渡,則需要使用<transition-group>,切每一個元素都要指定的key值

  1. 其他:
    • 初始引入動畫,在<transition>中加入appear

動畫庫

  1. 安裝:
    1
    npm install animate.css
  1. 引入:
    1
    import 'animate.css'
  1. 使用:
    1
    2
    3
    4
    5
    6
    7
    8
    <transition 
    name="animate__animated animate__bounce"
    <!-- 複製粘貼已配置的動畫名xxx -->
    enter-active-class="xxx"
    leave-active-class="xxx"
    >
    <a href="https://animate.style/">animate.css官網</a>
    </transition>