"Using VueJS Slot directive to display dynamic content with v

Displaying a variable in the default scope - this method is functional.

<slot>{{namedSlotText}}</slot>   

However, attempting it in another way results in the variable not being displayed.

Template

<div id="app">
  <my-component></my-component>
</div>

Code

let vm = new Vue({
  el:"#app",
  components:{    
    myComponent:{
      template:`
        <div>
         <slot :text="namedSlotText"></slot>
        </div>
      `,
      data:()=>({
        namedSlotText: "This is default slot text"
      })
    }
  }
})

I have seen instances where v-text has been successful.

Answer №1

When you provide a property to a slot, you are essentially offering data for use within the slot's content. This concept is known as scoped slots.

Take a look at this code snippet:

<div>
  <slot :text="namedSlotText"></slot>
</div>

This code snippet is solely providing access to the text property for use in the slot's content. To actually utilize this property, you must define a scoped template and then specifically refer to the property within the content.

<my-component>
  <template scope="{text}">
    {{text}}
  </template>
</my-component>

Answer №2

Avoid using arrow functions to declare data in Vue components. The correct ES2015 syntax for defining data inside components is as follows:

data() {
  return {
    namedSlotText: "This is default slot text"
  }
}

If you are using a standard slot, you can set the default value like this:

Vue.component('my-component', {
  template: `<div><slot>{{slotText}}</slot></div>`,
  data() {
    return {
      slotText: "This is default slot text"
    }
  }
})

Check out the JSFiddle example here: https://jsfiddle.net/va438nqk/

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 me from concealing my input field when its type is set to "file"?

I've been attempting to conceal my input field of a file type, but even with the hidden attribute, it refuses to hide. https://i.sstatic.net/jyMrn.png https://i.sstatic.net/P59wV.png Interestingly, once I remove type="file", the code succe ...

Encountering a issue of not finding the cli.json file while setting up a fresh Vue project with

Encountering issues with missing cli.json file while trying to initialize a new Vue project using Amplify. Following the steps in the official tutorial Managed to get to the point of Initializing a new backend and executing amplify init: https://i.sstati ...

Getting dynamic props from a clicked element in React involves accessing the target element's properties and

I am currently working with a React "tree" menu component that has main links with submenus generated dynamically through a JSON GET call. Upon inspecting the tree in the React Inspector, I noticed that each element has multiple props associated with it. H ...

Exploring ways to repeatedly collapse rows using HTML, CSS, and JavaScript

GOAL: I want to freeze the header, freeze the first column, and be able to collapse rows multiple times. CURRENT PROGRESS: I have achieved freezing the header, first column, but can only collapse rows once. MY CODE SNIPPET: </head> <body> &l ...

If I click on the datalist link option during an ajax append, I would like to be redirected

When I try to link after clicking an option in the appended AJAX, it does not seem to work as expected: More details: $('#input-friends').on('input', function () { var id = $('#input-friends').val(); $.ajax({ ...

The Angular 2 Router's navigation functionality seems to be malfunctioning within a service

Currently, I am facing an issue with using Angular2 Router.navigate as it is not functioning as expected. import { Injectable } from '@angular/core'; import { Http, Headers } from '@angular/http'; import { Router } from '@angular/ ...

Internet Explorer 7 is having trouble loading JavaScript

I'm encountering an issue with loading JQuery in IE7, it works smoothly in all other browsers such as Firefox, Safari, Opera, and IE8, except for IE7. If anyone has any insights on why this might be happening, please share your thoughts. Thank you, ...

save the received data to a new document

How can I pass the data returned from the API below to another function in a separate JavaScript file? This question may be similar to others, but those do not involve working with returned results. Please consider this before labeling it as a duplicate. ...

Setting a const variable back to its original value in VueJS

Is there a way to reset the constant variable value in Vue? Here's what I have: data(){ const _hdrList = [ { label: 'start_time', value: 'start_time' }, { la ...

The issue of a non-firing Ajax click event in a JavaScript file

I have set up a table in JSP and am attempting to trigger a function when clicking on the table rows to post the data. I created a JavaScript file with an ajax click event, but unfortunately, the event is not being fired. $(document).ready(function( ...

What is the best way to implement Google reCAPTCHA v3 with Vuetify?

I am interested in integrating Google Recaptcha 3 into my Vuetify project. Here is an example of the Google Recaptcha 3: https://i.sstatic.net/IYT9L.png I found a reference here: https://www.npmjs.com/package/vue-recaptcha-v3 First, I installed vue-rec ...

Cannot locate module using absolute paths in React Native with Typescript

I recently initiated a new project and am currently in the process of setting up an absolute path by referencing this informative article: https://medium.com/geekculture/making-life-easier-with-... Despite closely following the steps outlined, I'm en ...

Issue with Node Canvas/Resemble.js: The image provided has not finished loading during the load operation

Recently, I encountered a challenge while trying to utilize Resemble.js in a node environment. Despite some initial complications with installing canvas/cairo due to OS X Mavericks/XQuarts and Homebrew issues, I eventually succeeded. After making signific ...

What is the best way to transmit a response from PHP to Ajax?

A JavaScript function is used here that utilizes the POST method to send form data to PHP. The PHP script then checks this data in a database to verify its authenticity. However, there seems to be confusion on how to convey the response from PHP back to Ja ...

Ensure that your package.json and Gruntfile.js are stored in separate directories to optimize your project structure

Struggling to implement Grunt across different folders, my root directory includes: <root>/package.json <root>/node_modules Within another folder, I have my Gruntfile along with various subfolders and files where I work: <root>/apps/stat ...

The Chrome browser on iOS does not fire the Image onLoad event

In my web application, I am utilizing Scala.js and d3.js (scala-js-d3) to generate an SVG. However, I have encountered a problem with the background image not triggering the load event when using Chrome on iOS (iPhone), even though it works fine on Windows ...

Is there a way to access an HTML element using Vue's methods?

Here is an example of my Vue component structure: <template> ... </template> <script> export default { methods: { buildDescription () { if (!this.description) { const div = document.createEl ...

What is preventing me from setting the moment locale in node.js?

I have a node-app running, and the contents of my app.js file are as follows: var moment = require('moment'); moment().locale('fr'); console.log(moment.locale()) Despite expecting the output to be 'fr', it actually displays ...

What could be causing the test runner to not find the plugin?

I've been experimenting with Test Runner for running tests and attempting to utilize the sendKeys function. Below is the test code, similar to what is provided on the website: import { sendKeys } from '@web/test-runner-commands'; it(' ...

Converting strings to different data types: undefined, null, number, or boolean

Is there a more efficient and faster method to convert a string to its respective value type? I usually rely on this function for type conversion: var convertType = function (value){ if (value === "undefined") return undefined; if (value === "nul ...