An issue arises when trying to utilize meta tags in Nuxtjs while incorporating TypeScript into the

When working with Nuxtjs, I encountered an issue regarding my permissionKeys on the page and checking user access in the middleware. Everything runs smoothly when my script language is set to js, but errors arise when set to lang="ts".

I tried to find a solution by using the vue-meta package, but unfortunately, I kept running into the same error.

<script lang="ts">
export default {
  meta: {
    authorization: ['Organization/List']
  },
  data() {
    return {
           text: this.$t('organization.title'),
    }
   }
}

As a result, I keep receiving this error message: "Property '$t' does not exist on type '{ meta: { authorization: string[]; }; data(): any; }'."

The issue seems to revolve around utilizing meta and global variables together, which remains unresolved for me at this time.

Answer №1

To implement this functionality, you can utilize the nuxt-property-decorator library. This library allows you to access the head property, which may not directly integrate with the vue-meta plugin. However, you can create a decorator within your class to manipulate the vue-meta package settings.

<script lang="ts">
import { Component, Vue } from 'nuxt-property-decorator';

@Component({
  layout: 'home',
  head() {
    return {
      title: 'Test Title',
      meta: [
        { hid: 'description', name: 'description', content: 'My custom description' }
      ]
    }
  }
})
export default class MainPage extends Vue {

}
</script>

Alternatively, you have the option to define your own decorator within the class itself.

@Meta

https://github.com/nuxt/vue-meta/issues/181#issuecomment-390258209

@Update 1/9/2019

If needed, you can also use the extendRoutes method, although it is not recommended but still functional :)

Create a permissions.js file with contents like below:

export const normalUser = [
  'route-name-invoices',
  'route-name-invoices-create'
];

In your nuxt.config.js, import your permissions file:

import { normalUser } from './permissions'

...
// Locate the router section or define it
router: {
  extendRoutes(routes) {
    return routes.map(route => {
      if (normalUser.includes(route.name)) {
        route.meta = { authorization: ['Organization/list'] };
      }
      return route;
    });
  }
}

Answer №2

When we use <type="ts">, we trigger TypeScript typechecking, which helps to identify errors in our code. Let's dissect the code to understand why the error is valid.

const obj = {
  meta: {
    authorization: ['Organization/List']
  },
  data() {
    return {
           text: this.$t('organization.title'),
    }
   }
};

export default obj;

In the data method, the reference to this actually points to the object obj, where we are trying to access the property $t. However, such a property does not exist in obj.

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

Unable to determine the data type of the JSON object during the

