在app.locals上设置属性并调用app.set()有什么区别? [英] What is the difference between setting a property on app.locals and calling app.set()?

查看:114
本文介绍了在app.locals上设置属性并调用app.set()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Express,并考虑到保存配置样式数据的最佳位置。可用的选项可以是app.locals或app.set(settings)...所以:

  app.locals({config :{
name:'My App',
domain:'myapp.com',
viewPath:__dirname +'/ views',
viewEngine:'jade'
端口:3000
}});

app.set('view engine',app.locals.config.viewEngine ||'jade');

这也将允许我在我的意见中使用以下内容:

 < title>#{config.name}< / title> //< title> My App< / title> 

或者替代方法是使用app.set如下:

  app.set('name','My App'); 
app.set('domain','myapp.com');

...然后在视图中使用:

 < title>#{settings.name}< / title> 

我知道这两种方法都有效,但我很难确定哪种方法是更好的使用方法。目前,我倾向于使用app.locals,另外还有额外的'app'命名空间,我相信如果使用app.set,将会有更少的机会与未来的更新和其他模块发生冲突。


哇,所有的答案都是错误的,所以让我试一试。尽管其他人说赞同app.local参数与使用app.set()不同。观看

  app.js 
app.locals.foo ='bar';
app.set('baz','quz');


index.jade
块内容
dl
dt app.locals.foo ='bar';
dd \#{settings.foo} =#{settings.foo}
dd \#{foo} =#{foo}

dt app.set(' baz','quz')
dd \#{settings.baz} =#{settings.baz}
dd \#{baz} =#{baz}

如果你运行这段代码,你会看到,

  app.locals.foo ='bar'; 
#{settings.foo} =
#{foo} = bar
app.set('baz','quz')
#{settings.baz} = quz
#{baz} =

原因是设置 app.locals 设置视图用作其环境的对象的属性;从没有资格的意见将会如何看待。相反, app.set app.locals.settings 。你可以验证这一点,如果你用 app.locals.settings = {} 在上面的 app.locals.settings ,这将使#{settings.baz} 未定义。



那么你使用哪个?如果它不是基于响应( res.set )或全局配置( app.set )的应用程序设置,使用直接写入 app.locals


I'm in the process of learning Express - and thinking of the best place to save config style data. Options available are either in app.locals or app.set (settings)... so:

app.locals({ config: {
    name: 'My App',
    domain: 'myapp.com',
    viewPath: __dirname+'/views',
    viewEngine: 'jade'
    port: 3000
} });

app.set('view engine', app.locals.config.viewEngine || 'jade');

This would also allow me to use the following in my views:

<title>#{config.name}</title> // <title>My App</title>

Or the alternative is to use app.set like so:

app.set('name', 'My App');
app.set('domain', 'myapp.com');

... and then use this in the view:

<title>#{settings.name}</title>

I know both methods work, but I'm struggling to determine which is the better method to use. At the moment I'm leaning towards using app.locals, with the extra 'app' namespace as I believe there would be less chance of conflicts with future updates and other modules if using app.set.

解决方案

Wow, all of the answers are wrong, so let me give it a try. Despite what others say assinging to the app.local argument is different from using app.set(). Watch,

app.js
  app.locals.foo = 'bar';
  app.set('baz', 'quz');  


index.jade
  block content                            
    dl                                     
      dt app.locals.foo = 'bar';            
      dd \#{settings.foo} = #{settings.foo}
      dd \#{foo} = #{foo}                  

      dt app.set('baz', 'quz')             
      dd \#{settings.baz} = #{settings.baz}
      dd \#{baz} = #{baz}

If you ran this code, what you would see is,

app.locals.foo = 'bar';
  #{settings.foo} =
  #{foo} = bar
app.set('baz', 'quz')
  #{settings.baz} = quz
  #{baz} =

The reason for this is setting app.locals sets attributes of the object that the view uses as its environment; what the view will read from without qualification. Conversely, app.set sets attributes on app.locals.settings. You can verify this if you clobber app.locals.settings in the above with app.locals.settings = {}, which will make #{settings.baz} undefined.

So which do you use? If it's not an app setting based on the response (res.set) or global configuration (app.set), use the direct write to app.locals.

这篇关于在app.locals上设置属性并调用app.set()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