Using Vue's computed property in the router template

I'm currently facing a challenge in grasping how to pass a computed property all the way from the router to my template. Here's a simplified overview of the scenario:

const Home = {
  template: '<router-link to="/level/1">Level 1</router-link>'
}

const Level = {
  template: '<template>|{{ id }}|{{ message }}|</template>',
  props: ['id','message']
}

const router = new VueRouter({
  routes: [
    { path: '/', component: Home, props: true },
    { path: '/level/:id', component: Level, props: true }
  ]
})

const vm = new Vue({
    el: '#app',
    router,
    template: '<router-view></router-view>',
    computed: {
        message() {
            return 'HELLO';
    }
  }
})

Upon clicking the "Level 1" link, I anticipate seeing the following result:

|1|HELLO|

However, the actual output I receive is as follows:

|1||

The final implementation will involve more functionality than outlined here, but I hope this illustration exposes any misunderstandings I may have regarding props, routing, or computed properties.

Answer №1

Two main issues need to be addressed:

1) A critical error has been identified:

An error occurs when attempting to use <template> as the component root element due to its potential for containing multiple nodes.

To resolve this, it is recommended to change the root element to a div. In projects utilizing Vue CLI, templates are enclosed within <template>, but a singular root element should still be present inside it.

2) The Level component features a prop named message, which is not being passed correctly. While the Home route successfully passes the id property, it omits passing the required message. Moreover, since the message is within the root component, Home cannot directly receive it as of now.

Suggested solutions include:

  • Utilizing Vuex for a more streamlined resolution
  • Defining and passing message from Home directly to Level
  • Initially transferring the message from the root to Home, then forwarding it once more from Home to Level

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 adjust metadata titles while utilizing the 'use client' function in Next.js

I have a /demo route in my Next.js 13 application, and it is using the App Router. However, I am facing an issue with changing the title of the page (currently displaying as localhost:3000/demo). The code snippet for this issue is shown below: /demo/page ...

It is not possible to invoke a function within the mounted() lifecycle hook

I recently integrated a chat API into my Vue.js project for connecting to chat rooms. Upon entering a chat room page, an ajax request is triggered which then calls a function responsible for fetching the complete chat history: mounted() { $.ajax({ url: ...

When using Javascript template literals, they function properly when assigned to a variable, but they do not work when included in a JSON

Trying to get a grasp of Javascript. Let's say I have a variable declared with template literals: var templateObject = `Student name is ${filteredJSONExample.name} and student age is ${filteredJSONExample.age}.` This functions correctly as sh ...

Changing the background image of the body when hovering over a li element: a step-by-step

For my website, I want to achieve the same effect as seen on the homepage of this site: I am looking to change the background image of my webpage when a specific li element is hovered over. Each li element should trigger a different background image. Add ...

Tips for connecting two separate Vue.js applications

Take note: this is the rid function that is called within the code. <script> const rid = () => "rid_" + Array.from(crypto.getRandomValues(new BigUint64Array(2))).map(item => item.toString(36)).join(""); </script&g ...

How can I stop a user from navigating through links or submitting forms using jQuery?

I'm exploring the idea of converting my website into a Single Page Application. One challenge I am facing is capturing the navigation process, for example: <a href="myData.php">Change My Data</a> When a user clicks on the "Change My Data ...

Learn how to switch between search and results views in Angular while also transferring data

Currently, I'm attempting to tackle a common task that I've yet to encounter an example of. Display 1 and Control 1 : In this view, there is a basic textbox with ng-model="searchTerm" and ngSubmit event. After the user enters their search term, ...

Tips for directing specific mouse interactions to the underlying elements within an SVG

I'm working with a scatterplot that includes a brush layer for zooming in on the data and voronois for mouse hover snapping. However, I've encountered an issue where the click event doesn't fall through to the brush layer when the voronoi pa ...

What are the steps to troubleshoot Vue CLI plugins using Visual Studio Code?

As I work on developing vue cli plugins similar to vuetify's, creating a boilerplate for other developers, I often find myself needing to debug the code. Using console.log repeatedly has become tedious. I am looking for effective ways to debug these p ...

Ways to automatically assign a unique div id to every item in a v-for loop?

Here is my Vue <div class="drag"> <h2>List 1 Draggable</h2> <ul> <li v-for="category in data"> <draggable :key="category.id" v-bind:id="categ ...

establish expiration date for image

On my e-commerce site, users upload images to customize product items, such as adding their photo to a cake. I'm curious if there's a way to automatically delete these images after a certain expiry date. Is it possible to implement an automatic ...

In React, the constant always has a default value of "Object : Default"

My current project involves using SlateJS and I am encountering an issue when trying to load data into a const using: imgData = data.get("file"); Everything works as expected if React is not imported, but once it is included, the object contains unexpecte ...

Is it possible to retrieve context from a p-tag with a designated option while other words remain unselectable?

In the HTML code below, there is a p-tag that displays an article with two different types of words - those you can leave as they are and those you can select. For example: <div class="context"> <p v-html="report_data&qu ...

Sending data from TextBox as json format

JavaScript Code: var latitude = document.getElementById("<%=txt_Lat.ClientID %>").value; var longitude = document.getElementById("<%=txt_Long.ClientID %>").value; var jsonData = {latitude: latitude, longitude: longitude}; var jsonString = JSO ...

Exploring the world of CouchDB through jQuery and AJAX POST requests while navigating

I am in the process of building a simple web application. Today, I installed Couch 1.3.1 and set up a database. I am currently trying to save a document to my local Couch (localhost:5984) using a POST request from a client browser that is also on localhost ...

When using jQuery's `.click()` method on an element to collapse it with the 'show' parameter set to false, the disabling action will only take effect after the document

When you first run the program and click anywhere on the body, it activates the collapse element. I want it to only collapse the accordion on click, not show it immediately. Currently, it will deactivate only after it is hidden once. HTML <!DOCTYPE ht ...

What is the best way to restore the original dimensions of images after they have been altered?

I have content displayed on a webpage with images, each image having a specific width or none specified. When a user clicks a button, the content is exported to a PDF file. To ensure the images fit correctly on the PDF page, I adjust their width and height ...

How can I selectively apply a texture to a specific section of a face in Three.js?

I am exploring the idea of creating portals using threejs by sketching an ellipse and then applying a WebGlRenderTarget texture mapping to its face. Currently, I have a function that partially works, but it attempts to stretch the large rectangular buffer ...

The JavaScript alert message pops up two times consecutively

I encountered an issue while attempting to utilize a module named disclaimer in Drupal 7. The alert message "You must enter the year you were born in." appears twice and then redirects to a URL that should only be accessed after verifying that you are over ...

The data being retrieved from the controller in AngularJS is not displaying as expected

Just started learning AngularJS. Currently following this MEAN Stack Intro: Build an end-to-end application Created a basic html file to test angularjs. index.html <html> <head> <meta charset="utf-8"> <title> ...