Vue 2.X+ Router + Cli + VueX ( 六角课程笔记 )

1. 双向绑定 v-model

https://ithelp.ithome.com.tw/upload/images/20220310/20137684SxzjKgEn1b.png
小技巧:不会让使用者点选到第一个option
https://ithelp.ithome.com.tw/upload/images/20220310/201376849v5muXKQWG.png
https://ithelp.ithome.com.tw/upload/images/20220310/20137684C73NQGJzUR.png


2.渲染 v-for、v-if、v-else、v-show

https://ithelp.ithome.com.tw/upload/images/20220310/201376841LxMUKwltK.png
https://ithelp.ithome.com.tw/upload/images/20220310/20137684q629o6y54l.png


3. 渲染 v-bind

https://ithelp.ithome.com.tw/upload/images/20220310/20137684Oz14MUyXJj.png


4. 渲染 v-style 可用物件或阵列陈述

:class
https://ithelp.ithome.com.tw/upload/images/20220310/20137684HAZ3U36EwZ.png


5. 事件绑定 v-on

https://ithelp.ithome.com.tw/upload/images/20220310/20137684pOaxFyz604.png


6. computed

只读不写 无法直接this.xxx = xxx
差异:computed function名称无需使用 data
使用:内部的变数若变更,会直接触发computed
变数通常为data并且绑v-model
ex: get set
https://ithelp.ithome.com.tw/upload/images/20220310/20137684kh0jZOito0.png

* computed 可抓 computed 来使用 (@{}@!!)

    // 取得各状态总笔数
    filteredPage() {
      if (this.visibility === "all") {
        console.log("this.toDos", this.toDos);
        return this.toDos;
      }
      if (this.visibility === "active") {
        let activeFilter = this.toDos.filter((item) => {
          return item.completed === false;
        });
        console.log("activeFilter", activeFilter);
        return activeFilter;
      }
      if (this.visibility === "completed") {
        let completedFilter = this.toDos.filter((item) => {
          return item.completed === true;
        });
        console.log("completedFilter", completedFilter);
        return completedFilter;
      }
      return "";
    },
    // 藉由总笔数取得分页
    filterGetPage() {
      let filterPage = [];
      this.filteredPage.forEach((item, index) => {
        if (index % 5 === 0) {
          // 0/0 === 0 =_=
          filterPage.push([]);
        }
      });
      return filterPage;
    },

7.watch

差异:直接将v-model所绑的data 命名function
使用:监听data资料,改变直接触发
通常只能一次监听一个变数
若要一次监听多个需使用深度监听 > deep+物件

https://ithelp.ithome.com.tw/upload/images/20220310/20137684dDfNluDGmV.png

watch 监听阵列 或 物件中其一属性

https://ithelp.ithome.com.tw/upload/images/20220310/20137684eVXNOvTwvw.png


8.Vue 生命周期

ex:v-if
(1) DOM生成
https://ithelp.ithome.com.tw/upload/images/20220310/201376842GLgutRo2p.png
(2) Updated + destroyed
https://ithelp.ithome.com.tw/upload/images/20220310/20137684h6Za2jyPHV.png
(3) 保留资料 keep-alive
https://ithelp.ithome.com.tw/upload/images/20220310/201376842cDYqISGb9.png


9.Vue Component

注意html标签不能大写、元件data记得return

根元件
https://ithelp.ithome.com.tw/upload/images/20220310/20137684ODtzUbro9M.png
子元件
https://ithelp.ithome.com.tw/upload/images/20220310/20137684VB2n9hffqu.png
全域注册元件
https://ithelp.ithome.com.tw/upload/images/20220310/20137684X83YztnP8m.png


10.Props (外传内)

https://ithelp.ithome.com.tw/upload/images/20220310/20137684AwqgQTQGQv.png

Props 注意事项

(1) 子元件 拿取 根元件 资料,改根元件资料时会跳错

解决:另给子元件一个data装下该值,就不会直接更改到跟元件资料
https://ithelp.ithome.com.tw/upload/images/20220310/20137684ahFkJIv8Nd.png

(2) ajax抓资料速度不同,使用v-if判断有无抓到资料而後显示该元素

https://ithelp.ithome.com.tw/upload/images/20220310/20137684cL12f2n2HX.png

(3) 维持状态与生命周期

