2018-07-27(vue)

Vue

   采用簡潔的模板語法來,聲明式地將數(shù)據(jù)渲染進去DOM系統(tǒng):
      < div id = "app">

       { {message} }     
 
      </div>
     
    let app = new Vue ({
      
     el : '#app',
     data:{
       
    message:' Hello Vue! '
 
}
 

})


這就創(chuàng)建了一個 VUE 應用! 看起來這跟 渲染一個 字符串模板 非常類似,但是 Vue 做了大量的工作,現(xiàn)在數(shù)據(jù)和DOM 已經(jīng)被建立了關聯(lián),所有東西都是響應式的。

在 javaScript 控制臺,并修改 app.message 的值,上面例子的 值 也會發(fā)生改變。

綁定元素的 特性

v - bind

      <div id = ' app - 2 '>
      <span v - bind:title = ' message '>
             //鼠標懸停幾秒鐘查看此處動態(tài)綁定的提示信息!
      </span>
      </div>

    var app2 = new Vue({

        el : '#app2',

        data : {
  
         message : ' 頁面加載與 '    +  new Date() . toLocaleString()

//  現(xiàn)在的時間    轉換了  時間格式。   
   
    }


  })

條件 與 循環(huán)

控制切換一個元素是否顯示。

v - if

      <div id = "app-3">
      <p v - if = "seen"> 現(xiàn)在你看到我了 </p>
      </div>

      let app3 = new Vue({
       
          el : ' #app - 3 ',
   
          data:{
    
             seen : true
   
}
   

})
    
      //  在 控制臺 輸入  app3.seen = false.
          顯示的信息消失、

vue 不僅可以 把 數(shù)據(jù) 綁定到 DOM 文本或特性, 還可以 綁定到 DOM 結構,此外,Vue 也提供,一個強大的 過渡效果系統(tǒng),可以在 Vue 插入 更新 移除 元素 時 自動應用,過渡效果。

指令 :

 v - for   可以綁定  數(shù)組的 數(shù)據(jù) 來  渲染 一個 項目列表:

     <div id = 'app-4'>
          <ol>
                 <li v - for = "todo in todos">
  
                     {{  todo . text  }}
   
                 </li>
          </ol>
     </div>

     var app4 = new Vue({

              el : ' #app-4 ',
              data :{

                   todos:{

                  { text : ' 學習 JavaScript ' },
                  { text : ' 學習 Vue ' },
                  { text: '整個牛項目 ' }


}



}

})
      

在控制臺 ,輸入 app4.todos.push( text: ' 新項目 ' ) , 添加了新項目。

處理用戶輸入

v-on 添加一個事件監(jiān)聽器, 調(diào)用 它在 Vue 中 定義的方法:

     
      <div id = "app-5">
            <p>
               {{ message }} 
            </p>
            <button v - on : click = ' reverseMessage '>
                 逆轉消息
            </button>
      </div>
      

      let app5 = new Vue({

           el : '#app-5',
           data:  {

                 message: ' Hello Vue.js! '

},

      methods: {

            reverseMessage: function(){

                  this.message = this.message.split('').reverse().join('')

}


}

})

v - model 指令 , 它能輕松實現(xiàn) 表單輸入 和 應用狀態(tài)之間的 雙向綁定。


     <div id = "app-6">
             <p> {{message}}</p>
             <input v-model =" message ">
     </div>
     
     let app6  = new Vue({

        el : ' #app-6 ',
        data : {
 
             message : ' Hello Vue ! '

}


})


組件化應用構建

     <ol>


         //    創(chuàng)建 一個   todo - item  組件的 實例 


         <todo-item></todo-item>
  

    </ol>



      Vue.component ( 'todo-item',{

           template : ' <li> 這是誰??? </li>'


} )


最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容