在快速错误中使用把手部分

时间:2016-11-03 02:38:21

标签: javascript node.js express handlebars.js

事实上,我的英语很差。 我正在学习“使用Node和Express进行Web开发”,这是目录结构:directory structure

这是weather.handlebars:

<div class="weatherWidget">
  {{#each partials.weather.locations}}
  <div class="location">
    <h3>{{name}}</h3>
    <a href="{{forecastUrl}}">
      <img src="{{iconUrl}}" alt="{{weather}}">
       {{weather}}, {{temp}}
    </a>
  </div>
  {{/each}}
  <small>Source: <a href="http://www.wunderground.com">Weather Underground</a></small>
</div>

这是home.handlebars:

<h1>Welcome to Meadowlark Travel</h1>
{{> weather}}

我创建了一个&#39; lib / weather.js&#39;提供数据:

function getWeatherData(){
return {
    locations: [
        {
            name: 'Portland',
            forecastUrl: 'http://www.wunderground.com/US/OR/Portland.html',
            iconUrl: 'http://icons-ak.wxug.com/i/c/k/cloudy.gif',
            weather: 'Overcast',
            temp: '54.1 F (12.3 C)'
        },
        {
            name: 'Bend',
            forecastUrl: 'http://www.wunderground.com/US/OR/Bend.html',
            iconUrl: 'http://icons-ak.wxug.com/i/c/k/partlycloudy.gif',
            weather: 'Partly Cloudy',
            temp: '55.0 F (12.8 C)'
        },
        {
            name: 'Manzanita',
            forecastUrl: 'http://www.wunderground.com/US/OR/Manzanita.html',
            iconUrl: 'http://icons-ak.wxug.com/i/c/k/rain.gif',
            weather: 'Light Rain',
            temp: '55.0 F (12.8 C)'
        }
    ]
};
}

exports.weather = getWeatherData;

然后是meadowlark.js,这是主文件:

var express = require('express'),
exphbs  = require('express-handlebars'),
person = require('./lib/person.js'),
getWeather = require('./lib/weather.js');

var app = express();
······
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

······
app.use(function(req, res, next){
  if(!res.locals.partials){
     res.locals.partials = {};
  }

  res.locals.partials.weather = getWeather.weather();
    //console.log(res.locals.partials.weather.locations)
    next();
});

app.get('/',function(req,res){
  //console.log(res.locals.partials.weather.locations)
  res.render('home');
});

······

但运行时:  cmd print

当我将weather.handlebars内容复制到home.handlebars中时这样:

<h1>Welcome to Meadowlark Travel</h1>
<div class="weatherWidget">
{{#each partials.weather.locations}}
  <div class="location">
    <h3>{{name}}</h3>
    <a href="{{forecastUrl}}">
      <img src="{{iconUrl}}" alt="{{weather}}">
      {{weather}}, {{temp}}
    </a>
  </div>
{{/each}}
<small>Source: <a href="wunderground.com">Weather
Underground</a></small>
</div>

运行良好!

我想知道为什么,我需要你的帮助,谢谢!

0 个答案:

没有答案