如何在Sails 1.0中使用出口和action2设置动态viewTemplatePath?

问题描述 投票:1回答:1

我正在尝试创建一个操作,该操作基于url中传递的参数动态加载视图

下面是我的routes.js

'GET faq/:alias': {actions:'faq'}

我的faq动作

module.exports = {
    friendlyName: 'FAQ Pages',
    inputs: {
        alias: {
            description: 'the page url',
            type: 'string'
        }
    },
    exits: {
        success: {
          statusCode: 200,
        },
        notFound: {
          responseType: 'notFound',
        }
    },
    fn: async function(inputs, exits) {
        console.log('static', inputs.alias);

        //something like this - set view tempalatePath
        //exits.success.viewTemplatePath = 'faqs/' + inputs.alias;
        //load view if exist else throw notFound

        return exits.success();
    }
};

我所有的常见问题都在一个文件夹中,我将使用require('fs').existsSync()检查物理文件是否存在,然后加载并加载它

node.js sails.js
1个回答
0
投票

在您使用的action2格式中,您必须使用throw路由到备用出口。参见:

https://sailsjs.com/documentation/concepts/actions-and-controllers

请勿与此处的文档混淆:

https://sailsjs.com/documentation/reference/response-res/res-view

...我不知道它适用于什么,但不适用于1.0中的action2。

© www.soinside.com 2019 - 2024. All rights reserved.