vue的簡單學(xué)習(xí)

todolist

<html>
 <head>
   <meta charset="utf-8">
   <title>todolist</title>
   <script src="js/vue.js"></script>
   <style type="text/css">
     .header {
       width: 300px;
       height: 60px;
     }
     .header div {
       width: 100px;
       height: 60px;
       float: left;
       text-align: center;
     }
     .body {
       width: 300px;
       height: 600px;
     }
     .body ul {
       width: 300px;
       height: 600px;
     }
     .bg {
       background: red;
     }
     .body .n {
       display: block;
     }
     .a {
       display: none;
     }
     .h {
       text-decoration: line-through;
     }
   </style>
 </head>
 <body>
   <div id="app">
     <div class="input">
       <input type="text" @keydown.enter="enterkeyDown" v-model="name" placeholder="請(qǐng)輸入完成的事情" />
     </div>
     <div class="tab">
       <div class="header">
         <div @click="allClick(0)" :class="{bg:index==0}" >全部</div>
         <div @click="todoClick(1)" :class="{bg:index==1}">未完成</div>
         <div @click="readyClick(2)" :class="{bg:index==2}">已完成</div>
       </div>
       <div class="body">
         <ul class="all a" :class="{n:index==0}">
           <li v-for="item in allLst" @click="rClick(item.id)" :class="{h:item.status == 1}">{{item.name}}</li>
         </ul>
         <ul class="todo a" :class="{n:index==1}">
           <li v-for="item in tolst" @click="rClick(item.id)">{{item.name}} </li>
         </ul>
         <ul class="ready a" :class="{n:index==2}">
           <li v-for="item in readList" class="h">{{item.name}}  <span @click="remove(item.id)">*</span></li>
         </ul>
       </div>
     </div>
   </div>
 </body>
 <script>
   const vm = new Vue({
     el:"#app",
     data: {
       name:"",
       allLst:[  // 所有的事情
         {id:0,name:"事情的名字",status:0},
         {id:1,name:"事情的名字1",status:1},
         {id:2,name:"事情的名字2",status:0}
         ],
       index:0 ,
       id:10 
     },
     methods:{
       allClick(index) { 
         this.index = index;
       },
       todoClick(index) {
         this.index = index;
       },
       readyClick(index) { 
         this.index = index;
       },
       enterkeyDown() {
         if (this.name == null || this.name == "") {
           return;
         }
         let item = {id:++this.id,"name":this.name,status:0};
         this.allLst.push(item);
         this.name = "";
       },
       rClick(id) {
         let item = null;
         for (let i = 0; i < this.allLst.length; i++) {
           item = this.allLst[i];
           if (item.id == id) {
             break;
           }
         }

         item.status = 1;
       },
       remove(id) {
         let index = null;
         for (let i = 0; i < this.allLst.length; i++) {
           const item = this.allLst[i];
           if (item.id == id) {
             index = i;
             break;
           }
         }
         this.allLst.splice(index,1)
       }
     },
     computed:{
       tolst:function() {
         return this.allLst.filter(function(item) {
           return item.status == 0;
         })
       },
       readList:function() {
         return this.allLst.filter(function(item) {
           return item.status == 1;
         })
       }
     }
   })
 </script>
</html>

購物車

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>購物車</title>
    <script src="js/vue.js"></script>
    <style>
      .spanB {
        border: 1px solid black;
        display: inline-block;
        width: 10px;
        height: 10px;
      }
      .selected {
        background: red;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="header">
        <span class="spanB" @click="allclick" :class="{selected:allTag}"></span>
      </div>
      <div class="body">
        <table>
          <thead>
            <tr>
              <th>狀態(tài)</th>
              <th>名稱</th>
              <th>價(jià)錢</th>
              <th>數(shù)量</th>
              <th>總價(jià)</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item,index) in lst" :key="index">
              <td>
                <span @click="selected(item)" class="spanB" :class="{selected:item.checked}"></span>
              </td>
              <td>
                {{item.name}}
              </td>
              <td>{{item.price}}</td>
              <th>
                <button @click="item.number++">增加</button>
                <input type="text" v-model="item.number"/>
                <button @click="decNumber(item.id)">減少</button>
              </th>
              <th>{{item.number * item.price}}</th>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="footer">
        <div> 選中商品的數(shù)量: {{selectedCount}}</div>
        <div> 選中商品的總價(jià): {{selectedTotalPrice}} </div>
      </div>
    </div>
  </body>
  <script>
    const vm = new Vue({
      el:"#app",
      data: {
        lst:[
          {"id":1,"name":"泡面","price":5,"number":1,"checked":false},
          {"id":2,"name":"辣條","price":10,"number":1,"checked":true},
          {"id":3,"name":"礦泉水","price":3,"number":1,"checked":false}
        ]
      },
      methods: {
        decNumber(id) {
          let index = null;
          for (let i = 0; i < this.lst.length; i++) {
            if (this.lst[i].id == id) {
              index = i;
            }
          }
          if (this.lst[index].number > 1) { 
            this.lst[index].number--;
          } else { 
            this.lst.splice(index,1); 
          }
        },
        selected(item) {
          item.checked = !item.checked;
        },
        allclick() { // 點(diǎn)擊全選按鈕
          if (this.allTag) { // 如果當(dāng)前是全選狀態(tài)
            for (let i = 0; i < this.lst.length; i++) {
              this.lst[i].checked = false;
            } 
          } else {
            for (let i = 0; i < this.lst.length; i++) {
              this.lst[i].checked = true;
            } 
          }
        }
      },
      computed:{
        selectedCount: function() {
          let result = 0;
          for (let i = 0; i < this.lst.length; i++) {
            if (this.lst[i].checked) {
              result+=this.lst[i].number;
            }
          }
          return result;
        },
        selectedTotalPrice:function() {
          let result = 0;
          for (let i = 0; i < this.lst.length; i++) {
            if (this.lst[i].checked) {
              result+=this.lst[i].number*this.lst[i].price;
            }
          }
          return result;
        },
        allTag:{
          get:function() {
            for (let i = 0; i < this.lst.length; i++) {
              if (!this.lst[i].checked) {
                return false;
              }
            }
            return true;
          }
        }
      }
    })
  </script>
