Troubleshooting Vue.js 2 Routing Issues: Difficulty Accessing Posts Variable

My first venture into Vue.js involves working with WP REST API.

Initially, all my posts are displayed perfectly. However, when I attempt to integrate Vue-router, the component responsible for showcasing all the posts, 'home-post-list', breaks down at the very first 'v-if="posts"' statement.

Evidently, Vue perceives that there are no posts and therefore does not render anything further. Unfortunately, I am unable to resolve how to make it acknowledge the presence of 'posts'. No errors appear in the console.

Upon inspecting Vue DevTools, I notice:

Hence, the 'router-view' seems to be functioning correctly, but the props are empty. Although I believed I was passing props from the main instance to the child component, it appears I may have made an error somewhere.

I will now showcase my existing code.

***HomePostList component

const HomePostList = Vue.component('home-post-list', {
  props:['posts'],
  template: `<div class="cell medium-8">
                        <div id="all-posts" class="all-posts" v-if="posts">
                        <div class="grid-x grid-margin-x">
                          <div class="post medium-6 cell" :class="{'medium-12':index===0}" v-for="(post,index) in posts">
                         <!-- rest of the component structure unchanged -->
                    </div>
                  </div>
});

***SinglePost Component

const SinglePost = Vue.component('single-post-template', {
  props:['posts'],
  template: `<div class="cell medium-8">
                    <div class="grid-x grid-margin-x">
                        <p>Single Post here</p>
                    </div>
                  </div>`
});

***Routes & Vue Instance

const routes = [
  {
        path: '/',
        component: HomePostList,
        props: true
  },
  { 
        path: '/post/:postId',
        name: 'post',
        component: SinglePost
  }
];

const router = new VueRouter({
  routes
});

new Vue({
// remaining copied code is left out intentionally for brevity
});

***index.php

<?php
/*
Template Name: Front
*/
get_header(); ?>

<!-- Home Page -->
<div class="grid-container">
  <div class="grid-x grid-margin-x">

    <!-- Main Post Container -->
    <router-view></router-view>

    <!-- Sidebar -->
    <div id="sidebar" class="cell medium-4">
      <sidebar-search></sidebar-search>
    </div>
  </div>
</div>

<?php get_footer();

Answer №1

After some trial and error, I found a solution by inserting posts into the router-view component.

<router-view :posts="posts"></router-view>

I'm not entirely sure if this is the most conventional method, but it did the trick for me.

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

Navigate to a specific moment in an HTML5 audio track by clicking on the progress bar

<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script> $(document).ready(function(){ var counter = 0; ...

What is the best way to display various components on a single page?

Every user type within my app requires a unique dashboard. Currently, the admin vue appears as follows: <template> <main id="admin-main"> <header id="admin-dashboard-header" class="jumbotron"> <div> ...

Sign in with Google / External authentication service

Currently working on an AngularJS application that includes Google login functionality. To implement this feature, I found a useful guide at the following link: . Additionally, there is a script that is called when the page loads: <script type="text/j ...

Is there a way to specify both a fixed width and a custom resizable length for a Spring <form:textarea /> element?

Despite extensive research, I have yet to find an answer to my specific problem. Within a model window, I have a textarea element: <div class="form-group"> <label for="groupDescription" class="col-sm-2 control-label"> Descripti ...

Tips on successfully passing multiple keys and their associated HTML tag attributes in a React application

One of my links, specified with an a-tag, appears in this manner: <a href={ item.htmlReportUrl } target="_blank" rel="noopener noreferrer"> {item.htmlReportText}</a> The values for the href and the linktext are sourced from the following: ro ...

Best Practices for Converting TypeScript to JavaScript

What is the recommended approach to converting this JavaScript code into TypeScript? JAVASCRIPT: function MyClass() { var self = this, var1 = "abc", var2 = "xyz"; // Public self.method1 = function () { return "somethin ...

Iterate through a generic array to populate a table using ng-repeat

In the scenario I'm currently facing, I am working on creating a common reusable table structure using a directive. The goal is to display different JSON data in both the table header and body. Below is the controller code: angular.module('plun ...

Passing a JSON object between pages in Next.js using props during programmatic navigation

In my Next.js project, I have two pages. On the first page, the user fills out a form to create a post. The information is stored in a large JSON object, which needs to be passed to the second page for the user to preview the post before finalizing it. Wi ...

Can you explain the functionality of the following Express router?

In the tutorial I am currently following, there are 2 router methods used to retrieve articles from the database. router.param('article', function(req, res, next, slug) { Article.findOne({ slug: slug}) .populate('author') .th ...

Using AngularJS to filter JSON data

Greetings! I possess the following JSON data: $scope.Facilities= [ { Name: "-Select-", Value: 0, RegionNumber: 0 }, { Name: "Facility1", Value: 1, RegionNumber: 1 }, { Name: ...

Modifying the display property of an element using JavaScript

Hello, I'm encountering an issue with a section of my javascript code. I am attempting to make the #showAddress element display as block when the deliverservice radio button is clicked or checked. I have tried searching for solutions on Stack Overflow ...

Save a SQL query as a text file using Node.js

I'm having an issue with my code. I am trying to save the results of a SQL query into a text file, but instead of getting the actual results, all I see in the file is the word "object." const fs = require('fs'); const sql = require('mss ...

The Rsuite Uploader is refusing to transfer the file to the Express server

For my project, I am utilizing an Uploader component from RSuite to upload images to the Express server: <Uploader action={process.env.REACT_APP_API_URL + '/loadMap'} draggable headers={{Authorization: 'Bearer ' + localStorage.getIte ...

Giving identification to a pair of elements located within the same column

Struggling with assigning IDs to two elements in a single column - a dropdown and a text element. Managed it in the first scenario, but encountering issues in the second one. Seeking assistance on this matter. Scenario 1: <td> <sele ...

ReactJS tables that can be filtered and sorted

Within my component's state, there exists an array named 'contributors'. Each element within this array is an object containing various properties. My goal is to present this data in a table format, with the added functionality of sorting an ...

Placing a JavaScript button directly below the content it interacts with

I am currently working on a button that expands a div and displays content upon clicking. I am facing an issue where I want the button to always be positioned at the bottom of the div, instead of at the top as it is now, but moving it within the parent div ...

Dynamic refresh of content with Ajax

Recently, I stumbled upon the platform Polyvore and decided to test it out. While experimenting with its "Create a set" feature, I noticed that the site provides a view of items either from your own collection or sourced elsewhere. An interesting observat ...

The TypeScript error occurs when attempting to assign a type of 'Promise<void | Object>' to a type of 'Promise<Object>' within a Promise.then() function

I'm currently working on a service to cache documents in base64 format. The idea is to first check sessionStorage for the document, and if it's not there, fetch it from IRequestService and then store it in sessionStorage. However, I've encou ...

Troubleshooting issues with custom global components failing to apply styling in NuxtJs

I have designed various components such as buttons that I want to be able to use and reuse across my entire website. I have already developed plugins Object.entries(components).forEach((([name, component]) => { Vue.component(name, component) })) and ...

Challenges with handling JSON data in JavaScript

I am currently attempting to retrieve and parse JSON data in order to display it on a blank HTML file. Unfortunately, I keep encountering an issue where if I retrieve and parse the data, I receive an error stating Uncaught TypeError: Cannot read property & ...