首页 > 解决方案 > Vue.js - 使用动态组件访问数据

问题描述

我有一个显示结果列表的简单网页,用户可以在表格li样式之间切换。

我得到了带有两个动态组件的简单 Vue:results-arrayresults-list

它就像一个魅力,除非我从第一个组件切换到第二个组件:我丢失了在 mustache 中调用的结果属性(我得到了没有错误的空白值):

{{contact.img_path}} does not show anything

然而

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

** 更新 ** 这里有一个简化的 jsfiddle 试用:https ://jsfiddle.net/eywraw8t/151906/

我的文件 :

联系.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;
                });
        }
    }
});

索引.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>

标签: vue.jsvuejs2vue-component

解决方案


您应该contactdataVue 根实例的一部分中删除键,或者在v-for迭代器中使用另一个名称(例如v-for="myContact in contacts"

更新

此外,您不应该script为模板使用标签 -template而是使用标签,因为 Chrome 会忽略非 JavaScriptscript标签。解决方案 - https://codepen.io/TMCDOS/pen/gjYWNY


推荐阅读