自定义视图/动作/控制器在 Sails JS 中不起作用 [英] Custom view/action/controller not working in Sails JS

查看:38
本文介绍了自定义视图/动作/控制器在 Sails JS 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定是做错了什么,但我看不到...基本上,如果我通过 sails generate controller products 和文件 /controllers/ProductsController 创建控制器/模型 我在索引操作中添加了一些变量,例如:

I must be doing something wrong, but I can't see... Basically if I create a controller/model through sails generate controller products and in the file /controllers/ProductsController I add some variables to the index action such as:

index: function(req, res) {

    return res.view({
        myOne: 'World?',
        myvar: 'hello???',
        title: 'Yeap'
    })

},

当我检查 localhost:1337/products 时,肯定会在模板中打印变量.请注意,我没有添加自定义视图,我的模板文件位于 views/products/index.jade(我使用的是 Jade 而不是 EJS).但是,如果我在 /config/routes 中创建自定义视图,例如:

and when I check localhost:1337/products surely enough the variables are printed within the template. Note that I haven't added a custom view and my template file is located at views/products/index.jade (I'm using Jade rather than EJS). However if I create a custom view in /config/routes such as:

'/custom': {

    view: 'custom',
    controller: 'ProductsController',
    action: 'customAction'

}

在我的 ProductsController 中,我有一个与之前描述的非常相似的操作:

and in my ProductsController I have a very similar action as previously described:

customAction: function(req, res) {

    return res.view({
        myOne: 'Hello?',
        myvar: 'World???',
        title: 'Yeap'
    })

},

变量未打印在我的模板中.模板被调用好了,控制器和动作也是如此(看起来如此).谁能解释一下我做错了什么?

The variables are not printed in my template. The template is being called alright so is the Controller and Action (so it seems). Can anyone explain what I'm doing wrong?

推荐答案

如果您想通过以下方式访问视图,您可以选择将视图命名为与您的操作 customAction.jade 相同的名称:

You have a choice between naming the view the same name as your action customAction.jade if you want to access it via:

index: function(req, res) {

  return res.view({
    myOne: 'World?',
    myvar: 'hello???',
    title: 'Yeap'
  })

},

或者您可以使用以下语法访问相同的视图/模板(例如 index.jade):

or you can access the same view/template (e.g. index.jade) by using this syntax:

customAction: function(req, res) {

  return res.view('products/index', {
    myOne: 'World?',
    myvar: 'hello???',
    title: 'Yeap'
  })

},

我有一个使用 ejs 的工作存储库这里.

I have a working repo using ejs here.

这篇关于自定义视图/动作/控制器在 Sails JS 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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