Using utility functions
Utility functions in the Shopware 6 Administration are registered to the Shopware object and are therefore accessible everywhere in the Administration. They provide many useful shortcuts for common tasks.
Prerequisites
All you need for this guide is a running Shopware 6 instance, the files, a registered module, and a good understanding of JavaScript.
Accessing the utility functions
Let us see how to use one of the utility functions — for example, capitalizeString
function. As the name implies, the capitalizeString
function capitalizes strings by calling the lodash capitalize
function.
javascript
// <extension root>/src/Resources/app/administration/app/src/component/swag-basic-example/index.js
const { Component, Utils } = Shopware;
Component.register('swag-basic-example', {
data() {
return {
text: 'hello',
capitalizedString: undefined,
};
},
created() {
this.capitalize();
},
methods: {
capitalize() {
this.capitalizedString = Utils.string.capitalizeString(this.string);
},
},
});