简体   繁体   English

laravel vue,在刀片模板中使用 vue 代码

[英]laravel vue, using vue code in blade template

I'm trying to access vue component function data in a laravel blade that includes the component but I can't get it to work.我正在尝试在包含该组件的 laravel 刀片中访问 vue 组件功能数据,但我无法让它工作。 Using this code, the site loads to a blank page, but if I remove the @ from the blade span, then the page loads with the autocomplete input and it doesn't access the data使用此代码,站点加载到空白页面,但如果我从刀片跨度中删除@ ,则页面加载自动完成输入并且它不会访问数据

autocomplete.blade.php自动完成.blade.php

<autocomplete :items="items" v-model="item" :get-label="getLabel" :component-item='template' @update-items="updateItems">
</autocomplete>
<div>
    <span>@{{ item.name }}</span>
</div>

<div>
  <autocomplete-component></autocomplete-component>
</div>

autocomplete-component.vue自动完成组件.vue

<script>
export default {
  props: {
    item: {required:true},
    searchText: {required:true}
  },
  data () {
        return {
            item: {id: 9, name: 'Lion', description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.'},
            items: [],
        }
  },
  methods: {
    getLabel (item) {
      return item.name
    },

  }
};
</script>

How can I make it so that the autocomplete div can exist in the blade but use the function and data in the included component?如何使自动完成 div 可以存在于刀片中,但使用包含的组件中的功能和数据?

To do this you need to use scoped slots .为此,您需要使用作用域插槽

In your autocomplete.vue you would need to insert a slot and the provide the slot with the item.在您的autocomplete.vue中,您需要插入一个插槽并为该插槽提供项目。

Roughly speaking -大致说来 -

Autocomplete:自动完成:

<template>
    <slot :item="item"></slot>
</template>

// ...script etc

Blade view:刀片视图:

<autocomplete :items="items" v-model="item" :get-label="getLabel" :component-item='template' @update-items="updateItems">
    <template v-slot:default="slotProps">
        @{{ slotProps.item }}
    </template>

    // OR
    <template v-slot:default="{item}">
        @{{ item }}
    </template>
</autocomplete>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM