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ū)動(dòng)。
學(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)站的支持!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com