diff --git a/docs/guides/accessing-data.md b/docs/guides/accessing-data.md index 3cce7cc83a..43cada82b4 100644 --- a/docs/guides/accessing-data.md +++ b/docs/guides/accessing-data.md @@ -2,6 +2,10 @@ > TK +## Accessing API Data from your extensions + +For examples on how to access collections and fields in the API from within your custom extensions, see the [Layout](/guides/layouts/) and [Module](/guides/modules/) guides. + ## Storing Sensitive Extension Variables If your extension requires storing sensitive data, such as an API Key, you can store that information in diff --git a/docs/guides/layouts.md b/docs/guides/layouts.md index cef6a8b3b0..53a80187f5 100644 --- a/docs/guides/layouts.md +++ b/docs/guides/layouts.md @@ -63,6 +63,57 @@ The props you can use in an layout are: - `filters` (sync) - The user's currently active filters. - `search-query` (sync) - The user's current search query. + +#### Accessing the API from within your extension +The Directus App's Vue app instance provides a field called `system`, which can be injected into Vue components +using [Vue's inject framework](https://v3.vuejs.org/guide/component-provide-inject.html). This `system` field contains functions to access [Pinia](https://pinia.esm.dev) stores, and more importantly, contains a property called `api`, which is an authenticated Axios instance. Here's an example of how to use it: + +```vue + + +``` + +In the above example, you can see that: +- The `system` field gets injected into the component and becomes available as an attribute of the component (ie `this.system`) +- When the component is mounted, it uses `this.system.api.get` to request a list of all available collections +- The names of the collections are rendered into a list in the component's template +- a button is added with a method the logs all the data for the collections to the console + +This is just a basic example. A more efficient way to access and work with the list of collections would be to get an instance of the `collectionsStore` using `system.useCollectionsStore()`, but that's beyond the scope of this guide + ## 2. Install Dependencies and Configure the Buildchain Set up a package.json file by running: diff --git a/docs/guides/modules.md b/docs/guides/modules.md index 373d025cfc..af11106eb9 100644 --- a/docs/guides/modules.md +++ b/docs/guides/modules.md @@ -57,6 +57,57 @@ export default {}; ``` +#### Accessing the API from within your extension +The Directus App's Vue app instance provides a field called `system`, which can be injected into Vue components +using [Vue's inject framework](https://v3.vuejs.org/guide/component-provide-inject.html). This `system` field contains functions to access Vuex stores, and more importantly, contains a property called `api`, which is an authenticated Axios instance. Here's an example of how to use it: + +```vue + + + +``` + +In the above example, you can see that: +- The `system` field gets injected into the component and becomes available as an attribute of the component (ie `this.system`) +- When the component is mounted, it uses `this.system.api.get` to request a list of all available collections +- The names of the collections are rendered into a list in the component's template +- a button is added with a method the logs all the data for the collections to the console + +This is just a basic example. A more efficient way to access and work with the list of collections would be to get an instance of the `collectionsStore` using `system.useCollectionsStore()`, but that's beyond the scope of this guide + #### Available Props If you setup a route with a parameter, you can pass it in as a prop.