I'm having trouble reading an Object type of json... Here is the json I'm working with: body: { "111": { "name": "name1", "status": 10000 }, "222": { "name": "name2", "status": 20000 }, "333": ...

Guide for adding an OnClick event to a MatTable row:

I am looking to add functionality for clicking on a specific row to view details of that user. For instance, when I click on the row for "user1", I want to be able to see all the information related to "user1". Here is the HTML code snippet: <table ma ...

The ngOnChanges lifecycle hook does not trigger when the same value is updated repeatedly

Within my appComponent.ts file, I have a property called: this._userMessage Afterwards, I pass it to the childComponent like so: <child-component [p_sUserMessage]='_userMessage'></child-component> In the childComponent.ts file: @ ...

Using ThreeJS to Apply Dual Materials to a Mesh Entity

With ThreeJS, it's possible to incorporate more than one material into an Object3D/Mesh as stated in the documentation. You can either utilize a single Material or an array of Material: Class declaration and constructor for Mesh TypeScript file (exce ...

Guide on accessing mobile information and sim card details with Ionic 3 and Cordova on Android devices

Just started using Ionic and I'm looking for guidance on how to retrieve mobile and sim details with Ionic 3 and Cordova for Android. Any help is greatly appreciated in advance! ...

What steps should be taken in order to resolve the error message "Type is missing an index signature"?

Is there a solution to this type error? The argument of type 'Query' is causing an issue as it cannot be assigned to the parameter of type 'Input'. This is due to the absence of an index signature in type 'Query'.(2345) In ...

What is the best way to exclude the bottom four rows when sorting with MatSort?

Is there a way for me to keep the last four rows fixed when sorting the table based on the column header? Here is an image of the table: table image <table mat-table [dataSource]="dataSourceMD" matSort (matSortChange)="getRowMaximoTable( ...

Tips for preventing Angular from requiring an additional tag for a child component

Consider a scenario where I have a parent and child component in Angular 12. Their templates are structured as follows: Parent: <h1>This is the parent component</h1> <div class="container"> <div class="row"> ...

After integrating React Query into my project, all my content vanishes mysteriously

Currently, I am utilizing TypeScript and React in my project with the goal of fetching data from an API. To achieve this, I decided to incorporate React Query into the mix. import "./App.css"; import Nav from "./components/Navbar"; impo ...

How can Angular developers properly implement token refreshing in their applications?

Recently, I've been struggling with implementing a logic in my code. I have a specific requirement: Whenever there is a signed request (signed - means it has a JWT token for authenticated users) made to the API backend, the API backend may respond w ...

What are the steps to set up tsline in settings.json file?

Currently utilizing visual studio code Upon searching for the settings.json file, the contents appear as follows: { "liveServer.settings.donotVerifyTags": true, "liveServer.settings.donotShowInfoMsg": true, "javascript ...

Updating a string's value in Angular based on user input

I am currently developing a custom offer letter template that will dynamically update key data points such as Name, Address, Role, Salary, etc based on the selected candidate from a list. The dynamic data points will be enclosed within <<>> in ...

Transform array of elements from type T1 to element in the array to type T2

Here is a Typescript class I am working with: export class Envelope<T> { result: T; constructor(result: T) { this.result = result; } } I'm trying to convert Envelope<RecentPostResponse[]> to Observable<PostModel[]>: getP ...

I am encountering an issue where the nested loop in Angular TypeScript is failing to return

I am facing an issue with my nested loop inside a function. The problem is that it is only returning the default value of false, instead of the value calculated within the loop. Can someone please point out what I might be doing incorrectly? Provided belo ...

Cached images do not trigger the OnLoad event

Is there a way to monitor the load event of my images? Here's my current approach. export const Picture: FC<PictureProps> = ({ src, imgCls, picCls, lazy, alt: initialAlt, onLoad, onClick, style }) => { const alt = useMemo(() => initial ...

Just made the switch to Mongoose 5.12 and hit a snag - unable to use findOneAndUpdate with the $push operator

After upgrading to Mongoose 5.12 from 5.11 and incorporating Typescript, I encountered an issue with my schema: const MyFileSchema = new Schema<IMyFile>({ objectID: { type: String, required: true }, attachments: { type: Array, required: false ...

Authenticating to Google APIs using Node.js within a lambda function: A step-by-step guide

I'm encountering an issue when trying to connect a Google sheet to an AWS Lambda function. While the code runs smoothly during local testing, upon deployment to the function, I receive an error message indicating that the credentials.json file cannot ...

Issue with incorporating Nuxt layouts into routes

Our Nuxt configuration includes an auto-generated router.js. I am looking to implement a different layout for specific pages, each with their unique HTML structure. My expectation is to define the layout in nuxt.config.js using a meta tag (meta: { layout: ...

Encountering error code TS1003 while trying to access object properties using bracket notation in Typescript

How can object property be referenced using bracket notation in TypeScript? In traditional JavaScript, it can be done like this: getValue(object, key) { return object[key]; } By calling getValue({someKey: 1}, "someKey"), the function will return 1. H ...

Mixing Jest and Cypress in a TypeScript environment can lead to Assertion and JestMatchers issues

When utilizing [email protected] alongside Jest, we are encountering TypeScript errors related to Assertion and JestMatchers. What is the reason for these TypeScript errors when using Jest and [email protected] in the same project? ...