Unveiling the secret to accessing properties using v-if within a custom component template relocation

I'm currently working on an application that reveals a hidden div when the text "Create Test" is clicked. Everything works well when the code isn't placed inside the template of a component. This seems strange to me, what could be causing this issue? I really want to use a template as I plan to reuse the code.

Check out this example (using regular HTML + vue.js) to see how I expect it to work: https://jsfiddle.net/Lrwfssn1/7/

HTML

<div id="testContainer">
    <div id="teacherMain">
        <h2>Welcome to the</h2>
        <h1> PRO Test Application</h1>
    </div>
    <test></test>
</div>

Vue.js

Vue.component('test', {

    template: `
    <div id="tCreateTest">
        <div id="createTestContainer" @click="seen = !seen">
            <h2>Create</h2>
            <h1 id="testH1">Test</h1>
            <div class="text-left">
                <div class="col-sm-10 form-group-lg">
                    <div v-if="seen" id="testDetails">
                        <form class="form-group">
                            <label id="titleLabel" class="form-control-label" for="titleInput">Enter title:</label>
                            <input type="text" class="form-control form-control" id="titleInput"
                                   placeholder="Title">
                        </form>
                        <button class="btn btn-outline-success" id="submitTitle" onclick="testDetails()" type="submit">
                            Submit
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    `,
});

new Vue({
    el: '#tCreateTest',
    data: {
        seen: true
    }
});

Answer №1

Pass the prop down to the component by following these steps:

  • Add props: ['visible'], inside the component
  • Add the binding in the parent component: :visible="visible"

Here is a demonstration of how to implement this:

Vue.component('test', {
  props: ['visible'],
  template: `
    <div id="tCreateTest">
        <div id="createTestContainer" @click="visible = !visible">
            <h2>Create</h2>
            <h1 id="testH1">Test</h1>
            <div class="text-left">
                <div class="col-sm-10 form-group-lg">
                    <div v-if="visible" id="testDetails">
                        <form class="form-group">
                            <label id="titelLabel" class="form-control-label" for="titleInput">Enter title:</label>
                            <input type="text" class="form-control form-control" id="titleInput"
                                   placeholder="title">
                        </form>
                        <button class="btn btn-outline-success" id="submitTitle" onclick="testDetails()" type="submit">
                            Submit
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    `,
});

new Vue({
  el: '#testContainer',
  data: {
    visible: true
  }
});
<script src="https://unpkg.com/vue"></script>
<div id="testContainer">
  <div id="teacherMain">
    <h2>Welcome to the</h2>
    <h1> PRO Test Application</h1>
  </div>
  <test :visible="visible"></test>
  
  <br>
  <button @click="visible = !visible">Toggle visibility in parent component</button>
</div>

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 is preventing this from retrieving the source code?

I'm trying to retrieve the HTML source code from http://yahoo.com/(index.html) and display it in a dialog box using this code snippet: $.ajax({ url: 'http://yahoo.com', success: function(data) { alert(data); } }); Unfortunately, ...

Is the create attachment function of the Microsoft Graph API not functioning properly?

I've been trying to create an attachment for my messages by following the documentation provided, but unfortunately, the API seems to be giving me some trouble. I referred to the document at for guidance. Below is the JavaScript code that I have bee ...

What is the best way to dynamically retrieve a URL and utilize it as a hyperlink using jQuery?

Is there a way to retrieve the current URL and use it as a link in jQuery? For example, if my browser is currently on page mysite.com/index.php?page=post&see=11, I would like to use this URL in my link and append /new/ after the domain name, like so: ...

JQuery Form Validation - Detecting Input Changes

Currently, I have a form set up with jQuery validation that works well, but I want to enhance its functionality. The form uses a plugin for validation, but it only checks for errors upon submission. I'm interested in finding a way to validate the fiel ...

Encountered a problem while initiating the HTTP server in a node.js

I'm currently attempting to initiate an HTTP server from my index.js file. After going through The Node Beginner tutorial, I've been encountering errors every time I try to start the server using the provided code: Server.js - var http = re ...

