【问题标题】:Mouse Hover in VueJS component implementation in LaravelLaravel 中 VueJS 组件实现中的鼠标悬停
【发布时间】:2020-01-21 08:14:38
【问题描述】:

当我在 Laravel 上使用鼠标悬停时,它不起作用。

我的Vue文件存放在resources/js/Dashboard.vue

到目前为止,我尝试将v-on:mouseover 更改为@mouseover,但仍然无法正常工作。

结果是当我悬停按钮时它不会更改文本。

我做错了什么,我该如何解决?

下面是我Dashboard.vue文件的内容;

<template>
<div id="mouse">
  <a
    v-on:mouseover="mouseover"
    v-on:mouseleave="mouseleave">
    {{message}}
  </a>
</div>
</template>
<!--Force Override of the css style for datepicker class-->

<style>

  #mouse {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: block;
    width: 280px;
    height: 50px;
    margin: 0 auto;
    line-height: 50px;
    text-align: center;
    color: #fff;
    background: #007db9;
  }
</style>

<script>

export default {
    components: {
    },
    data() {
        return {
          showAudience: false,
          message: 'Hover Me',
        }
    },
    computed: {
    },
    methods : {
    mouseover: function(){
      this.message = 'Good!'
    },    
    mouseleave: function(){
      this.message = 'Hover Me!'
    }
    },
    mounted() {
    },
}
</script>

这里是我的Blade.php文件的内容;

@extends('layouts.app', ['pageSlug' => 'dashboard'])

@section('content')
  <dashboard-component></dashboard-component>
  @endsection

@push('js')
    <script src="{{ asset('black') }}/js/plugins/chartjs.min.js"></script>
    <script src="{{ asset('js/app.js') }}"></script>
    <script>
        $(document).ready(function() {
          //demo.initDashboardPageCharts();
        });
    </script>
@endpush

仔细查看并在本地复现,下面是package.json文件;

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "axios": "^0.19",
        "bootstrap": "^4.1.0",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^4.0.7",
        "lodash": "^4.17.13",
        "popper.js": "^1.12",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.15.2",
        "sass-loader": "^7.1.0",
        "vue": "^2.6.10",
        "vue-template-compiler": "^2.6.10"
    },
    "dependencies": {
        "chart.js": "^2.8.0",
        "friendly-errors-webpack-plugin": "npm:@soda/friendly-errors-webpack-plugin@^1.7.1",
        "vue-chartjs": "^3.4.2",
        "vue2-datepicker": "^2.12.0"
    }
}

编辑添加了我的 app.js

require('./bootstrap');
window.Vue = require('vue');
import Vue from 'vue'
import App from './components/App.vue'

/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

// const files = require.context('./', true, /\.vue$/i);
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default));
Vue.component('dashboard-component', require('./components/Dashboard.vue').default);
Vue.component('base-table', require('./components/base/BaseTable.vue'));

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


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

【问题讨论】:

  • 这可能是个愚蠢的问题,但你是否将你的 javascript 文件添加到 webpack 并运行 npm run dev
  • 每当我更改 vue 文件中的某些内容时,我都会执行 npm run dev
  • 而我的 webpack.mix.js 已经包含 mix.js('resources/js/app.js', 'public/js') .sass('resources/sass/app.scss', 'public/css');
  • 好吧,这只是一个快速的想法,因为我一直忘记这些小事。你检查你的控制台日志,那里有任何错误吗?不妨在脚本中添加一些简单的 console.log,看看它是否初始化。还有一个建议,如果您使用的是 google chrome,有一个不错的 vue.js 扩展,可以更轻松地进行预览和调试。
  • 遗憾的是,控制台日志中没有错误。 :( 我真的不知道我还能做什么或我还需要做什么。它只是一个简单的代码,我不知道我做错了什么

标签: javascript php laravel vue.js


【解决方案1】:

尝试添加这些以防止默认事件和传播

v-on:mouseover.prevent.stop="mouseover"
v-on:mouseleave.prevent.stop="mouseleave"

【讨论】:

    【解决方案2】:

    代码有效,当我在我的系统上运行时它有效。

    我在 JSFiddle 上试过。

    //html
    <div id="mouse" class="mouse1">
     <a
        v-on:mouseover="mouseover"
        v-on:mouseleave="mouseleave">
        {{name}}
      </a>
    </div>
    //CSS
    #mouse {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        display: block;
        width: 280px;
        height: 50px;
        margin: 0 auto;
        line-height: 50px;
        text-align: center;
        color: #fff;
        background: #007db9;
      }
    
    //js 
    new Vue({
      el: "#mouse",
      data: {
       name: 'Hover Me!',
         showAudience: false,
      },
      methods : {
        mouseover: function(){
          this.name = 'Good!'
        },    
        mouseleave: function(){
          this.name = 'Hover Me!'
        }
        },
        mounted(){
            }    
    })
    

    【讨论】:

      【解决方案3】:

      整洁。看起来当按钮在动画期间改变大小时,鼠标进入和退出悬停状态,因为边缘正在移动。

      我在每个按钮周围添加了一个 div,并将悬停触发器附加到 div 而不是按钮

      <body>
          <div id="app">
              <h1>Hover the mouse near the border</h1>
              <hr>
              <div @mouseover="hoverOver" @mouseout="hoverOut">
                  <button :class="classes">IMMEDIATE</button>
              </div>
              <br><br><br>
              <div @mouseover="hoverOver" @mouseout="hoverOutTimeout">
                  <button :class="classes">WAIT JUST A BIT
              </button>
              </div>
          </div>
      

      https://jsfiddle.net/jmbldwn/kbswLpto/5/

      【讨论】:

        【解决方案4】:

        在您的Dashboard.vue 文件中,尝试更改以下内容:

        data() {
            return {
                showAudience: false,
                message: 'Hover Me',
            }
        },
        

        进入:

        data: {
            showAudience: false,
            message: 'Hover Me',
        },
        

        (也许一个对象更适合你)

        【讨论】:

          【解决方案5】:

          答案是我应该放

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

          在 resources/views/layouts/app.blade.php 中,而不是将其放在我的其他视图文件中,例如我手动创建的那些。对于那些想知道我应该将代码放在 app.blade.php 哪里的人。把它放在 body 标签的末尾

          例如在 app.blade.php 中

          <body>
           // some code here
           // some code there
          
          
          // put the script code here
          <script src="{{ asset('js/app.js') }}"></script>
          </body>
          

          【讨论】:

            猜你喜欢
            • 2019-03-08
            • 2018-08-13
            • 1970-01-01
            • 2011-08-04
            • 2018-09-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-18
            相关资源
            最近更新 更多