【问题标题】:Laravel vue and js in blade dilemma刀片困境中的 Laravel vue 和 js
【发布时间】:2020-05-13 03:16:28
【问题描述】:

猜这是一个愚蠢的问题,但我不知道解决方案。我想将一个 vue 组件和 js 文件加载到刀片视图中。 当我放

<script src="{{ asset('js/app.js') }}"></script>
<script src="{{ asset('js/selectize_settings.js') }}"></script>

它加载 selectize_settings.js 文件,但不加载 vue。当我推迟时

<script src="{{ asset('js/app.js') }}" defer></script>
<script src="{{ asset('js/selectize_settings.js') }}"></script>

它加载 vue 组件,但不加载 selectize_settings.js(ReferenceError: $ 未定义)。我知道这是因为当我加载 selectize_settings.js 时,jquery 还没有通过 app.js 加载。

app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');
import BootstrapVue from 'bootstrap-vue'

Vue.use(BootstrapVue)
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import 'selectize/dist/css/selectize.default.css'

window.$ = window.jQuery = require('jquery')
require('selectize');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('example-component', require('./components/ExampleComponent.vue'));

const app = new Vue({
    el: '#app'
});

test.blade.php

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>
    <script src="{{ asset('js/selectize_settings.js') }}"></script>
    <script src="https://kit.fontawesome.com/4262f4e15a.js" crossorigin="anonymous"></script>

    <!-- Fonts -->
    <link rel="dns-prefetch" href="//fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">


</head>
<body>
    <div id="app">
        <div class="control-group">
            <label for="select-beast">Beast:</label>
            <select id="select-beast" class="demo-default" placeholder="Select a person...">
                <option value="">Select a person...</option>
                <option value="4">Thomas Edison</option>
                <option value="1">Nikola</option>
                <option value="3">Nikola Tesla</option>
                <option value="5">Arnold Schwarzenegger</option>
            </select>
        </div>
        <div class="vue-comp">
            <example-component></example-component>
        </div>
    </div>
</body>
</html>

selectize_settings.js

$( document ).ready(function() {
    $('#select-beast').selectize({
        create: true,
        sortField: {
            field: 'text',
            direction: 'asc'
        },
        dropdownParent: 'body'
    });
});

ExampleComponent.vue

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Example Component</div>

                    <div class="card-body">
                        I'm an example component.
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

我希望有人可以提供帮助。问候!

【问题讨论】:

  • 您是否仅将 jQuery 用于选择插件?也许你想看看这个插件,它基本上是用 VueJS 制作的 selectize.js:github.com/isneezy/vue-selectize

标签: javascript laravel vue.js


【解决方案1】:

在你的 app.js 中试试这个

//...
global.$ = global.jQuery = require('jquery');
//...

你也可以删除 defer attr

<script src="{{ asset('js/app.js') }}"></script>

【讨论】:

  • 谢谢回答。它仍然给我 Uncaught ReferenceError: $ is not defined 错误
【解决方案2】:

试试这个:

Vue.component('example-component', require('./components/ExampleComponent.vue').default);

【讨论】:

  • 谢谢回答。不幸的是仍然给出 [Vue warn]: Cannot find element: #app error when I don't use defer
【解决方案3】:

好的,当你把 defer 放到 js 文件中时它也可以工作!

<script src="{{ asset('js/app.js') }}" defer></script>
<script src="{{ asset('js/selectize_settings.js') }}" defer></script>

【讨论】:

    猜你喜欢
    • 2017-06-22
    • 2020-09-22
    • 1970-01-01
    • 2020-08-14
    • 2019-08-02
    • 2012-12-05
    • 2017-07-09
    • 2019-07-08
    • 2021-01-28
    相关资源
    最近更新 更多