Tips for Properly Positioning Floating Pop-Up Messages Using CSS and jQuery

I was experimenting with adding a button that triggers a popup message to my website. I followed a coding tutorial using jQuery and CSS, which looks like this: Javascript : !function (v) { v.fn.floatingWhatsApp = function (e) {...} }(jQuery); CSS : .fl ...

Traverse through the nested JSON array using a loop

Exploring a JSON object: { "date_price": [ { "date": "Jan 2000", "price": 1394.46 }, { "date": "Feb 2000", "price": 1366.42 }, { "date": "Mar 2000", "price": 1498.58 }, { "date": "Apr ...

Unexpected outcomes arising from using nested arrays with jQuery validation plugin

I have been utilizing the jQuery validation plugin from . I am encountering an issue with validating a nested array "tax_percents[]" that is within another array. The validation seems to only work for the first value in the array, and even for the second f ...

Despite the value being true, the if statement does not work when used with a Mongoose object in Node.js

Currently working on a bidding app, Below is the schema for the bidding model const bidSchema = new mongoose.Schema({ name: String, price : Number, description: String, location: String, specilization: String, image: String, ...

Is it possible to retrieve the vertices array from a QuickHull instance in three.js?

I'm currently working on generating a geometry using QuickHull from a THREE Mesh. However, it seems that the QuickHull object only contains information pertaining to the Faces of the mesh. Does anyone know if there is a way to access the vertex infor ...

Developing various VueJS TypeScript projects utilizing a shared library

In the process of developing two VueJS applications using TypeScript, I have created one for public use and another as an admin tool exclusively for my own use. Both applications are being built and tested using vue-cli with a simple npm run serve command. ...

One-time PHP form submission only

On my website, I offer a variety of PDF download options. To access these downloads, I encourage visitors to sign up using a form. After signing up, they receive a direct link to the chosen PDF in their email. My goal is to streamline the process so that ...

What causes the appearance of 'GET/ 304 --' in my code? (vue.js, express)

While attempting to fetch data on the client-side using axios in vue.js, I encountered a server-side error with the code 'GET/ 304 --' The reason for this occurrence is unclear to me and I am unsure of how to troubleshoot or resolve it. If I re ...

Guide on crafting a scrollable and touch-responsive grid using CSS and JavaScript

I am seeking guidance on creating a grid with a variable number of columns and rows. It should be contained within a separate div and should not interfere with other objects or alter the parent's size. The grid needs to be square and I am currently u ...

Having trouble developing a custom jQuery tool for textareas

I am currently attempting to customize this script that mimics the Word 2007 minibar within a textarea. My approach involves encapsulating it in a plugin, but I am encountering an issue where it does not function properly with multiple textareas. If you w ...

The setAttribute method does not function with getElementByClass()

Can someone please help me understand why this code is not working for me: document.getElementByClass('home1').setAttribute('style', 'background-image:url(img/red_menu.PNG);'); I have confirmed that I do indeed have an eleme ...

whenever I execute my `node file.js 1 23 44` command, nothing is displayed on the screen

When I execute node file.js 1 23 45, the expected output is supposed to be One TwoThree FourFive. However, for some unknown reason, nothing is being printed out. The script appears to run without any errors, but the desired output is just not appearing. ...

Utilizing Lodash in TypeScript to merge various arrays into one cohesive array while executing computations on each individual element

I am working with a Record<string, number[][]> and attempting to perform calculations on these values. Here is an example input: const input1 = { key1: [ [2002, 10], [2003, 50], ], }; const input2 = { key ...

Material-UI Autocomplete can only save and display one specific property of an object at a time

Can someone help me with implementing Autocomplete from Material-UI? I want to store only a specific property value and not the entire object. For example, let's say I want to store the property Value, but it could be any other property as well. con ...

Searching to loop through JSON within a Sequelize / Postgres database query in order to identify a corresponding value

Hello everyone, I'm new here so please bear with me. I'm currently working on a project using Sequelize with PostgresDB in Node and I need to search for a contact by email using findOrCreate function. The email entry is stored in JSON format (se ...