0

I'm facing a problem with importing Objects from the App.vue file to a component. But first I should explain the purpose of this project. There's a component (navigation-drawer) and an App.vue file. The Navigation drawer has vue props in it, which you can dynamically change in the App.vue file. The problem with that is that I can only use as many links as there are in the Navigation-Drawer file.

I would like to edit it so I can use as many links as I need, without even having to open the Navigation-Drawer.vue file. Before I go into more detail, here are the files with the props & limited amount of links:

App.vue

<template>
  <div id="app">
    <navigation-drawer
    name1="TFBern"
    name2="Stackoverflow"
    name3="YouTube"
    name4="Google"
    link1="https://vuejs.org"
    link2="https://stackoverflow.com"
    link3="https://youtube.com"
    link4="https://google.com"
    />

    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>

  </div>
</template>

<script>
  import HelloWorld from './components/HelloWorld.vue'
  import NavigationDrawer from './components/Navigation-Drawer.vue'

  export default {
    name: 'App',
    components: {
      HelloWorld,
      NavigationDrawer
      }
    }
</script>

Navigation-Drawer.vue

<template>
    <div class="navigationdrawer">
        <span @click="openNav" style="fontsize:30px;cursor:pointer;display:flex;justify-content:center;">&#9776;</span>
        <div id="mySidenav" class="sidenav">
            <a href="javascript:void(0)" class="closebtn" @click="closeNav">&times;</a>
            <a v-bind:href="link1">{{ name1 }}</a>
            <a v-bind:href="link2">{{ name2 }}</a>
            <a v-bind:href="link3">{{ name3 }}</a>
            <a v-bind:href="link4">{{ name4 }}</a>
        </div>
    </div>
</template>

<script>
export default {
    name: 'NavigationDrawer',
    props: {
        name1: String,
        name2: String,
        name3: String,
        name4: String,
        link1: String,
        link2: String,
        link3: String,
        link4: String
 },

 methods: {
     openNav() {
         document.getElementById('mySidenav').style.width = '15%'
         },

    closeNav() {
        document.getElementById('mySidenav').style.width = '0%'
        }
    }
 }

</script>

Now, what I had in mind was to create a js object, which can import the links from App.vue into the Drawer. Something like this:

<navigation-drawer links="[ {title="Google", link="www.google.ch"} , {title="Youtube", link="www.youtube.com"} , {title=…, link=…} ]"

I don't really know how to do it... Can anyone help?

Thank you.

1 Answer 1

1

You are pretty close to the answer already. Change = to :, the values to be surrounded with ' instead of " so you have a list of objects

<navigation-drawer v-bind:links="[ {title:'Google', link:'www.google.ch'} , {title:'Youtube', link:'www.youtube.com'} , {title:…, link:…} ]"

Then the navigation-drawer props look like:

props: {
    links: Array
},

and the html loops through the links with a v-for and template:

<div class="navigationdrawer">
    <span @click="openNav" style="fontsize:30px;cursor:pointer;display:flex;justify-content:center;">&#9776;</span>
    <div id="mySidenav" class="sidenav">
        <a href="javascript:void(0)" class="closebtn" @click="closeNav">&times;</a>
        <template v-for=v-for="(link, index) in links">
            <a v-bind:href="link.link"  :key="index">{{ link.title}}</a>
        </template>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.