国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue如何在自定義組件中使用v-model

2024-05-06 15:34:14
字體:
來源:轉載
供稿:網友

v-model指令

所謂的“指令”其實就是擴展了HTML標簽功能(屬性)。

先來一個組件,不用vue-model,正常父子通信

<!-- parent --><template><div class="parent">  <p>我是父親, 對兒子說: {{sthGiveChild}}</p>  <Child @returnBack="turnBack" :give="sthGiveChild"></Child></div></template><script>import Child from './Child.vue';export default {  data() {    return {      sthGiveChild: '給你100塊'    };  },  components: {    Child  },  methods: {    turnBack(val) {      this.sthGiveChild = val;    }  }}</script>
<!-- child --><template><div class="child">  <p>我是兒子,父親對我說: {{give}}</p>  <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="returnBackFn">回應</a></div></template><script>export default {  props: {    give: String  },  methods: {    returnBackFn() {      this.$emit('returnBack', '還你200塊');    }  }}</script>

點擊回應后,父親對兒子說的話變成了兒子的回應。兒子收到的信息也變了,實現通信。

改用v-model

<!-- parent --><template><div class="parent">  <p>我是父親, 對兒子說: {{sthGiveChild}}</p>  <Child v-model="sthGiveChild"></Child></div></template><script>import Child from './Child.vue';export default {  data() {    return {      sthGiveChild: '給你100塊'    };  },  components: {    Child  }}</script>
<!-- child --><template><div class="child">  <p>我是兒子,父親對我說: {{give}}</p>  <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="returnBackFn">回應</a></div></template><script>export default {  props: {    give: String  },  model: {    prop: 'give',    event: 'returnBack'  },  methods: {    returnBackFn() {      this.$emit('returnBack', '還你200塊');    }  }}</script>

文案雖有不同,但是效果最終是一致的。

看看官方自定義組件的v-model

官方例子https://vuefe.cn/v2/api/#model

有這么一句話: 默認情況下,一個組件上的 v-model 會把 value 用作 prop 且把 input 用作 event。

嘗試把上邊子組件的例子改一下,也是跑的通的

<!-- child --><template><div class="child">  <p>我是兒子,父親對我說: {{value}}</p>  <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="returnBackFn">回應</a></div></template><script>export default {  props: {    value: String  },  methods: {    returnBackFn() {      this.$emit('input', '還你200塊');    }  }}</script>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 梅州市| 南平市| 黔南| 舞阳县| 淮阳县| 临潭县| 教育| 芜湖市| 华亭县| 吴旗县| 永德县| 高雄县| 天长市| 玛纳斯县| 云浮市| 祥云县| 丹江口市| 神农架林区| 永胜县| 宜兴市| 边坝县| 万年县| 休宁县| 渭南市| 馆陶县| 昌吉市| 抚远县| 英吉沙县| 大宁县| 中阳县| 海安县| 威信县| 肥乡县| 瓦房店市| 龙江县| 壶关县| 汉寿县| 宝兴县| 定西市| 静安区| 莱阳市|