在 Addon SDK 中动态添加按钮到工具栏
Dyanmically Adding Buttons to Toolbar in Addon SDK
在我的脚本中,我正在读取 json 文件并从那里的数据构建 sdk menu buttons。这工作得很好,但我无法将这些按钮添加到我的 sdk 工具栏。
因此,在我从 json 文件加载数据的循环中,我从数据创建了一个按钮,并将该按钮添加到数组中,如下所示:
var alltheButtons = [];
...
alltheButtons.push(jsondata[key].button);
在创建按钮的循环中,我添加了
var myToolbar = Toolbar({
title: "My toolbar",
items: [alltheButtons]
});
但是,当尝试将 alltheButtons
添加到工具栏时,脚本会出错(由于它们没有附加到任何特定的工具栏,因此它们最终出现在常规导航栏上)。
错误是这样的:
Message: TypeError: aId.startsWith is not a function
那么,如何为工具栏的 items
属性指定一组按钮?
试试这个:
var myToolbar = Toolbar({
title: "My toolbar",
items: alltheButtons
});
items
应该是 Array
但你给了它 Array<Array>
或者您可以使用 ES2015 spread operator:
var myToolbar = Toolbar({
title: "My toolbar",
items: [...alltheButtons, oneMoreButton]
});
在我的脚本中,我正在读取 json 文件并从那里的数据构建 sdk menu buttons。这工作得很好,但我无法将这些按钮添加到我的 sdk 工具栏。
因此,在我从 json 文件加载数据的循环中,我从数据创建了一个按钮,并将该按钮添加到数组中,如下所示:
var alltheButtons = [];
...
alltheButtons.push(jsondata[key].button);
在创建按钮的循环中,我添加了
var myToolbar = Toolbar({
title: "My toolbar",
items: [alltheButtons]
});
但是,当尝试将 alltheButtons
添加到工具栏时,脚本会出错(由于它们没有附加到任何特定的工具栏,因此它们最终出现在常规导航栏上)。
错误是这样的:
Message: TypeError: aId.startsWith is not a function
那么,如何为工具栏的 items
属性指定一组按钮?
试试这个:
var myToolbar = Toolbar({
title: "My toolbar",
items: alltheButtons
});
items
应该是 Array
但你给了它 Array<Array>
或者您可以使用 ES2015 spread operator:
var myToolbar = Toolbar({
title: "My toolbar",
items: [...alltheButtons, oneMoreButton]
});