</html>

vue的聲明周期

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>vue的生命周期</title>
    <script src="js/vue.js"></script>
  </head>
  <body>
    <div>
      <div id="app">
        {{name}}
        <div @click="name+='aa'">點(diǎn)擊</div>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    const vm = new Vue({
      el:"#app",
      data: {
        name:"張三"
      },
      created() { // 在created中發(fā)送網(wǎng)絡(luò)請(qǐng)求
        console.log("初始化完成就會(huì)執(zhí)行")
      },
      mounted() {
        console.log("掛載")
      },
      destroyed() { // 移除一些監(jiān)聽田藐,例如定時(shí)器
        console.log("銷毀")
      }
    })
    
    // vm.$mount("#app");
  </script>
</html>

生命周期主要分為三個(gè) created mounted destroyed
在生命周期的不同階段,會(huì)回調(diào)一些鉤子函數(shù)
在相應(yīng)的生命周期鉤子函數(shù)中寫入相應(yīng)方法來實(shí)現(xiàn)相應(yīng)功能

node

js運(yùn)行在瀏覽器中, 不能擁有操作文件憔披, 二進(jìn)制等這些功能
運(yùn)行環(huán)境,es規(guī)范琅束,提供依賴包(http,io,buffer...)
我們可以利用node這個(gè)環(huán)境開發(fā)后臺(tái)服務(wù)器

npm

node提供一個(gè) js包的管理工具
npm 切換到 cnpm
npm install cnpm -g --registry=https://registry.npm.taobao.org
使用cnpm 安裝依賴包 -g是全局安裝
cnpm install 包名 -g
cnpm install 包名 --save
如果不添加 --save 只會(huì)下載這個(gè)包,在package.json文件不會(huì)下載的記錄
--dev 開發(fā)的時(shí)候使用诈嘿,上線之后就不在使用的一些包
cnpm uninstall 包名 下載包
dependencies 上線之后還在用(bootstrap,jquery)
devDependencies 開發(fā)打包的時(shí)候使用,上線之后就不使用了(webpack)
如果沒有添加 --save, 在package.json中不會(huì)有記錄
如果下載的項(xiàng)目由package.json糠雨, 只需要執(zhí)行 cnpm install , 他就會(huì)把依賴包都下載下來

webpack (gulp)

自動(dòng)化的打包工具
瀏覽器只能認(rèn)識(shí) html, css, js, 圖片
為了快速開發(fā)(es6,sass,typeScript...,aa.Vue ) -> 工具 -> html,css,js,圖片
安裝 cnpm install webpack webpack-cli --save --dev
編寫webpack的文件 webpack.config.js
webpack 打包指令進(jìn)行打包就可以了

vue-cli

腳手架才睹, 創(chuàng)建半成品,我們?cè)诎氤善坊A(chǔ)上開發(fā)
npm install -g @vue/cli 安裝腳手架
創(chuàng)建項(xiàng)目 vue create 項(xiàng)目名 (先通過cd進(jìn)入到創(chuàng)建項(xiàng)目的目錄)
babel 用來把es6 -> es5, 把typeScripte -> js
eslink 代碼格式的檢測工具

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末甘邀,一起剝皮案震驚了整個(gè)濱河市琅攘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌松邪,老刑警劉巖坞琴,帶你破解...
    沈念sama閱讀 218,640評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異测摔,居然都是意外死亡置济,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門锋八,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浙于,“玉大人,你說我怎么就攤上這事挟纱⌒咝铮” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵紊服,是天一觀的道長檀轨。 經(jīng)常有香客問我,道長欺嗤,這世上最難降的妖魔是什么参萄? 我笑而不...
    開封第一講書人閱讀 58,755評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮煎饼,結(jié)果婚禮上讹挎,老公的妹妹穿的比我還像新娘。我一直安慰自己吆玖,他們只是感情好筒溃,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著沾乘,像睡著了一般怜奖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上翅阵,一...
    開封第一講書人閱讀 51,610評(píng)論 1 305
  • 那天歪玲,我揣著相機(jī)與錄音,去河邊找鬼掷匠。 笑死读慎,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的槐雾。 我是一名探鬼主播夭委,決...
    沈念sama閱讀 40,352評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼募强!你這毒婦竟也來了株灸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,257評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤擎值,失蹤者是張志新(化名)和其女友劉穎慌烧,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鸠儿,經(jīng)...
    沈念sama閱讀 45,717評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡屹蚊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評(píng)論 3 336
  • 正文 我和宋清朗相戀三年厕氨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片汹粤。...
    茶點(diǎn)故事閱讀 40,021評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡命斧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出嘱兼,到底是詐尸還是另有隱情国葬,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評(píng)論 5 346
  • 正文 年R本政府宣布芹壕,位于F島的核電站汇四,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏踢涌。R本人自食惡果不足惜通孽,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望睁壁。 院中可真熱鬧利虫,春花似錦、人聲如沸堡僻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽钉疫。三九已至硼讽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間牲阁,已是汗流浹背固阁。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留城菊,地道東北人备燃。 一個(gè)月前我還...
    沈念sama閱讀 48,224評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像凌唬,于是被迫代替她去往敵國和親并齐。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評(píng)論 2 355