https://ithelp.ithome.com.tw/upload/images/20220310/2013768465bKGYuHSH.png


11. Emit 内传外 or 触发(传递)事件

https://ithelp.ithome.com.tw/upload/images/20220310/20137684qDLsxPdO4t.png


12. slot 使用他人套件,欲套用自己的内容时

一次全换
https://ithelp.ithome.com.tw/upload/images/20220310/20137684VJ7PHBwXTs.png
https://ithelp.ithome.com.tw/upload/images/20220310/20137684sB6anHFM8d.png
抽换
https://ithelp.ithome.com.tw/upload/images/20220310/20137684ZHlpq9vqM6.png


13. is 动态切换元件

https://ithelp.ithome.com.tw/upload/images/20220310/20137684oPhdtbLwti.png
https://ithelp.ithome.com.tw/upload/images/20220310/20137684qwNHtR1rHC.png
效果同下v-if
https://ithelp.ithome.com.tw/upload/images/20220310/20137684X2kEzNutrn.png


14. Vue Cli

(1) 安装 node + Vue cli 并建立专案

vue create new-projext(专案名称)

a. 上课教学
https://ithelp.ithome.com.tw/upload/images/20220310/20137684yy4qqjtNqA.png
b. 打完指令後选最下面的,自订安装
prettier 不熟悉语法用
https://ithelp.ithome.com.tw/upload/images/20220312/20137684BXyP76oZ26.png
(2) 专案建立完成

npm run build
npm run serve
https://ithelp.ithome.com.tw/upload/images/20220310/20137684EfITWtiTex.png
(3) 架构
https://ithelp.ithome.com.tw/upload/images/20220310/201376845ZI8cC5g7A.png


15. Vue Router

(1) 虚拟路径配置

https://ithelp.ithome.com.tw/upload/images/20220310/20137684LcYfytKVfJ.png

(2) 虚新增路径及连结

https://ithelp.ithome.com.tw/upload/images/20220310/20137684wUhZpS82ur.png

(3) 巢状路由

https://ithelp.ithome.com.tw/upload/images/20220310/20137684Qg78T34FdU.png
https://ithelp.ithome.com.tw/upload/images/20220310/20137684kH4AZskndv.png

(4) 动态路由 npm install axios

抓取网址列後段进行操作
https://ithelp.ithome.com.tw/upload/images/20220310/20137684cQeJmySjz2.png
https://ithelp.ithome.com.tw/upload/images/20220310/20137684htdqRYTQAQ.png

(5) 同一个路径载两个元件

https://ithelp.ithome.com.tw/upload/images/20220310/20137684YAHFxwq4hB.png
https://ithelp.ithome.com.tw/upload/images/20220310/20137684ah6WGPtZxz.png

(6) router方法

https://ithelp.ithome.com.tw/upload/images/20220310/20137684j8EUewc329.png


15. VueX

  1. 禁止於Vue各元件档案直接抓底层资料 ( this.$store.state.XXX )
    解决:getter 先抓资料 ( 详见(1) )

  2. 同前 ( this.$store.commit ) 禁止
    解决:action 先抓资料给 mutation变更,action才可用commit ( 详见(2) )

(1)传值 state + getter + mapGetters

https://ithelp.ithome.com.tw/upload/images/20220310/20137684obWnmscaEI.png

(2)改变值 state + mutation + action(commit) + mapActions

https://ithelp.ithome.com.tw/upload/images/20220310/20137684U5raeoRWDZ.png


<<:  Python读取MySQL资料库bool值後,判断式的有趣问题

>>:  Webpack

网络框架:如何制定protocol

接着上一篇,来看看如何制定protocol,制定的时候,一般是server端和client端双方坐下...

绝对路径及相对路径

上一篇提到图片汇入方式最重要环节就是src属性,相对路径及绝对路径使用方式 要注意相对路径档案及绝...

Day 10 - 用 canvas 复刻 小画家 放大镜

动手写看看 目前只能做到放大... const [magnifier, setMagnifier] ...

基础建设: 事件与讯息系统

微服务是一种以业务功能为主的服务设计概念,将业务功能明确划分和解耦合,彼此独立但协作的方式叠代更新。...

Day28 D3js Diagram常见的两点浪漫路径

D3js Diagram常见的两点浪漫路径 用途 在绘制diagram图表时,会用到的垂直水平连线,...