我了解“ finally”关键字在各种语言中的含义,但是,我很难理解为什么您会在口味偏爱格式之外使用它。
例如,在PHP中:
try {
possibleErrorThrownFunction();
}
catch (CustomException $customException) {
// handle custom error
}
catch (Exception $exception) {
// handle the error
}
finally {
// run this code every single time regardless of error or not
}
Run Code Online (Sandbox Code Playgroud)
这段代码在做什么和这样做之间有什么区别?
try {
possibleErrorThrownFunction();
}
catch (CustomException $customException) {
// handle custom error
}
catch (Exception $exception) {
// handle the error
}
// run this code every single time regardless of error or not
Run Code Online (Sandbox Code Playgroud)
由于捕获了错误,最后一行不是总会运行吗?在这种情况下,finally除非您只想维护代码样式的格式,否则没有实际使用的情况? …
我正在尝试提取在把手模板编译期间实际使用的部分列表。如果我能在实际编译之前以某种方式获得该信息,那就更理想了。
下面是一个简单的把手示例,它以您期望的方式编译:
var handlebars = require('handlebars');
var template = `
Template: {{>a}}
`;
handlebars.registerPartial('a', `
Partial A: {{A}} {{>b}}
`);
handlebars.registerPartial('b', `
Partial B: {{B}} {{>c}}
`);
handlebars.registerPartial('c', `
Partial C: {{C}}
`);
handlebars.registerPartial('d', `
Partial D: {{D}}
`);
var compiled = handlebars.compile(template);
console.log(compiled({
'A' : 'A is for Apple',
'B' : 'B is for Ball',
'C' : 'C is for Cat',
'D' : 'Not used'
}));
Run Code Online (Sandbox Code Playgroud)
输出为
Template:
Partial A: A is for Apple
Partial B: B …Run Code Online (Sandbox Code Playgroud) 我在输出组件列表然后重新排序它们时遇到了问题。这适用于我的桌面 chrome,但不适用于移动 safari 或 chrome。有人遇到过这种情况吗?
有关更多信息,我有一个管理实体数组的 vuex 模块,我提交此更改以重新排序它们:
state.entities.sort((a, b) => parseInt(a.initiative) < parseInt(b.initiative))
然后在一个组件中,我根据这个实体数组输出标记:
<entity v-if="showEntityList"
v-for="entity in entities"
:key="entity.id"
:entity="entity"/>