2016-08-25 7 views
0

私は、vue.js、vue-resource、vue-mdl、およびGoogleマテリアルデザインライトを使用してWebアプリケーションを構築しています。 JSコンパイルは、lackvel elixirからwebpackを使用して実行されます。 このアプリケーションでは、残りのAPI(Django Rest Framework)から返された配列から各オブジェクトのテーブル行をレンダリングする必要があります。私はvue.jsを使用してコンテンツをレンダリングするために、HTML内部次のコードを行った:Vue.jsループがコンテンツをレンダリングしていない

<tr v-for="customer in customers"> 
    <td class="mdl-data-table__cell--non-numeric">{{ customer.status }}</td> 
    <td class="mdl-data-table__cell--non-numeric">{{ customer.name }}</td> 
    <td class="mdl-data-table__cell--non-numeric">{{ customer.company }}</td> 
<tr> 

これは、テーブルの行として配列内のすべてのオブジェクトをレンダリングするべきです。私はまたこのようなテンプレートのタグで上記をラップしようとしました:

<template v-for="customer in customers"> 
<tr> 
    <td class="mdl-data-table__cell--non-numeric">{{ customer.status }}</td> 
    <td class="mdl-data-table__cell--non-numeric">{{ customer.name }}</td> 
    <td class="mdl-data-table__cell--non-numeric">{{ customer.company }}</td> 
</tr> 
</template> 

これも変更されませんでした。 私はまた、vueインスタンスのready()関数の中で配列をハードコードしようとしましたが、これは役に立たなかったです。

window._ = require('lodash'); 
require('material-design-lite'); 
window.Vue = require('vue'); 
require('vue-resource'); 
var VueMdl = require('vue-mdl'); 
Vue.use(VueMdl.default); 
const app = new Vue({ 
    el:'body', 
    ready: function(){ 
     //this.getCustomerList(); 
     this.customers = [ 
      { name: "Peter", status: "true", company: "Company 1"}, 
      { name: "John", status: "false", company: "Company 2"} 
     ] 

    }, 
    data: { 
     customers: [], 
     response: null, 
     messages: [] 
    }, 
    methods: { 
     getCustomerList: function() 
     { 
      this.$http({url: '/api/customers/', method: 'GET'}).then(function(response){ 
       //Success 
       this.customers = response.data 
       console.log(response.data) 
      }, 
      function(response){ 
       console.log(response) 
      }) 
     } 
    } 
}) 

これに上記の変更のいずれか変更されません:

window._ = require('lodash'); 
require('material-design-lite'); 
window.Vue = require('vue'); 
require('vue-resource'); 
var VueMdl = require('vue-mdl'); 
Vue.use(VueMdl.default); 
const app = new Vue({ 
    el:'body', 
    ready: function(){ 
     //this.getCustomerList(); 

    }, 
    data: { 
     customers: [ 
      { name: "Peter", status: "true", company: "Company 1"}, 
      { name: "John", status: "false", company: "Company 2"} 
     ], 
     response: null, 
     messages: [] 
    }, 
    methods: { 
     getCustomerList: function() 
     { 
      this.$http({url: '/api/customers/', method: 'GET'}).then(function(response){ 
       //Success 
       this.customers = response.data 
       console.log(response.data) 
      }, 
      function(response){ 
       console.log(response) 
      }) 
     } 
    } 
}) 

私もちょうどGoogleのMDLクラスのいずれかが適用されていないプレーンなHTMLテーブルを作成しようとしましたが、これはありませんどんな結果も与えない。 コンソールにthis.customersを記録すると、実際にデータが含まれていることが示されますが、レンダリングされていないためです。何故ですか?私は間違って何をしていますか?

+0

コンソールにエラーがありますか?あなたの ''グループには '

'タグがありますか? –

+0

コンソールには何も載っていません。もちろん、 ''

'' 'と' '' ''で囲まれています。 –

答えて

1

コードのスニペットは、期待どおりに機能します。あなたが言及したライブラリへのCDN参照を追加しましたが、私は何もしません。私はこれをあなたがあなたの問題を再現する変更を見つけることができるかどうかを知るための出発点として提供します。ここで

const app = new Vue({ 
 
    el: 'body', 
 
    ready: function() { 
 
    //this.getCustomerList(); 
 
    this.customers = [{ 
 
     name: "Peter", 
 
     status: "true", 
 
     company: "Company 1" 
 
    }, { 
 
     name: "John", 
 
     status: "false", 
 
     company: "Company 2" 
 
    }] 
 
    }, 
 
    data: { 
 
    customers: [], 
 
    response: null, 
 
    messages: [] 
 
    } 
 
})
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/vue-resource/0.9.3/vue-resource.min.js"></script> 
 
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> 
 
