分享Vue子組件接收父組件傳值的3種方式
更新時間:2022年03月29日 11:49:16 作者:什么都干的派森
這篇文章主要給大家分享的是Vue子組件接收父組件傳值的3種方式,主要通過聲明接收、接收數(shù)據(jù)的同時進行?類型限制、接收數(shù)據(jù)的同時對?數(shù)據(jù)類型、必要性、默認值?進行限制相關內容展開更多詳細的相關資料,需要的小伙伴可以參考一下
父組件代碼↓
<template> ?? ?<div> ?? ??? ?<div>父組件</div> ?? ??? ?<Student :name="name" :age="age"></Student> ?? ?</div> </template> <script> ?? ?// 引入組件 ?? ?import Student from './components/Student' ?? ? ?? ?export default { ?? ??? ?name: 'App', ?? ??? ?components: {Student,}, ?? ??? ?data() { ?? ??? ??? ?return { ?? ??? ??? ??? ?name: "張三", ?? ??? ??? ??? ?age: 18 ?? ??? ??? ?} ?? ??? ?} ?? ??? ? ?? ?} ?? ? </script>
1.簡單聲明接收
<template> ?? ?<div> ?? ??? ?<div>子組件</div> ?? ??? ?<h2>學生姓名:{{ name }}</h2> ?? ??? ?<h2>年齡:{{ age }}</h2> ?? ?</div> </template> <script> ?? ?export default { ?? ??? ?name: 'Student', ?? ??? ?data() { ?? ??? ??? ?return {} ?? ??? ?}, ?? ??? ? ?? ??? ?// 簡單聲明接收 ---------- ?? ??? ?props: ['name', 'age'] ?? ??? ?// --------------------- ?? ??? ? ?? ?} </script>
2.接收數(shù)據(jù)的同時進行 類型限制
<template> ?? ?<div> ?? ??? ?<div>子組件</div> ?? ??? ?<h2>學生姓名:{{ name }}</h2> ?? ??? ?<h2>年齡:{{ age }}</h2> ?? ?</div> </template> <script> ?? ?export default { ?? ??? ?name: 'Student', ?? ??? ?data() { ?? ??? ??? ?return {} ?? ??? ?}, ?? ??? ? ?? ??? ?// 接收數(shù)據(jù)的同時進行類型限制 --- ?? ??? ?props: { ?? ??? ??? ?name: String, ?? ??? ??? ?age: Number ?? ??? ?} ?? ??? ?// -------------------------- ?? ??? ? ?? ?} </script>
3.接收數(shù)據(jù)的同時對 數(shù)據(jù)類型、必要性、默認值 進行限制
<template> ?? ?<div> ?? ??? ?<div>子組件</div> ?? ??? ?<h2>學生姓名:{{ name }}</h2> ?? ??? ?<h2>年齡:{{ age }}</h2> ?? ?</div> </template> <script> ?? ?export default { ?? ??? ?name: 'Student', ?? ??? ?data() { ?? ??? ??? ?return {} ?? ??? ?}, ?? ??? ?// 接收數(shù)據(jù)的同時對 數(shù)據(jù)類型、必要性、默認值 進行限制 ----- ?? ??? ?props: { ?? ??? ??? ?name: { ?? ??? ??? ??? ?type: String,?? ? ?// name傳入類型必須為字符串 ?? ??? ??? ??? ?required: true ? ?// name設為必傳字段 ?? ??? ??? ?}, ?? ??? ??? ?age: { ?? ??? ??? ??? ?type: Number, ?? ??? ??? ??? ?default: 233 ? ? ?// 默認值 ?? ??? ??? ?} ?? ??? ?}, ?? ??? ?// ------------------------------------------------ ?? ?} </script>
到此這篇關于分享Vue子組件接收父組件傳值的3種方式的文章就介紹到這了,更多相關Vue子組件接收父組件傳值內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue3.2+ts實現(xiàn)在方法中可調用的擬態(tài)框彈窗(類el-MessageBox)
這篇文章主要介紹了vue3.2+ts實現(xiàn)在方法中可調用的擬態(tài)框彈窗(類el-MessageBox),這個需求最主要的是要通過方法去調用,為了像el-messagebox使用那樣方便,需要的朋友可以參考下2022-12-12淺析vue 函數(shù)配置項watch及函數(shù) $watch 源碼分享
這篇文章主要介紹了vue 函數(shù)配置項watch及函數(shù) $watch 源碼分享 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-11-11詳解Vue基于vue-quill-editor富文本編輯器使用心得
這篇文章主要介紹了Vue基于vue-quill-editor富文本編輯器使用心得,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01Vue項目打包成exe可執(zhí)行文件的實現(xiàn)過程(無瑕疵,完美版)
突然接到公司需求,說客戶想讓我們把項目直接打包,所以下面這篇文章主要給大家介紹了關于Vue項目打包成exe可執(zhí)行文件的實現(xiàn)過程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-11-11