Vite(在Laravel)和Alpinejs不起作用

发布于 2025-02-13 13:13:21 字数 934 浏览 0 评论 0原文

根据迁移说明,我从Laravel Mix转移到Laravel Vite。除高山外,一切都在编译和工作。在我的控制台中,我会得到以下内容。

参考:找不到变量:alpine`

Resources/app.js

import Alpine from 'alpinejs';
window.Alpine = Alpine;

// Stores need to be defined before Alpine.start()
import './alpine-stores/modal';

Alpine.start();

Alpine Stores/Modal

Alpine.store('modal', {
    active: false,
    id: null,
    open(id) {
        this.active = true
        this.id = id
        document.body.classList.add('overflow-hidden')
        window.dispatchEvent(new CustomEvent('modalopened'))
    },
    close() {
        this.active = false
        this.id = null
        document.body.classList.remove('overflow-hidden')
        window.dispatchEvent(new CustomEvent('modalclosed'))
    }
})

I've moved from Laravel Mix to Laravel Vite per the migration instructions. Everything is compiling and working, except for Alpine. In my console I get the following.

ReferenceError: Can't find variable: Alpine`

resources/app.js

import Alpine from 'alpinejs';
window.Alpine = Alpine;

// Stores need to be defined before Alpine.start()
import './alpine-stores/modal';

Alpine.start();

alpine-stores/modal

Alpine.store('modal', {
    active: false,
    id: null,
    open(id) {
        this.active = true
        this.id = id
        document.body.classList.add('overflow-hidden')
        window.dispatchEvent(new CustomEvent('modalopened'))
    },
    close() {
        this.active = false
        this.id = null
        document.body.classList.remove('overflow-hidden')
        window.dispatchEvent(new CustomEvent('modalclosed'))
    }
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2025-02-20 13:13:21

由于VITE,我们不能使用要求语句。因此,在分离数据和存储的同时更加复杂。我无法在Alpinejs网站上找到任何官方信息。我还经历了相同的问题,并以这种方式解决了这种类型的问题。

Alpine-data

export default () => ({ 
    active: false,
    id: null,
    open(id) {
        this.active = true
        this.id = id
        document.body.classList.add('overflow-hidden')
        window.dispatchEvent(new CustomEvent('modalopened'))
    },
    close() {
        this.active = false
        this.id = null
        document.body.classList.remove('overflow-hidden')
        window.dispatchEvent(new CustomEvent('modalclosed'))
    }
})

资源/app.js

import Alpine from 'alpinejs'
window.Alpine = Alpine

// Stores need to be defined before Alpine.start()
import modal from './alpine-stores/modal.js'
Alpine.data('modal', modal)

Alpine.start()

高山商店不要使用箭头功能

export default ({
    .....
})

Due to vite we cannot use require statement. So while separating file for data and store is more complicated. I cannot found any official information on alpinejs website. I also pass through same problem and solve this type of problem in this way.

alpine-data

export default () => ({ 
    active: false,
    id: null,
    open(id) {
        this.active = true
        this.id = id
        document.body.classList.add('overflow-hidden')
        window.dispatchEvent(new CustomEvent('modalopened'))
    },
    close() {
        this.active = false
        this.id = null
        document.body.classList.remove('overflow-hidden')
        window.dispatchEvent(new CustomEvent('modalclosed'))
    }
})

resources/app.js

import Alpine from 'alpinejs'
window.Alpine = Alpine

// Stores need to be defined before Alpine.start()
import modal from './alpine-stores/modal.js'
Alpine.data('modal', modal)

Alpine.start()

For alpine store don't use arrow function

export default ({
    .....
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文