vue頁面結構
在做項目的時候常常有這樣的一個情況,這個頁面的數(shù)據(jù)(比如:id號)要帶到另一個頁面去查詢某個數(shù)據(jù)的詳情等,傳統(tǒng)的作法不是在url上加參數(shù),cookie或者是現(xiàn)在H5的“sessionStorage”和“l(fā)ocalStorage”上賦值,這是頁面之間傳遞的方法。
隨著Angularjs,React,Vue的流行組件式的開發(fā)方式成為另一種不錯的解決方案。
最近就有一些小伙伴問我,vue組件之間是如何傳遞參數(shù)的?其實vue是有三種方式可以組件之間傳遞數(shù)據(jù)(props,組件通信,slot),這次就說第一種方式如下:
a父組件內容:
引入b子組件import b form 'b.vue'
components: {'b-div': b} // 注冊,只能在當前a組件里使用 <b-div :propsname='datas(向子組件傳遞的參數(shù))'></b-div>
b子組件內容:
<template> <div>{{propsname}}</div> </template> export default{ props: ['propsname'], data(){} }
只要在a組件中的datas的值一直在改變,在b子組件中props就會實時監(jiān)聽propsname的變化,在頁面上也會做出相應的渲染,使用方式也是{{propsname}}。
PS:下面給大家介紹下vue父子組件間傳值(props)
先定義一個子組件,在組件中注冊props
<template> <div> <div>{{message}}(子組件)</div> </div> </template> <script> export default { props: { message: String //定義傳值的類型<br> } } </script> <style> </style>
在父組件中,引入子組件,并傳入子組件內需要的值
<template> <div> <div>父組件</div> <child :message="parentMsg"></child> </div> </template> <script> import child from './child' //引入child組件 export default { data() { return { parentMsg: 'a message from parent' //在data中定義需要傳入的值 } }, components: { child } } </script> <style> </style>
這種方式只能由父向子傳遞,子組件不能更新父組件內的data
總結
以上所述是小編給大家介紹的vue父組件向子組件(props)傳遞數(shù)據(jù)的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com