Unable to assign dynamic key to Vue 3 directive for object property

Currently, I am utilizing the maska npm package to mask input fields in Vuetify.

Within my setup, I have an array of masks that I make use of:

export const Masks = {
    hour: { mask: "##:##", eager: true },
    host: { mask: "#00.#00.#00.#00", eager: true },
    port: { mask: "#0000" },
    cc: { mask: "#### #### #### ####" },
    ccName: { mask: "A A" },
    cvv: { mask: "###0" }
};

When incorporating these masks in the <template> section of a .vue file, everything functions as expected:

<v-text-field
    v-model="model.start"
    v-maska:[Masks.hour]
/>

However, when working on a dynamic form component that loads the assigned mask from the model object, I encounter the following setup:

const form = {
    start: {
        ...
        mask: Masks.hour,
        ...
    }
};

And in the template:

<div v-for="(field, index) in Object.keys(model)" :key="field">
    <v-text-field
        v-model="model[field].value"
        v-maska:[model[field].mask]
    />
</div>

Yet, running this code snippet results in a syntax error:

 ERROR  Internal server error: Error parsing JavaScript expression: Unexpected token, expected "]" (1:12)                                                                   4:59:07 p.m.  
  Plugin: vite:vue
  File: D:/Docs/Code/fresk/proteus/components/forms/dynamic-form.vue:98:13
  96 |                                  v-else-if="!model[field].type"
  97 |                                  v-model="model[field].value"
  98 |                                  v-maska:[model[field].mask]
     |               ^
  99 |                                  :name="field"
  100|                                  :label="model[field].label ? $t(model[field].label) : undefined"
      at createCompilerError (D:\Docs\Code\fresk\proteus\node_modules\@vue\compiler-core\dist\compiler-core.cjs.js:1332:17)
      at emitError (D:\Docs\Code\fresk\proteus\node_modules\@vue\compiler-core\dist\compiler-core.cjs.js:2822:5)
      at createExp (D:\Docs\Code\fresk\proteus\node_modules\@vue\compiler-core\dist\compiler-core.cjs.js:2815:7)
      at Object.ondirarg (D:\Docs\Code\fresk\proteus\node_modules\@vue\compiler-core\dist\compiler-core.cjs.js:2263:25)
      at Tokenizer.stateInDirArg (D:\Docs\Code\fresk\proteus\node_modules\@vue\compiler-core\dist\compiler-core.cjs.js:846:16)
      at Tokenizer.parse (D:\Docs\Code\fresk\proteus\node_modules\@vue\compiler-core\dist\compiler-core.cjs.js:1078:16)
      at Object.baseParse (D:\Docs\Code\fresk\proteus\node_modules\@vue\compiler-core\dist\compiler-core.cjs.js:2861:13)
      at Object.parse (D:\Docs\Code\fresk\proteus\node_modules\@vue\compiler-dom\dist\compiler-dom.cjs.js:703:23)
      at Object.parse$2 [as parse] (D:\Docs\Code\fresk\proteus\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:1851:24)
      at createDescriptor (file:///D:/Docs/Code/fresk/proteus/node_modules/@vitejs/plugin-vue/dist/index.mjs:74:43)

So, my query revolves around the process of passing a variable value within an indexed array in a v-for loop to a v-maska or any other Vue 3 directive. How can this be achieved?

Answer №1

After revising my code, I was successful in getting it to function properly by adjusting my v-for loop like this:

<div v-for="(field, key) in model" :key="key">

Subsequently, I implemented it in the template without including the array index.

v-maska:[field.mask]

It seems that using an array index as a value within the [] of a directive is not allowed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

``Is there a specific scenario where the use of getInitialProps is recommended when automatically redirecting from one

Within my application, I have set up an auto-redirect from the root directory '/' to '/PageOne' with the following code: const Home = () => { const router = useRouter(); useEffect(() => { router.push('/pageone', ...

What is the best way to incorporate TypeScript into a simple JavaScript project and release it as a JavaScript library with JSDoc for TypeScript users?

Issue: I have encountered difficulties finding an efficient solution for this. Here's a summary of what I attempted: To start, ensure that allowJs and checkJs are both set to true in the tsconfig.json, then include the project files accordingly. Ty ...

Building a customized Mui clip with child support: A step-by-step guide

Looking to create a customized chip that can handle both single and nested declarations? Check out this example using MUI v5. interface StyledModalChipProps { theme: Theme children: React.ReactNode } export const StyledModalChip = styled(Chip)<Styled ...

Using {angular} import from 'angular' is causing a malfunction in AngularJS version 1.5

React - 17.0 TypeScript - 4.1.2 Babel - 7.13.14 tsconfig.json { "compilerOptions": { "target": "es6", "module": "esnext", "moduleResolution": "node", "jsx": ...

Is there a simple method to eliminate devDependencies from the ultimate package using esbuild?

My current challenge involves using esbuild to package my lambda functions. However, during the build generation for deployment, I encounter an alert indicating that the package size exceeds the limit, as shown in the image below. File too large In explo ...

Deactivate multiple textareas within a loop by utilizing JQuery/Typescript and KnockoutJS

I am working on a for loop that generates a series of textareas with unique ids using KO data-binding, but they all share the same name. My goal is to utilize Jquery to determine if all the textareas in the list are empty. If they are, I want to disable al ...

How can I access the rendered HTML element from a component in Vue 3?

This particular component is known as LayerComponent (placeholder). <script> export default { data() { return { count: 0 } } } </script> <template> <button @click="count++">You have clicked me {{ count ...

How can the panel within an accordion be enlarged or minimized?

Currently, I am implementing an accordion feature with the option to expand or collapse all panels using two buttons. My goal is to allow users to manage each panel within the accordion individually. However, I have encountered an issue that needs attenti ...

Exploring Angular2's interaction with HTML5 local storage

Currently, I am following a tutorial on authentication in Angular2 which can be found at the following link: https://medium.com/@blacksonic86/authentication-in-angular-2-958052c64492 I have encountered an issue with the code snippet below: import localSt ...

Transform the function into an observable form

Is there a way to transform this function into an observable? I need it to check for the existence of a document based on a query, and I want to be able to subscribe to it in order to create a new document if one does not already exist. Unfortunately, I a ...

Angular : How can a single item be transferred from an array list to another service using Angular services?

How to Transfer a Single List Item to the Cart? I'm working on an Angular web application and I need help with transferring a single item from one service to another service and also displaying it in a different component. While I have successfully i ...

Error: Issue determining the type of variable. Unable to eliminate type 'any'

I am trying to load some widgets from a template object (possibly JSON in the future). Here's an example: type RectangleTemplate = { name: 'Rectangle'; props: { width: number; height: number; } }; type ButtonTemplate = { nam ...

I encountered an error in my Node.js application stating that it could not find the name 'Userdetailshistory' array. I am puzzled as to why this error is occurring and I suspect it may be due to my

import { Component, OnInit } from '@angular/core'; import { UserdetailshistoryService } from '../../services'; @Component({ selector: 'my-userdetailshistory', templateUrl: './userdetails-history.component.html', ...

SystemJS TypeScript Project

I am embarking on a journey to initiate a new TypeScript project. My aim is to keep it simple and lightweight without unnecessary complexities, while ensuring the following: - Utilize npm - Implement TypeScript - Include import statements like: import ...

Running pre-commit eslint autofix but encountering errors that do not exist

Encountering an issue with committing changes to a file due to a failed pre-commit eslint --fix task, displaying the following errors: × eslint --fix: C:\Users\user\source\repos\project\project-frontend\src\compone ...

Incorporate new class into preexisting modules from external library

I am currently working on expanding Phaser by incorporating a new module called Phaser.Physics.Box2D. While Phaser already utilizes this module internally, it is an additional plugin and I am determined to create my own version. TypeScript is the language ...

Here is a guide on showcasing information obtained from ASP.NET API in Angular version 13

My goal is to fetch motorcycle data from a web API and showcase it in my Angular project. ASP.NET Framework Web API 4.7 Angular CLI: 13.3.7 Angular: 13.3.11 On the Web API end: Controller: [EnableCors(origins: "*", headers: "*", ...

Will the component re-render before executing the next lines when using setState or dispatch with the useReducer hook?

Upon using the useState and useReducer hooks, I encountered an issue where any code lines following the state update function (setState, dispatch) would be executed in the subsequent re-rendering, with the previous state intact before the update. Essential ...

Is there a way to retrieve keys of the object from this combination type?

Can someone please help me understand how to retrieve keys from this union type? The Value is currently being assigned as a never type. I would like the Value to be either sno, key, or id type Key = { sno: number } | { key: number } | { id: number }; typ ...

Automatically insert a hyphen (-) between each set of 10 digits in a phone number as it is being typed into the text

I'm attempting to automatically insert a hyphen (-) in between 10 digits of a phone number as it is being typed into the text field, following North American standard formatting. I want the output to look like this: 647-364-3975 I am using the keyup ...