我正在尝试创建一个根据 url 中传递的参数动态加载 View 的操作
下面是我的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()
检查物理文件是否存在,然后加载它
最佳答案
在您使用的 action2 格式中,您必须使用 throw
路由到备用导出。请参阅:
https://sailsjs.com/documentation/concepts/actions-and-controllers
不要对这里的文档感到困惑:
https://sailsjs.com/documentation/reference/response-res/res-view
...我不知道它适用于什么,但它不适用于 1.0 中的 action2。
关于node.js - 如何使用 exits 和 actions2 在 sails 1.0 中设置动态 viewTemplatePath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53996278/