diff --git a/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/abp-js-proxy.png b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/abp-js-proxy.png new file mode 100644 index 0000000000..262a281e0f Binary files /dev/null and b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/abp-js-proxy.png differ diff --git a/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/post.md b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/post.md new file mode 100644 index 0000000000..2805ab310c --- /dev/null +++ b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/post.md @@ -0,0 +1,324 @@ +# Using Vue components in your Razor Pages ABP Application + +In modern web development, integrating dynamic front-end frameworks with server-side technologies has become increasingly essential for creating responsive and interactive applications. This article explores how to effectively use Vue components within Razor Pages in an ABP Framework application. We will delve into the process of consuming endpoints through ABP Client Proxies, leveraging ABP's powerful localization features to enhance user experience, and implementing ABP permissions to ensure secure access control. By the end of this guide, you will have a comprehensive understanding of how to seamlessly blend Vue.js with Razor Pages, empowering you to build robust and user-friendly applications. + +This arcitle won't use any SPA approach. The goal of this article to use Razor Pages with simple Vue components to eliminate jQuery while developing MVC application. + +## Creating the Solution + +Let's create a simple TODO list application to demonstrate how to use Vue components in Razor Pages. I'll build a really simple backend without connection to a database for demonstration purposes. We will focus to the frontend part. + +- Creating solution with ABP CLI: + +```bash +abp new MyTodoApp -t app-nolayers -csf +``` + +## Configure Vue + +We need to add `@abp/vue` package to the project to use Vue components. + +```bash +npm install @abp/vue +``` + +- Install client libraries by using ABP CLI: + +```bash +abp install-libs +``` + +As a last step, we need to configure our bundle in `ConfigureBundles` method in `MyTodoAppModule.cs` file: + +```csharp +private void ConfigureBundles() +{ + Configure(options => + { + // ... + + options.ScriptBundles.Configure( + // Or BasicThemeBundles.Scripts.Global + // Or LeptonXLiteThemeBundles.Scripts.Global + // 👇 Depends on the theme you are using + LeptonXThemeBundles.Scripts.Global, + bundle => + { + bundle.AddFiles("/global-scripts.js"); + // 👇 Make sure to add this line + bundle.AddContributors(typeof(VueScriptContributor)); + } + ); + }); +} +``` + +> If your IDE doesn't recognize `VueScriptContributor`, you can add it manually: +> +> ```csharp +> using Volo.Abp.AspNetCore.Mvc.UI.Packages.Vue; +> ``` + +Now we're ready to use Vue components in our Razor Pages. + +## Creating a Vue Component + +Let's create a simple Vue component to display the TODO list. + +### Passing a simple message to the component + +- Remove existing html coes in `Index.cshtml` and replace with the following code: + +```html +
+ +
+``` + +- Navigate to `Index.cshtml.js` file and add the following code: +```js +Vue.component('todo-component', { + template: '
Hello, {{ message }}
', + props: ['message'] +}); + +new Vue({ + el: '#vue-app' +}); +``` + +Run the application and you should see the following output: + +![Vue Component](./vue-message.png) + +> _Hard refresh might be required to see the component since we added a new vue js file to the bundle._ +> +> If still you can't see the component, please check the browser console for any errors. + +### Interacting with the component + +Let's add a button to the component to interact with the component. + +- Add another component in the `Index.cshtml` file: + +```html +
+ + +
+``` + +```js +Vue.component('counter-component', { + template:` +
+
+

Count: {{ count }}

+ +
+
+ `, + data: function () { + return { + count: 0 + }; + }, + methods: { + increment: function () { + this.count++; + } + } +}); +``` + +> _Do not replicate `new Vue({})` code block in the file. It's already in the `Index.cshtml.js` file. Keep it as it is._ + +Run the application and you should see the following output: + +![Vue Component](./vue-counter-result.gif) + + +## Using ABP Client Proxy, Authorization and Localization + + +### Building the backend +Before we go, let's build our backend to use in the component. + +- Creating a simple Application Service: + +```csharp +public class TodoAppService : MyTodoAppAppService, ITodoAppService +{ + public static List Items { get; } = new List(); + + [Authorize("Todo.Create")] + public async Task AddTodoItemAsync(TodoItem input) + { + Items.Add(input); + return input; + } + + [Authorize("Todo")] + public async Task> GetAllAsync() + { + await Task.Delay(1500); + return Items; + } + + public async Task GetMessageAsync() + { + return "Hello World"; + } +} +``` + +- `TodoItem.cs` + +```csharp +public class TodoItem +{ + public string Description { get; set; } + public bool IsDone { get; set; } +} +``` + +- `ITodoAppService.cs` + +```csharp +public interface ITodoAppService +{ + Task> GetAllAsync(); + Task AddTodoItemAsync(TodoItem input); + Task GetMessageAsync(); +} +``` + +- Run the application and if you can see the following client proxy in the browser console, you're ready to go: + + ![Client Proxy](./abp-js-proxy.png) + +> [!NOTE] +> If you can't see the client proxy in the browser console, please check the [Dynamic JavaScript Proxies](https://abp.io/docs/latest/framework/ui/mvc-razor-pages/dynamic-javascript-proxies) to learn how to enable it. + +- Add a new permission in the `MyTodoAppPermissionDefinitionProvider.cs` file: +```csharp +public override void Define(IPermissionDefinitionContext context) +{ + var myGroup = context.AddGroup(MyTodoAppPermissions.GroupName); + + var todo = myGroup.AddPermission("Todo"); + todo.AddChild("Todo.Create"); +} +``` +> _I go without localization or constants for simplicity._ + +- Add a localization key in the `en.json` file: + +```json +{ + "TodoItems": "Todo Items Localized" +} +``` + +### Building the Vue Component: Using ABP Localization, Authorization and Client Proxy + +Since the component it directly loaded into the page, we can access the `abp` object in the page. + +So we can use: + +- `abp.localization.localize()` to localize a string. +- `abp.auth.isGranted()` to check the authorization. +- `myTodoApp.todo.getAll()` and `myTodoApp.todo.addTodoItem` to call the Application Service. + +inside **Vue Component** code. + +- Let's add another component named `todo-component` and usee all the **ABP Features** in it. + +```html +
+ + +
+``` + +- Implement the `todo-component` in `Index.cshtml.js` file: + +```js +Vue.component('todo-component', { + template: ` +
+
+

{{ abp.localization.localize('TodoItems') }}

+
+
+
+
+ Loading... +
+
+
    +
  • + + +
  • +
+

No todos yet

+
+ +
+ `, + data: function () { + return { + newTodo: { + description: '', + isDone: false + }, + isBusy: false, + todos: [] + }; + }, + methods: { + addTodo() { + myTodoApp.todo.addTodoItem(this.newTodo); + this.newTodo = { description: '', isDone: false }; + this.todos.push(this.newTodo); + // this.loadTodos(); + }, + async loadTodos() { + if (!abp.auth.isGranted('Todo')) { + return; + } + this.isBusy = true; + this.todos = await myTodoApp.todo.getAll(); + this.isBusy = false; + } + }, + mounted() { + this.loadTodos(); + } +}); +``` + +And see the result: + +![Vue Component](./todo-component-result.gif) + + +Since we use `abp.auth.isGranted()` to check the authorization, we can see the component only if we have the permission. + +Whenever you remove `Todo.Create` permission, you can see the component is not rendered. + +![Todo Permission](./todo-permission.png) + + +You won't see the card footer: + +![Todo Permission](./todo-permission-vue.png) diff --git a/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/todo-component-result.gif b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/todo-component-result.gif new file mode 100644 index 0000000000..2eb50ee5c3 Binary files /dev/null and b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/todo-component-result.gif differ diff --git a/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/todo-permission-vue.png b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/todo-permission-vue.png new file mode 100644 index 0000000000..2f201df576 Binary files /dev/null and b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/todo-permission-vue.png differ diff --git a/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/todo-permission.png b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/todo-permission.png new file mode 100644 index 0000000000..061f1279e1 Binary files /dev/null and b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/todo-permission.png differ diff --git a/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/vue-counter-result.gif b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/vue-counter-result.gif new file mode 100644 index 0000000000..656f001535 Binary files /dev/null and b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/vue-counter-result.gif differ diff --git a/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/vue-message.png b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/vue-message.png new file mode 100644 index 0000000000..4c3e3c6166 Binary files /dev/null and b/docs/en/Community-Articles/2025-03-18-Using-Vue-Components/vue-message.png differ