Assign the default value of a Vue prop to the options of its parent component

I have a child component that needs to accept a value given in the component's parent's $options object as a possible default value.

In the background, the component should be able to receive its data through a prop or from a configuration. However, there is existing code that expects the data to be passed as an option to the main Vue instance.

The new way of creating the Vue instance looks like this:

new Vue({
    el: '#app',
    render: h => h(MainComp, { 
        props: {
            document: 'data.json'
        }
    })
});

The component is designed to calculate the prop's value from a config file if it is not provided (or set it to a default value of null):

import Config from './config.js';
[...]    
props: {
    document: {
        default: Config.document || null
    },

This setup works well.

However, in the legacy code, the Vue instance used to be created like this:

new Vue({
    el: '#app',
    document: 'data.json',
    render: h => h(MainComp)
});

and the data was accessed in the data() function by using

this.$parent.$options['document']
.

My initial thought was to incorporate this same code into the new default:

default: this.$parent.$options['document'] || Config.document || null

but it turned out that it doesn't work, as the docs mention that "props are validated before a component instance is created, so instance properties [are not] available".

So I attempted to define my "special default" through the data() function like this:

data() {
    return {
        document: document || this.$parent.$options['document']
    };
},

Unfortunately, Vue rightly points out: "The data property "document" is already declared as a prop. Use prop default value instead."

How can I break free from this cycle?

Answer №1

You can try this method:

default: () => this.$parent.$options['document'] || Config.document || null

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

Can TSLint and ESLint give a warning when a function is accessed as a property?

There have been instances where I've made this specific mistake, and I'm curious if there are any ESLint or TSLint rules in place that could detect it. if (this.isBrowser && this.imageURL) {.....} private isBrowser(): boolean{ retur ...

How can multiple buttons be added to each row in Jquery datatables and how can events be applied to them?

I currently have one button, but I am in need of two buttons. These buttons will trigger MySql commands when clicked to retrieve data from the database. How can I set up an event handler for these buttons? $(document).ready(function () { var table= ...

Node.js failing to log chat messages

The goal is to log the chat message entered by the user in the console (terminal). Currently, there seems to be an issue with logging and I'm struggling to debug it. Keep in mind that I am a novice when it comes to NodeJS and only have basic knowledge ...

Turn off all VuetifyJS transitions

Is there a way to completely turn off all transitions, such as the slide-x-transition or dialog modal scale transition, in VuetifyJS? ...

Conversation with form component in angular2

I am currently using ng2-bootstrap-modal. For adding a sample form to the example Confirm Dialog, check out ng2-bootstrap-modal. To change the template: <div class="modal-dialog"> <div class="modal-content"> <form [formGroup]="login ...

AngularJS $scope variable issue

After searching online, I found this code snippet that I tried to implement, but unfortunately, it's not displaying any data. Below is the HTML code: <html data-ng-app=""> <head> <title>Example 4: Using AngularJS Directives an ...

Error: Custom Service is behaving unpredictably

My latest project involves creating a customized service. While the service function is returning data as expected, I encounter an issue when calling it in the controller - it returns 'undefined'. Service: var toDoListServices = angular.module( ...

What is the best way to get the latest props in the midst of an async function?

There's a fascinating open-source project called react-share, where the ShareButton component offers a prop known as beforeOnClick for customization. I've utilized this beforeOnClick to upload images to our CDN before sharing, ensuring that only ...

Tips for showing the chart title and subtitle using "vue-google-charts" library by devstark: Where to position them - top or bottom?

Utilizing the resource available at https://www.npmjs.com/package/vue-google-charts The title is not being displayed on the chart. Is there a way to show it either above or below the chart? I attempted adding "display=true" with the option "title" and fo ...

Float: A threshold reached when adding 1 no longer has any effect

In the realm of programming, float serves as an estimation of a numeric value. For example, 12345678901234567890 === 12345678901234567891 evaluates to true However, 1234567890 === 1234567891 yields false Where does the equation num === num+1 reach a br ...

Altering the hue of a picture

Looking to alter the color of an image on a webpage, specifically a carport. It's crucial that the texture and shadows remain consistent. Swapping out images with different colors would require an excessive amount of images, so I'm seeking advice ...

Looping through a nested for loop within an HTML table

I am currently working on generating a dynamic table using Lists of varying sizes. My first list is a List of Cars List<Car>. I have successfully placed the names of different car companies in the header. Next, I have a List<List<CarSales>& ...

Updating the output of a Vue3 plugin when a property is changed

I am working on displaying localized text that updates based on the selected language. To achieve this, I have developed a plugin with the following function: app.config.globalProperties.$trans = (key) => { return findTranslation(key, app.config.gl ...

Using JavaScript to execute a JSON parse function will not work

I am currently working on this code and would appreciate any assistance. I'm trying to retrieve and parse data from my listApp.json file to display a list with one link. As a beginner, I could use some guidance. <script type = "text/javascript"> ...

Implementing advanced checkbox filtering feature in React

Does anyone have experience with creating dynamic Checkbox filtering in React using Material-UI? I'm finding it challenging because the checkbox options are generated dynamically from incoming data and need to be categorized by type of Select componen ...

Chrome's Vue DevTools are able to detect Vue, but unfortunately no data is being displayed

I'm a Vue beginner and I want to explore the DevTools feature. Recently, I set up a new application using vue cli (without any additional code). The app runs on Vue v2 and my chrome plugin version is v6.0.0 beta 3. vue create my-app While testing the ...

Is there a way to retrieve the list of files from a static public folder using javascript?

I have successfully set up a public folder directory using express and node. For instance, this code works perfectly - var testImage = new Image(); testImage.src = '/images/png/avatar.png'; However, I need to access several images stored ins ...

Creating a customized component using unique styles in Material UI version 1

Currently, I am facing a challenge in styling my Card component with a width of 75 pixels while using Redux and Material UI V1. Despite following the example provided by Material UI on custom styling through withStyles and connect, I have not been able to ...

How can I stretch a background image using jquery to cover the entire document instead of just the window

I have implemented a plugin called https://github.com/srobbin/jquery-backstretch to effectively stretch the background image. The problem arises when the content of the page is long enough to create a scrollbar. In this case, while scrolling, the backgrou ...

Encountering an undefined json array when making an AJAX request

There have been numerous questions on this topic, but none of the specific solutions seemed to apply to my situation. So, I apologize if this is a duplicate query. I am currently working on fetching data from an SQL database using a PHP file that passes t ...