Vue2.0父子組件傳遞函數(shù)的教程詳解
Vue.js 是什么
Vue.js (讀音 /vjuː/,類似于 view) 是一套構(gòu)建用戶界面的漸進(jìn)式框架。與其他重量級框架不同的是,Vue 采用自底向上增量開發(fā)的設(shè)計(jì)。Vue 的核心庫只關(guān)注視圖層,它不僅易于上手,還便于與第三方庫或既有項(xiàng)目整合。另一方面,當(dāng)與單文件組件和 Vue 生態(tài)系統(tǒng)支持的庫結(jié)合使用時(shí),Vue 也完全能夠?yàn)閺?fù)雜的單頁應(yīng)用程序提供驅(qū)動。
學(xué)習(xí)筆記:在vue2.0中,父組件調(diào)用子組件時(shí),想要將父組件中的函數(shù)體也做傳遞.
1. 通過props :需要從子組件傳參數(shù)到父組件時(shí)適用
// 父組件.vue
<template> <div> <ok-input :params='number' :callback='callbackNum'></ok-input> </div> </template> <script type="text/ecmascript-6"> import okInput from '../ok-input/okinput.vue'; export default { props: {}, data() { return { number: {}, callbackNum: function (x) { console.log(x); } }; }, methods: { }, components: { 'ok-input': okInput } }; </script>
// 子組件.vue
<template> <div> <input v-model='numVal' @change='handleFun'></input> </div> </template> <script type="text/ecmascript-6"> import {Input, Select, Option, Button} from 'element-ui'; import 'element-ui/lib/theme-default/index.css'; export default { props: { params: { type: Object, default: { type: '' } }, callback: {} }, data() { return { x: 'hah', numVal: '' }; }, methods: { handleFun(val) { this.callback(val); // 將參數(shù)傳回父組件中的回調(diào)函數(shù) } }, components: { 'el-input': Input, } }; </script>
2.通過$emit: 只需獲得當(dāng)前操作對象時(shí)適用
// 父組件.vue <template> <div> <ok-input :params='inputs' @change='handleAge'></ok-input> </div> </template> <script type="text/ecmascript-6"> import okInput from '../ok-input/okinput.vue'; export default { props: {}, data() { return { number: {} }; }, methods: { handleAge(evt) { console.log(evt.target.value); // 接收從子組件傳過來的當(dāng)前對象 } }, components: { 'ok-input': okInput } }; </script>
// 子組件.vue
<template> <div> <input v-model='numVal' @blur='handleChange'></input> </div> </template> <script type="text/ecmascript-6"> import {Input, Select, Option, Button} from 'element-ui'; import 'element-ui/lib/theme-default/index.css'; export default { props: { params: { type: Object, default: { type: '' } }, callback: {} }, data() { return { x: 'hah', numVal: '' }; }, methods: { handleChange(evt) { this.$emit('change', evt); // 將當(dāng)前對象 evt 傳遞到父組件 }, }, components: { 'el-input': Input, } }; </script>
總結(jié)
以上所述是小編給大家介紹的Vue2.0父子組件傳遞函數(shù)的教程詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue項(xiàng)目通過a標(biāo)簽下載圖片至zip包的示例代碼
在vue項(xiàng)目中,將圖片下載可使用流的形式,下載成單個(gè)圖片,或者將多個(gè)圖片下載至zip包,本文就是介紹使用a標(biāo)簽下載圖片的用法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10關(guān)于vue3 vuex4 store的響應(yīng)式取值問題解決
這篇文章主要介紹了vue3 vuex4 store的響應(yīng)式取值問題,在實(shí)際生活中遇到這樣一個(gè)問題:在頁面中點(diǎn)擊按鈕,數(shù)量增加,值是存在store中的,點(diǎn)擊事件值沒變,如何解決這個(gè)問題,本文給大家分享解決方法,需要的朋友可以參考下2022-08-08vue3 ElementUI 日期禁選當(dāng)日前當(dāng)日后三天后的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue3 ElementUI 日期禁選當(dāng)日前當(dāng)日后三天后的實(shí)現(xiàn)代碼,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05vue+elementUI組件tree如何實(shí)現(xiàn)單選加條件禁用
這篇文章主要介紹了vue+elementUI組件tree如何實(shí)現(xiàn)單選加條件禁用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09