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)化成短線(xià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)過(guò)變化的數(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)文章
vue的滾動(dòng)條插件實(shí)現(xiàn)代碼
這篇文章主要介紹了vue的滾動(dòng)條插件實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09vue 封裝 Adminlte3組件的實(shí)現(xiàn)
這篇文章主要介紹了vue 封裝 Adminlte3組件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03詳解mpvue開(kāi)發(fā)微信小程序基礎(chǔ)知識(shí)
這篇文章主要介紹了詳解mpvue開(kāi)發(fā)微信小程序基礎(chǔ)知識(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09使用Vue開(kāi)發(fā)自己的Chrome擴(kuò)展程序過(guò)程詳解
這篇文章主要介紹了使用Vue開(kāi)發(fā)自己的Chrome擴(kuò)展程序過(guò)程詳解,瀏覽器擴(kuò)展程序是可以修改和增強(qiáng) Web 瀏覽器功能的小程序。它們可用于各種任務(wù),例如阻止廣告,管理密碼,組織標(biāo)簽,改變網(wǎng)頁(yè)的外觀和行為等等。,需要的朋友可以參考下2019-06-06vue工程全局設(shè)置ajax的等待動(dòng)效的方法
這篇文章主要介紹了vue工程全局設(shè)置ajax的等待動(dòng)效的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02vuejs 切換導(dǎo)航條高亮(路由菜單高亮)的方法示例
這篇文章主要介紹了vuejs 切換導(dǎo)航條高亮路由高亮的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05vue中Echarts使用動(dòng)態(tài)數(shù)據(jù)的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了vue中Echarts使用動(dòng)態(tài)數(shù)據(jù)的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue中控制mock在開(kāi)發(fā)環(huán)境使用,在生產(chǎn)環(huán)境禁用方式
這篇文章主要介紹了vue中控制mock在開(kāi)發(fā)環(huán)境使用,在生產(chǎn)環(huán)境禁用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04