簡體   English   中英

Vue.js-使用動態組件進行數據訪問

[英]Vue.js - data access with dynamic components

我有一個簡單的網頁,顯示結果列表,用戶可以在表格li樣式之間切換。

我得到了具有兩個動態組件的簡單Vue: results-arrayresults-list

它的工作原理就像一種魅力,除了當我從第一個組件切換到第二個組件時:我釋放了胡子中稱為的結果屬性(我得到沒有錯誤的空白值):

{{contact.img_path}} does not show anything

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

**更新**這里是一個簡化的jsfiddle可以嘗試: https ://jsfiddle.net/eywraw8t/151906/

我的文件 :

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>

您應該從Vue根實例的data部分中刪除關鍵contact ,或者在v-for迭代器中使用其他名稱(例如v-for="myContact in contacts"

更新

另外,您不應為模板使用script標簽,而應使用template ,因為Chrome會忽略非JavaScript script標簽。 解決方案-https://codepen.io/TMCDOS/pen/gjYWNY

解決方案是將兩個模板腳本移到#app div之外

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM