日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue --- 从模块从父元素获取数据

發布時間:2023/12/10 vue 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue --- 从模块从父元素获取数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

vue的精彩之處在于其組件的可復用性.下面談談組件(component)如何從父元素獲取數據

模塊引用

首先說說,如何引用模塊

<div id="app"><my-component ></my-component> </div> <script src=“unpkg.com/vue/dist/vue.min.js”> </ script> // vue的CDN <script>Vue.component('my-component',{ // 模塊的聲明,注意:一定要在Vue實例掛載之前template: '<div> {{ message }}</div>' // 模塊的模板, 用于替換<my-component>中的內容})var app = new Vue({el:"#app",}) </script>

此時,自定義模塊my-component就在父元素內了.

props

vue提供了一個props屬性,它的作用是將父元素的數據,正向傳遞給子模塊,例如:

// 改變<my-component>,將message數據(父元素)綁定到該模塊上 <div id = "app"><input type="text" v-model.lazy="msg" placeholder="你有什么想對我說"><my-component message="來自父元素的消息" :warning-one="msg"></my-component> </div> // 在模塊聲明中加一個props屬性,其值是一個數組,注意在<my-component>中的warning-one,對應到JS聲明中是warningOne(駝峰) Vue.component('my-component',{props:[''message","warningOne"], // 繼承父元素的數據template:'<div> {{ message}}: {{warningOne}}</div>', // 模板用于顯示 }); var app= new Vue({el: "#app",data:{msg: ' '} });

看下效果:

輸入:晚上一起看電影(在乎你)吧~

老規矩,整體代碼如下:

<!DOCTYPE html> <html><head><meta charset="utf-8"> </head><body><div id="app"><input rtpe="text" v-model.lazy ="msg" placeholder="你有什么想對我說"><my-component message="來自父組件的消息" :warning-one="msg"></my-component></div><script src="https://unpkg.com/vue/dist/vue.min.js"></script><script >Vue.component('my-component',{props: ['message','warningOne'],template: '<div>{{ message }}: {{warningOne}}</div>'});var app = new Vue({el:"#app",data:{msg:''}})</script> </body></html>

總結

以上是生活随笔為你收集整理的vue --- 从模块从父元素获取数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。