简体   繁体   English

Vue.js-使用动态组件进行数据访问

[英]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. 我有一个简单的网页,显示结果列表,用户可以在表格li样式之间切换。

I got simple Vue with two dynamic components : results-array and results-list . 我得到了具有两个动态组件的简单Vue: results-arrayresults-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/ **更新**这里是一个简化的jsfiddle可以尝试: https ://jsfiddle.net/eywraw8t/151906/

My files : 我的文件 :

contact.js 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 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" ) 您应该从Vue根实例的data部分中删除关键contact ,或者在v-for迭代器中使用其他名称(例如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. 另外,您不应为模板使用script标签,而应使用template ,因为Chrome会忽略非JavaScript script标签。 The solution - https://codepen.io/TMCDOS/pen/gjYWNY 解决方案-https://codepen.io/TMCDOS/pen/gjYWNY

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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