2016-08-02 102 views
0

我目前正在學習流星框架,現在我不明白爲什麼我的代碼不工作。我試圖創建一個名爲「time」的模板,其中有一個名爲「date」的變量,它使用了新的Date();在我的HTML文件上顯示日期和時間,但它不起作用。它顯示的是「現在的時間」,沒有顯示時間。使用模板助手顯示流星的時間和日期

這裏是我的HTML和JS文件(我試圖使它下面針對我的課程使用的第一個模板圖像相同的邏輯):

HTML:

<head> 
    <title>my_first_app</title> 
</head> 
<body> 
    <h1>Hello from Greece!</h1> 
    {{>time}} 
</body> 

<template name="time"> 
    <p>The time now is {{date}}</p> 
</template> 

的Javascript:

import { Template } from 'meteor/templating'; 
import { ReactiveVar } from 'meteor/reactive-var'; 
import './main.html'; 

var date = new Date(); 

Template.time.helpers({ 
    time: function(){ 
    return new Date(); 
    } 
}); 

回答

2

您只需要將模板中的助手名稱更改爲'時間'而不是'日期',或者減少您可以這樣做的含糊之處:

<head> 
    <title>my_first_app</title> 
</head> 
<body> 
<h1>Hello from Greece!</h1> 
{{>time}} 
</body> 
<template name="time"> 
    <p>The time now is {{timeVal}}</p> 
import { Template } from 'meteor/templating'; 
import { ReactiveVar } from 'meteor/reactive-var'; 

import './main.html'; 

var date = new Date(); 


Template.time.helpers({ 


timeVal: function(){ return new Date(); }});