<link rel="stylesheet" href="https://code.getmdl.io/1.2.0/material.indigo-pink.min.css"> 
 
<script defer src="https://code.getmdl.io/1.2.0/material.min.js"></script> 
 
<script src="https://rawgit.com/posva/vue-mdl/develop/dist/vue-mdl.min.js"></script> 
 
<div class="mdl-grid"> 
 
    <div class="mdl-cell mdl-cell--3-col"> 
 
    <button id="create-customer" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" @click="$refs.createCustomer.open"> 
 
     Create customer 
 
    </button> 
 
    </div> 
 
    <div class="mdl-cell mdl-cell--3-col"> 
 
    <button id="convert-reseller" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect"> 
 
     Convert to reseller 
 
    </button> 
 
    </div> 
 
    <div class="mdl-cell mdl-cell--3-col"> 
 
    <button id="remove-customer" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect"> 
 
     Remove Customer 
 
    </button> 
 
    </div> 
 
    <div class="mdl-cell mdl-cell--3-col"> 
 
    <button id="change-status" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect"> 
 
     Change status 
 
    </button> 
 
    </div> 
 
</div> 
 
<div class="mdl-grid"> 
 
    <div class="mdl-cell mdl-cell--12-col"> 
 
    <table class="mdl-data-table mdl-js-data-table mdl-data-table--selectable" style="width:100%;"> 
 
     <thead> 
 
     <tr> 
 
      <th class="mdl-data-table__cell--non-numeric">Status</th> 
 
      <th class="mdl-data-table__cell--non-numeric">Customer name</th> 
 
      <th class="mdl-data-table__cell--non-numeric">Company</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr v-for="customer in customers"> 
 
      <td class="mdl-data-table__cell--non-numeric">{{ customer.status }}</td> 
 
      <td class="mdl-data-table__cell--non-numeric">{{ customer.name }}</td> 
 
      <td class="mdl-data-table__cell--non-numeric">{{ customer.company }}</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
    </div> 
 
</div>

+0

通常、スクリプトが '' '' 'にある場合、または' '' '' 'の一番下にある場合は違いはありません。あなたの例では、それらは '' '

' 'の上にありますが、違いはありませんか? –

+0

以下の回答を参照してください。コメントとしてコードを投稿することはできません。 –

+0

@Kenneth_H投稿したマークアップのほとんどを使用するようにスニペットを更新しました。 :)しかし、それはまだすべての作品。 –

0

私はライブのコードを持っているよう<table><body>内側と底にすべてのスクリプトを持つと私の<head>の完全な出力です。 お客様

お客様

     <div class="mdl-grid"> 
          <div class="mdl-cell mdl-cell--3-col"> 
           <button id="create-customer" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect"  @click="$refs.createCustomer.open"> 
            Create customer 
           </button> 
          </div> 
          <div class="mdl-cell mdl-cell--3-col"> 
           <button id="convert-reseller" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect"> 
            Convert to reseller 
           </button> 
          </div> 
          <div class="mdl-cell mdl-cell--3-col"> 
           <button id="remove-customer" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect"> 
            Remove Customer 
           </button> 
          </div> 
          <div class="mdl-cell mdl-cell--3-col"> 
           <button id="change-status" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect"> 
            Change status 
           </button> 
          </div> 
         </div> 
         <div class="mdl-grid"> 
          <div class="mdl-cell mdl-cell--12-col"> 
           <table class="mdl-data-table mdl-js-data-table mdl-data-table--selectable" style="width:100%;"> 
            <thead> 
             <tr> 
              <th class="mdl-data-table__cell--non-numeric">Status</th> 
              <th class="mdl-data-table__cell--non-numeric">Customer name</th> 
              <th class="mdl-data-table__cell--non-numeric">Company</th> 
             </tr> 
            </thead> 
            <tbody> 
             <tr v-for="customer in customers"> 
              <td class="mdl-data-table__cell--non-numeric">{{ customer.status }}</td> 
              <td class="mdl-data-table__cell--non-numeric">{{ customer.name }}</td> 
              <td class="mdl-data-table__cell--non-numeric">{{ customer.company }}</td> 
             </tr> 
            </tbody> 
           </table> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
     </main> 
     <script type="text/javascript" src="/static/js/customer_list.js"></script> 
    </body> 
</html> 
0

それは今働いているようです。 内部にapp.jsがありましたconst app = new Vue({ el: 'body' }) 何らかの理由で、私のメソッドが正常に機能しましたが、customer_list.js内に作成したものと競合していました。

関連する問題