简体   繁体   中英

Vue.js - data access with dynamic components

I have simple web page that shows a result list and users can switch between table or li style.

I got simple Vue with two dynamic components : results-array and results-list .

It works like a charm except when I switch from the first component to the second one: I loose results properties called in mustache (I got blank values with no error) :

{{contact.img_path}} does not show anything

whereas

<img :src="contact.img_path" /> works great

** UPDATE ** Here a simplified jsfiddle to try out: https://jsfiddle.net/eywraw8t/151906/

My files :

contact.js

var list = Vue.component('results-list', {
    template: '#results-list-template',
    props: ['display_mode', 'contacts']
});

var table = Vue.component('results-array', {
    template: '#results-array-template',
    props: ['display_mode', 'contacts'],
});

const app = new Vue({
    el: '#app',
    router,
    data: {
        currentResultsView: 'results-array',
        display_mode:       'array',
        contacts:           [],
        contact:            { company: {}, phones: {}, section: {}, schedule_type: {}}, // Declaring Reactive Properties
        phone:              {} // Declaring Reactive Properties
        },
    created () {
      this.navigate(this.$route.query.page);
    },

    methods: {
        changeDisplay: function(event, type){
            this.currentResultsView = (type == "array") ? "results-array" : "results-list";

            this.display_mode = type;
            console.log('contacts', this.contacts[0].lastname) // WORKS !
            },
        navigate: function(page){
            var page = page > 0 ? page : 1;
            axios.get('/', {params: {page: page, fulltext_search: this.fulltext_search, sort_dir: this.sort_dir}})
                .then((response) => {
                    this.contacts = response.data.entries;
                });
        }
    }
});

index.html

<ul>
  <li @click="changeDisplay($event, 'hcard')" :class="{active:display_mode == 'hcard'}">Carte de visite</li>
  <li @click="changeDisplay($event, 'vcard')" :class="{active:display_mode == 'vcard'}">Vignette verticale</li>
  <li @click="changeDisplay($event, 'array')" :class="{active:display_mode == 'array'}">Tableau</li>
</ul>

<div id="app">

  <script type="text-x-template" id="results-array-template">
    <table>
      <tr>
        <th></th>
        <th>Firstname</th>
        <th>Lastname</th>
      </tr>
      <tr v-for="contact in contacts">
        <td><img :src="contact.img_path" class="contact_img" /></td>
        <td>{{ contact.firstname }}</td>
        <td>{{ contact.lastname }}</td>
      </tr>
    </table>
  </script>

  <script type="text-x-template" id="results-list-template">
    <ul>
      <li v-for="contact in contacts">
        {{contact.firstname}} <!-- **Does not show anything** -->
        <img :src="contact.img_path" /> <!-- **Works great!** -->

      </li>
    </ul>
  </script>

  <div id="results" :class="display_mode" class="clearfix">
    <keep-alive>
      <component v-bind:is="currentResultsView" :display_options="display_options" :display_mode="display_mode" :contacts="contacts" ></component>
    </keep-alive>
  </div>

</div>

You should either remove the key contact from the data part of your Vue root instance, or use another name in the v-for iterator (eg v-for="myContact in contacts" )

UPDATE

Also, you should not use script tags for the template - use template instead, because Chrome ignores non-JavaScript script tags. The solution - https://codepen.io/TMCDOS/pen/gjYWNY

解决方案是将两个模板脚本移到#app div之外

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM