1

I want to initialize one variable with another variable in Vue js Here I want to initialize 'multiple' variable with 'select' variable

    <div id="myapp">
     <select v-model="select">
            <option selected>A</option>
            <option>B</option>
            <option>C</option>
        </select>
        <span>Selected {{select}}</span>
        <hr>
        <select v-model="multiple" multiple>
            <option selected>A</option>
            <option>B</option>
            <option>C</option>
        </select>
        <span>Multiple Selected :{{ multiple | JSON}}</span>
    </div>

<script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script>   
<script>
    var vm=new Vue({
       el:'#myapp',
       data:{
                select:'B',
                multiple:select
        }
})
</script>`

Here I get an error like: Uncaught ReferenceError: select is not defined

1 Answer 1

1

You can't access data properties like this in the data() method.

If you want to do this (though it seems a bit useless in this example), you will have to define a variable outside, and then reference it in data()

<script>
    var select = 'B'
    var vm=new Vue({
       el:'#myapp',
       data:{
                select: select,
                multiple:select
        }
})
</script>`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.