国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vuejs 實現簡易 todoList 功能 與 組件實例代碼

2024-05-06 15:30:22
字體:
來源:轉載
供稿:網友

todoList

結合之前 Vuejs 基礎與語法
•使用 v-model 雙向綁定 input 輸入內容與數據 data
•使用 @click 和 methods 關聯事件
•使用 v-for 進行數據循環展示

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>TodoList</title> <script src="./vue.js"></script></head><body> <div id="root">  <div>   <input v-model="inputValue"/>   <button @click="handleSubmit">提交</button>  </div>  <ul>   <li v-for="(item,index) of list" :key="index">    {{item}}   </li>  </ul> </div> <script>  new Vue({   el: "#root",   data: {    inputValue: '',    list: []   },   methods: {    handleSubmit: function(){     this.list.push(this.inputValue)     this.inputValue = ''    }   }  }) </script></body></html>

JSbin 預覽

todoList 組件拆分

Vuejs 組件相關 詳細參考組件基礎

全局組件

注冊全局組件,并在 HTML 中通過模板調用組件

//注冊全局組件  Vue.component('todo-item',{   template: '<li>item</li>'  })  <ul>   <!-- <li v-for="(item,index) of list" :key="index">    {{item}}   </li> -->   <todo-item></todo-item>   <!-- 通過模板使用組件 -->  </ul>

JSbin 預覽

局部組件

在注冊了局部組件之后,直接通過模板調用是不可以的,必須要在最外層的 Vue 的實例中添加 components: { }進行組件聲明。 

//注冊局部組件  var TodoItem = {   template: '<li>item</li>'  }  new Vue({   el: "#root",   components: {  //局部組件需要聲明的 components    'todo-item': TodoItem   },   data: {    inputValue: '',    list: []   },   methods: {    handleSubmit: function(){     this.list.push(this.inputValue)     this.inputValue = ''    }   }  })

JSbin 預覽

即通過局部注冊的組件,需要在其他的 Vue 實例中使用該局部組件。必須使用 components 對該局部組件進行注冊。
上面的實例中,要在 Vue 實例中使用 TodoItem 這個局部組件,就通過 todo-item 這個標簽來使用。當在實例中 注冊好了以后,才可以在模板里面使用這個標簽。這樣就算正確的使用了局部組件。

外部傳遞參數

給 todo-item 標簽添加 :content 屬性,值為循環的每一項的內容 "item",

這樣就可以吧 content 傳遞給 todo-item 這個組件

<todo-item v-for="(item,index) of list" :key="index" :content="item"></todo-item>

但是直接將組件改成是不行的

  Vue.component('todo-item',{   template: '<li>{{content}}</li>'  })

需要讓組件接收屬性,所以要在todo-item組件里面定義props屬性,其值為一個數組 'content' 。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 鸡东县| 广汉市| 钦州市| 喜德县| 阳西县| 卢龙县| 安多县| 朝阳县| 富蕴县| 上栗县| 遂宁市| 山阳县| 堆龙德庆县| 百色市| 观塘区| 中宁县| 宜川县| 许昌市| 紫云| 连平县| 迁安市| 三门峡市| 通化县| 洛扎县| 甘德县| 广西| 清徐县| 固镇县| 桓台县| 中方县| 新龙县| 芦山县| 东明县| 普定县| 南漳县| 达州市| 扶风县| 阿图什市| 榆社县| 颍上县| 井研县|