Leverage the provided HTML data in Vue.js

I am a Vue newbie looking to transition my project from mostly jQuery to Vue. Currently, I am facing challenges with my navigation as I use `twig` for rendering links and paths through Symfony routing.

//Code simplified
<nav>
      <ul>
          <li><a href="{{ path('app_home') }}">{{ 'home'|trans }}</a></li>
          <li><a href="{{ path('app_logout') }}">{{ 'logout'|trans }}</a></li>
      </ul>
</nav>

Now, I need to figure out how to integrate these elements into a navigation using Vue, which is also handling templating in the project.

Here is the current code snippet:

//app.js
import Vue from 'vue';
import App from './components/App';

new Vue({
    render: h => h(App)
}).$mount('#app'); //div#app acts as the wrapper for everything else



//App.vue
<template>
    <SideNav></SideNav>
    //Add more components like content, etc.
</template>

<script>
    import SideNav from "./SideNav";

    export default {
        name: "App",
        components: {
            SideNav
        }
    };
</script>



//SideNav.vue
<template>
    <custom-nav>
        <custom-link></custom-link>
        <custom-link></custom-link>
    </custom-nav>
</template>

<script>
    import { custom-nav, custom-link } from '/*library with templates - already exists*/'
    export default {
        el: '#side-nav',
        name: "SideNav"
    };
</script>

What would be the most effective approach to achieve this? I aim to continue leveraging symfony translations and routing in the process.

Answer №1

If you want to pass data to your VueJS application using the application/json format while maintaining a proper Content-Type semantic, you can follow these steps:

// Example of using Twig template
//...
 <script id="twig-data" type="application/json">{{yourdata|json_encode}}<script>
//...

// VueJS
//...
 export default {
    el: '#side-nav',
    name: "SideNav",
    data() {
        return {
            twigData: null
        }
    },
    created(){
        this.twigData = JSON.parse(document.getElementById('twig-data').innerHTML);
    }
 };
//...

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

What causes the statement to be executed before the database transaction?

How can I ensure that the state domains are set only after all DB transactions are completed in my code? Please provide guidance on how to perform this operation correctly. I am using the following method to update the new domains array: setFavorites() { ...

Next.js allows you to create a single page that corresponds to the root path '/' as well as a dynamic route '/param'

I have a single-page website built with Next.js. The home page, which displays a list of products, is located at route / and the corresponding code can be found in pages/index.js. Each product has an id, allowing users to jump directly to it using /#produc ...

Implementing useEffect behavior on a button click: a step-by-step guide

I have a form that collects a phone number and then, upon clicking the button, it fetches data from an API using the phone number as a filter parameter. Additionally, I want the button to redirect the user to a dynamic link with their unique code retriev ...

Utilizing Vue.js to retrieve database information and populate input fields through select options

In my Laravel 8 and Vue 3 application, I have a Student Component with a datalist that lists all the students. My goal is to populate the input fields with the specific student information when clicking on a student. I attempted to use Vue select, which i ...

Security measures for interacting with REST API by both frontend application and secondary backend service

I currently have a REST API backend service A that is being utilized by two other services: B service, which is a web app running in a browser (on a separate node server) C service, which is also a backend service (running on a separate server) Initiall ...

The error message "Blazor WebAssembly JSRuntime.InvokeVoidAsync - blazor.webassembly.js:1 Uncaught (in promise) TypeError: Converting circular structure to JSON" indicates that

I have implemented the drawio-integration project in my Blazor WebAssembly application. https://github.com/jgraph/drawio-integration This is how the simple helloworld sample appears: <img onclick='DiagramEditor.editElement(this);' src=" ...

Steps for creating a button that increments by using ng loop:1. Begin

I am attempting to create a button that can be used to increment and decrement. However, I am facing an issue where all input fields are being affected instead of just one. This is the code in body.component.html <div class="items-detail-detail" *n ...

Populate a table with data from a different table using JavaScript

On my webpage, I have a grid of selectable divs that are defined by rows and columns. When I select certain divs, it creates what I'll call "table Copy", a three-dimensional table. If I select different elements, another three-dimensional table calle ...

An error of type TypeError is occurring for each array of objects beyond the first index

Using react leaflet and a water API, I have created an array of objects from the data obtained from the API. The console log shows that all the correct data is present, with line 109 displaying the accurate information. However, at lines 254 and 255, when ...

The ng-options loop in the array is unable to locate the specified value

In my C# controller, I generate a list and translate it to Json for Angular to receive at the front end. However, when using ng-options to loop through this array in order to get the array value, I always end up with the index instead. <select class="s ...

How to specify a single kind of JavaScript object using Typescript

Let's say we have an object structured as follows: const obj = [ { createdAt: "2022-10-25T08:06:29.392Z", updatedAt: "2022-10-25T08:06:29.392Z"}, { createdAt: "2022-10-25T08:06:29.392Z", animal: "cat"} ] We ...

What is the process for transforming a key-value object from JavaScript to TypeScript?

I am currently facing a challenge in converting a JavaScript object into a TypeScript version as part of our code refactoring process :( I am struggling to figure out how to properly migrate a JS JSON object into a correct TS format. For instance, consider ...

The identifier 'name' is not found in the specified data type

How can I resolve the error 'Property 'name' does not exist on type' in TypeScript? Here is the code block : **Environment.prod.ts** export const environment = { production: true, name:"(Production)", apiUrl: 'https://tes ...

Passing along a request using Node.js

I am facing an issue in my project where I need to redirect requests received by a nodejs endpoint to a .NET 7 web API endpoint. The nodejs endpoint is triggered by an external party and it receives the request as expected. However, there seems to be a pro ...

Utilizing Reactjs and typescript to dynamically set width and height properties of a div element using randomly generated variables

I am attempting to set a randomly generated variable as the width and height of a div. I have successfully written a function that generates a random size, but encounter an error when trying to assign it to the style attribute. My objective is to create a ...

Steer clear of using the character sequence "&#8220;" in both PHP and

I am struggling with the encoding (I think). A script I wrote fetches a PHP file through AJAX that generates a JSON file. The JSON file appears as follows in Firebug: ["&#8220;This is a word&#8221; This not"] I am trying to find a way to remove & ...

Yeoman - Storing global settings efficiently

I have recently developed a Yeoman generator and am now looking to incorporate prompts for certain global configurations. My goal is to have the generator prompt users for their GitHub username and token during the initial run, and then somehow store this ...

Hide the scrollbars on the sidebar

I'm attempting to recreate a sleek scrollbar that caught my eye on the website . To achieve this, I need to have a screen larger than 955px in order to display the sidebar. However, when my sidebar overflows in the y direction, it causes an additional ...

Finding the exact location of a specific element within a div: tips and tricks

Is there a way to determine the position of an image inside a div when clicked? Here is my HTML code: <div> <p>test</p> <img src='test1.jpg'/> <p>test</p> <p>test</p> <img src=&ap ...

Launching the file explorer when the icon is clicked

I am currently working with Angular 5 using Typescript. I need assistance in opening the file explorer window to add an attachment when clicking on an icon. I have successfully done this for a button, but I am facing issues with the click event binding on ...