Laravel Vue和JS在刀片困境中

时间:2020-01-27 17:47:34

标签: javascript laravel vue.js

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

import React from "react";
import { StylesProvider } from "@material-ui/core/styles";

import Tooltip from "@material-ui/core/Tooltip";
import styled from "@emotion/styled";

const StyledTooltip = styled(({ className, ...other }) => (
  <Tooltip classes={{ tooltip: className }} {...other} />
))`
  font-size: 2em;
  color: blue;
  background-color: yellow;
`;
export default function App() {
  return (
    <StylesProvider injectFirst>
      <StyledTooltip title="Test tooltip">
        <span>Hover over me</span>
      </StyledTooltip>
    </StylesProvider>
  );
}

它将加载selectize_settings.js文件,但不会加载vue。当我把延期

     map $request_method $req_post_limit_per_ip {
      default    "";
      POST       $remote_addr;
    }


    map $request_method $req_get_limit_per_ip {
      default    "";
      GET       $remote_addr;
    }

    limit_req_zone $req_post_limit_per_ip zone=post_limit_per_ip:10m rate=100r/s;
    limit_req_zone $req_get_limit_per_ip zone=get_limit_per_ip:10m rate=1000r/s;  

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

app.js

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

test.blade.php

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

selectize_settings.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'
});

ExampleComponent.vue

<!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>

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

3 个答案:

答案 0 :(得分:0)

在您的app.js中尝试

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

您也可以删除延期属性

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

答案 1 :(得分:0)

尝试一下:

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

答案 2 :(得分:0)

好的,当您将defer放到js文件中时,也可以使用!

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