2017-02-17 78 views
0

我使用laravel,這是我的VUE時刻JS fromnow功能VUE JS laravel

Vue.http.headers.common['X-CSRF-TOKEN'] = $("#token").attr("value"); 
     new Vue({ 
      el: '#notificationMenu', 
      data: { 
      patients: [] 
      }, 
      created: function(){ 
       this.getPatients(); 
       this.addUsers(); 
      }, 
      methods: { 
       getPatients: function(){ 
         $.getJSON("{{route('api_patients')}}", function(patients){ 
         this.patients = patients; 
        }.bind(this)); 
        setTimeout(this.getPatients, 1000); 
       }, 
       addUsers: function(){ 
       // var newItem = {'name':'asdfasdf','email':'sdfsdf','password':'sdfsdf'} 
       // var input = this.newItem; 
        this.$http.get('/govaccine/addUsers', '').then((response) => { 
        }, (response) => { 
        this.formErrors = response.data; 
        }); 

       } 
      } 

     }); 

,這裏是我的觀點

<li class=" notif unread" v-for="patient in patients"> 
         <a href="#"> 
         <div class="messageblock"> 
          <div class="message"><strong>@{{ patient.patient_lname}} @{{ patient.patient_fname}}'s</strong> next vaccination is due on 2017-03-23 for @{{patient.vaccine_name}} 
          </div> 
          <div class="messageinfo"> 
          <i class="icon-trophy"></i>2 hours ago 
          </div> 
      </div> 
     </a> 
    </li> 

我想我在列創建查看和使用moment.js的fromnow函數,我試圖尋找解決方案,但他們不適合我。

我使用了required('moment'),但這總是我得到的錯誤「require is not defined」,即使通過npm install -g browserify安裝了browserify。

你們有什麼辦法解決這個問題嗎?或者其他任何方法來獲得現在的時間,或者像碳排放人員那樣?

+0

您是否在使用Webpack或Browserify等構建系統? – motanelu

+0

查看此[問題](http://stackoverflow.com/questions/34308004/moment-js-with-vuejs)並查看[vue-moment](https://github.com/brockpetrie/vue -moment)項目。 – VincenzoC

+0

我仍然沒有我現在要嘗試 – Christian

回答

0

模板內部的上下文只會包含在Vue數據上聲明的變量。您的選擇是:

  1. 爲您的Vue數據添加對時刻的參考。
  2. 向你的Vue添加一個方法,它將爲你做計算。
  3. 添加一個計算來爲你做瞬間計算。

最後一條是推薦路線。