最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue.js  父向子組件傳參的實(shí)例代碼

 更新時(shí)間:2017年10月29日 11:44:54   作者:zjsfdx  
這篇文章主要介紹了vue.js 父向子組件傳參的實(shí)例代碼,需要的朋友可以參考下

1.新建componentA.vue組件,代碼如下:

store.js代碼如下:

const STORAGE_KEY = 'todos-vue.js'
export default{
  fetch(){
    return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]')
  },
  save(items){
    window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items));
  }
}

App.vue代碼如下:

<template>
 <div id="app">
  <h1 v-text="title"></h1>
  <input v-model="newItem" v-on:keyup.enter="addNew"/>
  <ul>
   <li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click='toogleFinish(item)'>
    {{item.label}}
   </li>
  </ul>
  <!-- 使用組件,注意駝峰命名法轉(zhuǎn)化成短線 -->
  <!-- 向自組件傳數(shù)據(jù) -->
  <Component-a msgfromfather='you die!'></Component-a>
 </div>
</template>
<script>
import Store from './store'
import ComponentA from './components/componentA'  //該組件會(huì)被加載到該頁(yè)面
export default {
 name: 'app',
 data () {
  return {
   title: 'this is a todo list',
   items:Store.fetch(),
   newItem:''
  }
 },
 components:{ //注冊(cè)組件
  ComponentA
 },
 watch:{
   items:{
    handler(items){   //經(jīng)過變化的數(shù)組會(huì)作為第一個(gè)參數(shù)傳入
     Store.save(items)
    },
    deep:true    //深度復(fù)制
   }
 },
 methods:{
  toogleFinish(item){
   item.isFinished = !item.isFinished
  },
  addNew(){
   this.items.push({
    label:this.newItem,
    isFinished:false,
   })
   this.newItem = ''
  }
 }
}
</script>
<style>
#app {
 font-family: 'Avenir', Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
}
.finished{
 text-decoration: underline;
}
</style>

componentA.vue代碼如下:

<template>
 <div class="hello">
  <h1>{{msg}}</h1>
  <h2>{{msgfromfather}}</h2>
  <button v-on:click="onClickMe">Click!</button>
 </div>
</template>
<<script>
export default {
 data(){
   return{
     msg:'Hello form component a'
   }
 },
 props:['msgfromfather'],//自組件接收數(shù)據(jù)
 methods:{
   onClickMe(){
     console.log(this.msgfromfather);
   }
 }
}
</script>
<style scoped>
</style>

點(diǎn)擊按鈕之后效果圖如下:

總結(jié)

以上所述是小編給大家介紹的vue.js  父向子組件傳參的實(shí)例代碼,希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論

泰顺县| 栖霞市| 新郑市| 隆化县| 望城县| 焦作市| 谷城县| 醴陵市| 梁河县| 临泉县| 绵竹市| 巴南区| 河西区| 丰原市| 永安市| 张家界市| 隆安县| 徐水县| 华池县| 务川| 乌鲁木齐县| 北票市| 临泉县| 武川县| 抚远县| 丰镇市| 泽州县| 广平县| 宁晋县| 资兴市| 宝应县| 中山市| 务川| 嘉兴市| 望谟县| 霞浦县| 平定县| 高要市| 冀州市| 桂阳县| 元阳县|