StuBlackett
StuBlackett

Reputation: 3857

Vue JS Conditional Rendering

I have a Icon.vue file that looks as follows :

<template>
    <div class="book-item Icon--container Icon--{{active}}">
        <a href="{{slug}}">
            <img v-bind:src="path" transition="fadein" class="img-responsive"/>
        </a>
        <template v-if="name">
          <div class="info">
            <h4>{{name}}</h4>
          </div>
        </template>
        <template v-if="remove">
          <div class="delete">
            <a href="#">
              <i class="fa fa-trash"></i>
            </a>
          </div>
        </template>
        <template v-if="edit">
          <div class="edit">
            <a href="#" class="cta purple">Edit</a>
          </div>
        </template>
        <template v-if="view">
          <div class="view">
            <a href="#" class="cta purple">View</a>
          </div>
        </template>
  </div>
</template>

<script>
export default
{

  props:{
    info: {},
    remove: {},
    edit: {},
    view: {},
    active: {default:'show'},
    path: {default:''},
    name: {default:'Icon name'},
    slug: {default:'#'},
  },
  data: function() {
    return {}
  },
  methods:{},
  events: {},
  ready:function(e)
  {

  },
  created:function(e)
  {

  }

};
</script>

Also availble on pastebin

As you can see, There is some logic in there for the following :

I am using Laravel and passing variables from my blade template, But how do I set the if to true within the blade template.

For example :

<icon path="{{url('img/admin/add.png') }}" name="" remove="remove"></icon>

Does not add the remove logic. How do I go about doing that, If possible?

Thanks

Upvotes: 0

Views: 972

Answers (1)

user900362
user900362

Reputation:

To add logic from a component first you need to bind an event to an element from this component and then leverage the use of methods inside it (https://vuejs.org/guide/events.html).

The template should look something like:

<icon 
path="{{url('img/admin/add.png') }}" 
name="" 
v-on:click="remove"></icon>

And inside the script:

methods: {
  remove: function () {
    // Do something to remove
  }
}

Good luck!

Upvotes: 1

Related Questions