我的组件如下所示:

<template>
    <div>
        <div v-if="!loaded">
            <p><i class="fas fa-spinner fa-spin"></i> Loading feed</p>
        </div>

        <div v-else>

            <div data-slider ref="feedSlider" v-if="length > 0">
                <div class="swiper-wrapper">
                    <div class="slide" v-for="record in records" :key="record.id">
                        <slot :record="record"></slot>
                    </div>
                </div>
            </div>

            <div v-else>
                <p>There are no records available.</p>
            </div>

        </div>

    </div>
</template>
<script>
    import Swiper from 'swiper';
    import AjaxCaller from '../../mixins/AjaxCaller';
    export default {
        mixins: [AjaxCaller],
        data() {
            return {
                loaded: false,
                records: [],
                length: 0,
            }
        },
        mounted() {
            this.makeCall(this.success, this.failure);
        },
        methods: {
            success(response) {
                this.loaded = true;
                if (!response.data.records) {
                    return;
                }
                this.records = response.data.records;
                this.length = this.records.length;
                if (this.length < 2) {
                    return;
                }
                setTimeout(() => {
                    this.initiateSlider();
                }, 1000);
            },
            initiateSlider() {
                (new Swiper(this.$refs.feedSlider, {
                    effect: 'slide',
                    slideClass: 'slide',
                    slideActiveClass: 'slide-active',
                    slideVisibleClass: 'slide-visible',
                    slideDuplicateClass: 'slide-duplicate',

                    slidesPerView: 1,
                    spaceBetween: 0,
                    loop: true,
                    speed: 2000,
                    autoplay: {
                        delay: 5000,
                    },
                    autoplayDisableOnInteraction: false,
                }));
            },
            failure(error) {
                this.stopProcessing();
                console.log(error);
            }
        }
    }
</script>

进口的mixin AjaxCaller可与任何其他成分配合使用:

<script>
    export default {
        props: {
            url: {
                type: String,
                required: true
            },
            method: {
                type: String,
                default: 'post'
            }
        },
        data() {
            return {
                processing: false
            }
        },
        computed: {
            getMethodParams() {
                if (this.method === 'post') {
                    return {};
                }
                return this.requestData();
            },
            postMethodData() {
                if (this.method === 'get') {
                    return {};
                }
                return this.requestData();
            }
        },
        methods: {
            requestData() {
                return {};
            },
            startProcessing() {
                this.processing = true;
                this.startProcessingEvent();
            },
            stopProcessing() {
                this.processing = false;
                this.stopProcessingEvent();
            },
            startProcessingEvent() {},
            stopProcessingEvent() {},
            makeCall(success, failure) {
                this.startProcessing();
                window.axios.request({
                        url: this.url,
                        method: this.method,
                        params: this.getMethodParams,
                        data: this.postMethodData
                    })
                    .then(success)
                    .catch(failure);
            }
        }
    }
</script>

以下是我如何在视图中称之为:

<feed-wrapper url="{{ route('front.news.feed') }}">
    <div slot-scope="{ record }">
        <p>
            <a :href="record.uri" v-text="record.name"></a><br />
            <span v-text="record.excerpt"></span>
        </p>
    </div>
</feed-wrapper>

除了IE 11(及更低版本)之外,任何浏览器都可以正常工作.

我得到了

[Vue warn]:无法生成渲染函数:

语法错误:中应有标识符...

它甚至无法在mounted段内执行方法调用.

我用laravel-mixLaravel,所以所有的东西都是用webpackbabel来编译的,所以这不是ES6相关的问题.

我已经花了整整一个晚上试图解开这个谜团,所以任何帮助都将不胜感激.

推荐答案

我知道你已经说过,你不相信这是ES6的问题,但证据表明确实如此.

IE11不支持解构.如果在IE11控制台中键入类似于var {record} = {}的内容,您将看到相同的错误消息"Expected identifier".

try 搜索原始错误消息中的编译代码,并查找单词record.我猜你会发现这样的东西:

fn:function({ record })

如果你看到了,这意味着解构已经进入了浏览器,而没有通过Babel编译.

发生这种情况的确切原因取决于您在何处使用该作用域插槽模板.如果你在一个文件组件中使用它,它应该通过Babel,但如果你没有,那么它可能会在没有传输的情况下进入浏览器.你说你称之为"从视图内部",但这并没有明确说明你是如何使用它的.文档中有一条关于这一点的注释,说明它的价值:

https://vuejs.org/v2/guide/components-slots.html#Destructuring-slot-scope

假设您无法直接解决传输问题(例如,通过将模板移动到它将通过Babel的某个地方),那么您可以删除ES6解构.比如:

<div slot-scope="slotProps">

然后在下面的代码中使用slotProps.record而不是record.

Vue.js相关问答推荐

复选框列未在vutify中排序

设置Vite Vue 3需要多个应用程序输出目录

使用V-TOOLTIP(实例化)使工具提示在V-文本字段中显示清晰的图标

是否可以使用 Vuetify/VueJS 为轮播设置默认图像而不是第一个图像?

在 vue.js 中添加新类时样式不适用

使用 Vue.js 将文件输入绑定到按钮

aspnet core如何在deploy上构建webpack

如何渲染数组以 Select 选项 vue.js

有条件地在 Vue 中添加一个 CSS 类

Vue 两种方式的 prop 绑定

vuex 中 { dispatch, commit } 的类型是什么?

如何有条件地更改 vue/vuetify 文本字段 colored颜色

在 Vuejs 中计算 DOM 元素之间距离的最佳方法是什么?

如何更改 Vuetify 日历日期格式

如何将项目推送到 Vuejs 中数据对象的数组中? Vue 似乎没有关注 .push() 方法

在 VUE JS 的光标位置插入字符

Vue3在按钮单击时以编程方式创建组件实例

使用 svelte js 的原因

Vuex 模块Mutations

在 Vue 中使用事件修饰符 .prevent 提交表单而不进行重定向