亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Vue.js - 綁定到組件的道具未顯示正確的結果

Vue.js - 綁定到組件的道具未顯示正確的結果

喵喵時光機 2022-05-22 16:03:27
我的 Vue.js 應用程序中有這個 SwitchButton 組件,它有一個自定義事件“切換”。單擊(切換)按鈕時,切換事件將 isEnabled 設置為 false 或 true。沒有問題,但是當我將父組件中的 isEnabled 綁定到 SwitchButton 子組件以設置其初始值時,它似乎不起作用。父組件中使用的代碼(將 isEnabled true 作為初始值)<SwitchButton v-model="distSwitch" :isEnabled="true">    <label slot="left">{{ $t('general.dealer') }}</label>    <label slot="right">{{ $t('general.wholesale') }}</label></SwitchButton>開關按鈕組件:<template>    <div class="switch-button-control">        <div class="switch-button-label">            <slot name="left"></slot>        </div>        <div class="switch-button" :class="{'enabled': isEnabled}" @click="toggle">            <div class="button"></div>        </div>        <div class="switch-button-label">            <slot name="right"></slot>        </div>    </div></template><script>export default {    name: 'SwitchButton',    model: {        prop: 'isEnabled',        event: 'toggle'    },    props: {        isEnabled: {            type: Boolean,            required: true        }    },    methods: {        toggle() {            this.$emit("toggle", !this.isEnabled);        }    },    created() {        /*eslint-disable no-console*/        console.log('isEnabled', this.isEnabled)    }}</script>我希望創建的鉤子中的 console.log 輸出“isEnabled > true”,但它輸出“false”我在這里想念什么?
查看完整描述

1 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

由于該屬性的值isEnabled是由硬編碼設置的true,因此從子組件更改它是行不通的。


在父組件中初始化數據變量并設置為屬性可以在這里提供幫助。


模板:


<SwitchButton v-model="distSwitch" :isEnabled="isEnabled" @toggle="toggleIsEnabled">

    <label slot="left">{{ $t('general.dealer') }}</label>

    <label slot="right">{{ $t('general.wholesale') }}</label>

</SwitchButton>

數據:


data:()=>{

  return {

    isEnabled:true

  }

}

在該部分中創建一個方法methods:


toggleIsEnabled:function(value){

   this.isEnabled = value;

}

SwitchButton使用自定義事件發射器從組件發出的值toggle將觸發父組件中的回調函數toggleIsEnabled。


查看完整回答
反對 回復 2022-05-22
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號