LoginSignup
3
3

More than 3 years have passed since last update.

Vue〜子コンポーネントのdisabledを親から切り替える方法〜

Last updated at Posted at 2019-10-06

やりたい事

まず、前提としてこの様なbootstrapのセレクトボックスのcomponentを作り、それを「編集画面」と「閲覧画面」で使いまわしたい。
そのためセレクトボックスのcomponentの、disabledtrueorfalseを親コンポーネントで切り替えたい

画面イメージ

スクリーンショット 2019-10-06 23.11.51.png

コード

        <div class="form-group">
            <label for="exampleSelect1exampleFormControlSelect1">選択の例</label>
            <select class="form-control" id="exampleFormControlSelect1">
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
            </select>
        </div>

結論

コード

        <div class="form-group">
            <label for="exampleSelect1exampleFormControlSelect1">選択の例</label>
            <select class="form-control" id="exampleFormControlSelect1" :disabled = "isDisabledCheck(isDisabled)">
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
            </select>
        </div>
    export default {
        props: ['isDisabled'],
        methods: {
            isDisabledCheck : function (isDisabled) {
                return isDisabled === 'true'
            }
        }
    }

変更点

htmlに以下を追加
:disabled = "isDisabledCheck(isDisabled)"
vueのmethodsに以下を追加
        methods: {
            isDisabledCheck : function (isDisabled) {
                return isDisabled === 'true'
            }
        }

受け渡された値が文字列のtrueであれば、trueを返す。

悩んだ事

propsを直接適用する

:disabled = "isDisabled"

propsには文字列しか渡せ無かったため、直接適用しても効かなった。。。

computedかmethodsか

悩んだというか一瞬頭に浮かんだのですが、computedに引数を持たせられないので、methodsに決定

最後に

他に良い方法があればぜひコメントいただけると幸いです。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3