相关文章推荐
俊逸的青蛙  ·  在DataStudio中创建ODPS ...·  3 天前    · 
暗恋学妹的吐司  ·  scanpy 调用 sklearn ...·  10 月前    · 
儒雅的硬币  ·  React-Hook-Form 中 ...·  1 年前    · 
从未表白的匕首  ·  java double取整数 ...·  1 年前    · 
不拘小节的羽毛球  ·  Tableau CA或者CDA ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
<template>
    <select class="form-control" v-model="selected" :required @change="changeLocation">
        <option :selected>Choose Province</option>
        <option v-for="option in options" v-bind:value="option.id" >{{ option.name }}</option>
    </select>
</template>

I use this : <option :selected>Choose Province</option> to selected

But whene executed, on the gulp watch exist error

The error like this :

ERROR in ./~/vue-loader/lib/template-compiler.js?id=data-v-53777228!./~/vue-load er/lib/selector.js?type=template&index=0!./resources/assets/js/components/bootst rap/LocationBsSelect.vue Module build failed: SyntaxError: Unexpected token (28:4)

Seems my step is wrong

How can I solve it?

github.com/vuejs/vue/issues/308 -github.com/vuejs/vue/issues/308#issuecomment-314248619 - this fixed my issue – V H Jul 24, 2019 at 14:46

You are binding properties to nothing. :required in

<select class="form-control" v-model="selected" :required @change="changeLocation">

and :selected in

<option :selected>Choose Province</option>

If you set the code like so, your errors should be gone:

<template>
  <select class="form-control" v-model="selected" :required @change="changeLocation">
    <option>Choose Province</option>
    <option v-for="option in options" v-bind:value="option.id" >{{ option.name }}</option>
 </select>
</template>

Getting the select tags to have a default value

  • you would now need to have a data property called selected so that v-model works. So,

    data () { return { selected: "Choose Province"
  • If that seems like too much work, you can also do it like:

    <template>
      <select class="form-control" :required="true" @change="changeLocation">
       <option :selected="true">Choose Province</option>
       <option v-for="option in options" v-bind:value="option.id" >{{ option.name }}</option>
      </select>
    </template>
    
  • You can use the v-model approach if your default value depends on some data property.

  • You can go for the second method if your default selected value happens to be the first option.

  • You can also handle it programmatically by doing so:

    <select class="form-control" :required="true">
      <option 
       v-for="option in options" 
       v-bind:value="option.id"
       :selected="option == '<the default value you want>'"
      >{{ option }}</option>
    </select>
                    @Amesh Venugopal, I tried this <option v-for="recipe in recipes" :selected="option == 'Sugar Cookies (Default)'">{{ recipe }}</option> but the first option is still blank in the select list.  Would you post fiddle showing how this works?
    – Chris22
                    Aug 6, 2018 at 21:41
    

    The simplest answer is to set the selected option to true or false.

    <option :selected="selectedDay === 1" value="1">1</option>
    

    Where the data object is:

    data() {
        return {
            selectedDay: '1',
            // [1, 2, 3, ..., 31]
            days: Array.from({ length: 31 }, (v, i) => i).slice(1)
    

    This is an example to set the selected month day:

    <select v-model="selectedDay" style="width:10%;">
        <option v-for="day in days" :selected="selectedDay === day">{{ day }}</option>
    </select>
    

    On your data set:

    data() { selectedDay: 1, // [1, 2, 3, ..., 31] days: Array.from({ length: 31 }, (v, i) => i).slice(1) mounted () { let selectedDay = new Date(); this.selectedDay = selectedDay.getDate(); // Sets selectedDay to the today's number of the month This works, but not because of the :selected bindings. Using v-model means that any checked, value, and selected attributes are ignored. Please see basic usage here, where this ignoring of specific attributes is described - vuejs.org/v2/guide/forms.html – Lucas Jul 8, 2018 at 4:07 @LucasMorgan, when using a component whose list of options is dynamically obtained on mount, the v-model does not seem to work. This solution is the only one that I have found so far working in that scenary. – jap1968 Aug 30, 2018 at 12:15 A person could also shorten that by some logic similar to this: this.days = [...Array(31)]; and then <option v-for="(_, day) in days" :selected="selectedDay === day">{{ day }}</option>. Good luck out there. – agm1984 Oct 23, 2018 at 20:13 With that said, I think a person should also probably use a library for date picking because the person will enter into a world of pain if they try to start accounting for days in the month and leap years. It's a well-characterized problem possibly better left to a library that already has unit tests and a nice API. – agm1984 Oct 23, 2018 at 20:21
    <select v-model="challan.warehouse_id">
    <option value="">Select Warehouse</option>
    <option v-for="warehouse in warehouses" v-bind:value="warehouse.id"  >
       {{ warehouse.name }}
    </option>
    

    Here "challan.warehouse_id" come from "challan" object you get from:

    editChallan: function() {
        let that = this;
        axios.post('/api/challan_list/get_challan_data', {
        challan_id: that.challan_id
     .then(function (response) {
        that.challan = response.data;
     .catch(function (error) {
        that.errors = error;
                    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
    – thewaywewere
                    Jun 14, 2017 at 9:13
    
    <template>
        <select class="form-control" v-model="selected" required @change="changeLocation">
            <option selected>Choose Province</option>
            <option v-for="option in options" v-bind:value="option.id" >{{ option.name }}</option>
        </select>
    </template>
    Using your example gives an attribute called "selected" with the value selected... selected= "selected" – Uriahs Victor May 22, 2020 at 22:16 return { article: {title: 'aaaaa', 'categoriesIds': [1,3], ...}, selectCategories: {1: 'xxx', 2: 'www', 3: 'yyy', 4: 'zzz'},

    template

    <div class="form-group">
         <label for="content">Categories</label>
         <select name="categories" 
             v-model="article.categoriesIds" 
             id="categories" 
             multiple 
             class="form-control col-md-5" 
             size="6">
             <option v-for="(category, key) in selectCategories" 
                 :key="key" 
                 v-bind:value="key">{{category}}</option>
         </select>
    

    Selected items are binded to the article.categoriesIds array.

    Another way, which I often find more reliable, is you could add a directive to your app.js or wherever you are initiating your VueJS, eg:

    Vue.directive('attr', (el, binding) => {
      if (binding.value === true) binding.value = ''
      if (binding.value === '' || binding.value) {
        el.setAttribute(binding.arg, binding.value)
    

    You can then utilise v-attr to set an attribute, eg:

    <option value="Western Australia" v-attr:selected="form.state == 'Western Australia'">Western Australia</option>
    

    So as I understand the main goal is to set "Choose Province" as the default. I tried other options but the simple one worked for me the best:

    <template>
        <select class="form-control" v-model="selected" :required @change="changeLocation">
            <option>Choose Province</option> # just an option with no selected or assigned v-model
            <option v-for="option in options" v-bind:value="option.id" >{{ option.name }}</option>
        </select>
    </template>  
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •