Using Vue.js to utilize a component within another component

I have a component that I would like to utilize within another component in order to create a navigation menu. I am looking to separate the menu from the links. How can I display data in a child component within another parent component while using Vue.js CDN? I am new to using vue.js so any help is appreciated. Thank you.

index.html

<div id="navbar"><navbar-div></navbar-div></div>

navbar.js:

Vue.component('navbar-content', {
  props: ['name', 'link'],
  template: '<a class="navbar-item" v-bind:href="link">{{ name }}</a>'
})

Vue.component('navbar-div', {
  props: ['links'],
  template: `
    <nav class="navbar is-dark">
     <div class="navbar-brand">[...]</div>
     <div class="navbar-menu">
       <nav class="navbar-start">
         <navbar-content v-for="item in links"
               v-bind:key="item.id"
               v-bind:name="item.name"
               v-bind:link="item.link">
        </navbar-content>
       </nav>
     </div>
    </nav>
  `})


new Vue({
    el: "#navbar",
    data: {
       links: [
                {id: 1, name: "Item 1", link: "link1"},
                {id: 2, name: "Item 2", link: "link2"}
            ],
        }
})

Answer №1

Ensure in your HTML that you connect the links to the navbar-div; if not, it won't be able to receive the data as props

Vue.component('navbar-content', {
  props: ['name', 'link'],
  template: `
    <a
      class="navbar-item"
      :href="link"
    >
      {{ name }}
    </a>
  `
})

Vue.component('navbar-div', {
  props: ['links'],
  template: `
    <nav class="navbar is-dark">
      <div class="navbar-brand">[...]</div>
      <div class="navbar-menu">
        <nav class="navbar-start">
          <navbar-content
            v-for="item in links"
            :key="item.id"
            :name="item.name"
            :link="item.link"
          />
        </nav>
      </div>
    </nav>
  `
})

new Vue({
  el: "#navbar",
  data: {
    links: [{
        id: 1,
        name: "Item 1",
        link: "link1"
      },
      {
        id: 2,
        name: "Item 2",
        link: "link2"
      }
    ],
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="navbar">
  <navbar-div :links="links"></navbar-div>
</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

Having Trouble Retrieving and Updating Records in MS CRM?

My current situation involves working with Microsoft Dynamics coding for the first time, and I am in need of a solution for my code. I have established two entities called Acc and Con, where Acc serves as the parent entity and Con is the child entity of A ...

The dimensions of the d3 div remain constant despite any modifications to its attributes

In my angular application, I am trying to customize the width and height of div elements in d3 when I select a legend. Strangely, I am able to adjust the width and height of the svg element without any issues. Here is the issue illustrated: https://i.ssta ...

Tips for adjusting the position of rows within a v-data-table - moving them both up and down

Is there a way to rearrange rows up and down in the table? I've been using the checkbox feature and the CRUD data table from the documentation, but I haven't found any examples on how to implement row movement. Currently, my v-data-table setup l ...

Strange issue: the code appears to be running multiple times with just one click

I've implemented a commenting system with a like feature. However, I'm facing an issue where sometimes clicking the like link results in sending multiple requests (up to 8-9) per click. This problem also occurs with another jQuery code that is tr ...

Paper.js - generate a collection of movable shapes

I am attempting to use paper.js to create draggable shapes where the index of each shape is provided during dragging. Unfortunately, I keep encountering an error that says "Uncaught TypeError: Cannot read property 'position' of undefined". If a ...

Obtain Page Parameters within a Nested Next.js Layout using App Router

My Next.js App Router has a nested dynamic page structure using catchall routes configured like this: app/stay/ |__[...category] | |__page.js |__[uid] | |__page.js |__layout.js Within the 'layout.js' file, there is a con ...

Utilize dropdown1 to dynamically populate dropdown 2 in AngularJS

Here is the HTML code snippet I am currently working with: <select ng-controller="category" ng-model="selectedTestAccount" ng-options="c.category for c in categories track by c.categoryID" ></select> <select ng-controller="subcategory" ng ...

The utilization of useEffect causes the page to go blank

Essentially, the issue is that once I include useEffect(() => { const fetchData = async () => { const result = await fetch('http://localhost.com/ping'); console.log(result) }; fetchData(); }, []); in my compone ...

calling this.$emit results in a TypeError being thrown

I have a specific requirement for my Vue3 component where I need to emit an event during a method call. The code structure is as follows: export default { emits: ['event'], methods: { myMethod () { this.$emit('event') // t ...

Tips for refreshing jQuery to ensure it functions smoothly for subsequent tasks

I am facing an issue with running a second process under JQuery, as shown in the code snippet below: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <script type=" ...

Tips for concealing the final click (add) tab after it has been clicked and revealing the hidden (add) button when the user clicks on the remove button

I have created a form where users can add tests. Everything is working smoothly but I want the previous "Add Another Test" field to be removed when the user clicks on it and a new field to be shown. Everything is running well, but the issue is that when t ...

Is it possible to retrieve correctly formatted JSON data from this API?

I need help retrieving data from the API located at . When I access the API directly, it returns an array wrapped in <pre></pre> tags, rather than in JSON format. I would like to use an AJAX call to retrieve this data instead of using PHP. Is t ...

Automated system is responsible for flagging and disabling comments

We are facing a puzzling issue at the moment. Our comments form allows users to submit their thoughts on news articles, and each submission is immediately accepted and displayed on the same page. Within each comment, there is a link that enables users to ...

Having trouble determining the total amount in my online shopping cart

I am facing an issue with the shopping cart I created, which consists of two components (Productlist and Cart List). When I click on the 'add to cart' button in the Product List, it successfully moves into the service file and then to the Cart Li ...

When using Owl Carousel in Chrome, the carousel unexpectedly stops after switching tabs

Hi there, I'm currently testing out the auto play feature in owl carousel. One issue I've encountered is that when I switch to another tab in Chrome and then return to my webpage with the carousel, it stops functioning unless I manually drag the ...

Using R plotly to show an image when hovering over a location on a map

After successfully implementing the technique of displaying an image on hover in a regular R plotly chart, I encountered issues when trying to apply the same method to a plotly map. Specifically, the approach broke down and failed to display the image. Can ...

Unable to modify document value in MongoDB using Node.js

Currently, I am attempting to fetch the value of a field form that is being sent to a subroute and utilize it to update a database collection. My ability to retrieve the form value and manipulate it is working fine; however, I encounter an issue when I sol ...

Eliminate list items with a keyboard stroke

I am currently developing a straightforward todo list application using JavaScript. The main functionality I am trying to implement is the ability to add new items from an input field to a list, as well as the option to remove items from the list. While ...

What is the functionality of the disabled attribute on an option tag within a dropdown select menu?

I am working with a code snippet that involves understanding how the attribute:disabled functions on an <option> within a <select> element! Let's say I have a dropdown for ratings and I select the 5-star option (★★★★★). Upon sel ...

Guide to utilizing various functions, with equations, within a specific scope in angularJS

myApp.controller('incomeController', ['$scope', function($scope) { $scope.pay = 0; $scope.hours = 0; $scope.tax=0.19297; $scope.total = function() { return $scope.pay * $scope.hours;} $scope.taxTotal ...