国产探花免费观看_亚洲丰满少妇自慰呻吟_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>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 拜泉县| 阿瓦提县| 阳西县| 泗阳县| 临泽县| 昌图县| 保亭| 天门市| 河北区| 松原市| 安康市| 德兴市| 宁波市| 许昌市| 常熟市| 利川市| 甘泉县| 晋中市| 都安| 疏附县| 陵水| 遂宁市| 喀喇| 永安市| 永泰县| 望都县| 财经| 新余市| 唐海县| 四平市| 福泉市| 瓦房店市| 牟定县| 广东省| 炉霍县| 江阴市| 石屏县| 桐梓县| 泸溪县| 孝昌县| 米林县|