简体   繁体   English

从控制台调用时,Vue2组件方法中的错误仍然有效

[英]Error in Vue2 Component Method Yet Works When Called From the Console

I have a Bootstrap modal that shows itself on this Vue method: 我有一个Bootstrap模式,可以在Vue方法上显示自己:

methods: {
    showAddSongModal() {
        $('#addSongModal').modal('show');
    }
}

This works in a project where the method is of the root Vue instance. 这在方法是根Vue实例的项目中有效。 However, I am now trying to use this modal and method in a Vue component. 但是,我现在试图在Vue组件中使用此模式和方法。 The method is invoked by @click="showAddSongModal() but when it's put inside a component, I get this error on click: 该方法由@click="showAddSongModal()调用,但是当将其放入组件中时,单击时会出现此错误:

Uncaught TypeError: $(...).modal is not a function

Otherwise, the page loads without errors in Chrome Dev Tools. 否则,该页面将在Chrome Dev Tools中正确加载。 And what's more, typing $('#addSongModal').modal('show'); 而且,键入$('#addSongModal').modal('show'); at the console behaves like it should -- the modal works! 在控制台上的行为应为-模态工作!

Thus far, I checked: 到目前为止,我检查了:

  1. jQuery is called but once. jQuery被调用了一次。
  2. jQuery, Bootstrap, Vue, Axios, et al. jQuery,Bootstrap,Vue,Axios等。 are called before my Vue components. 在我的Vue组件之前被调用。

The fact that @click="showAddSongModal() works from the console should validate my build process, right? @click="showAddSongModal()从控制台工作的事实应该验证我的构建过程,对吗?

Why does this modal and method work from a root Vue instance but cause the Uncaught TypeError when placed inside a Vue component? 为什么此模态和方法可以在Vue根实例上工作,但是当放置在Vue组件中时会导致Uncaught TypeError

My project has the Vue components, each in a component file and included like this: 我的项目有Vue组件,每个组件都在一个组件文件中,并且包含如下所示:

<template>
    <div class="song-table">
        <song-modal-form></song-modal-form>
        <div class="table-header d-flex justify-content-between align-items-center mt-3">
            <form id="search" class="">
                Search <input name="query" v-model="filterKey"> 
            </form>
 ....
html continues

<script>
    import SongModalForm from './SongModalForm.vue';

    export default {

        components: {
            SongModalForm
        },
 ....
        methods: {
            showAddSongModal() {
                    console.log('HELP!');
                    $('#addSongModal').modal('show');
            },
  ....
</script>

This Pen shows the scenario, a parent component calling the child component with the modal. 该笔显示了场景,一个父组件使用模态调用子组件。

The pen works, but in my actual project $('#addSongModal').modal('show'); 笔有效,但是在我的实际项目中$('#addSongModal').modal('show'); is only working from the console and not from the component: 仅从控制台而不是组件运行: 在此处输入图片说明

I am using Laravel 5.4.14 framework in this project. 我在这个项目中使用Laravel 5.4.14框架。 Using webpack, I was mixing together .js files like this: 使用webpack,我将如下的.js文件混合在一起:

  • .js files required site-wide (JQuery, Bootstrap, Vue, et al) into one app.js 整个站点所需的.js文件(JQuery,Bootstrap,Vue等)整合到一个app.js

  • backend-specific .js files like my Vue instance and specialized scripts into backend.js 特定于后端的.js文件,例如我的Vue实例和专用脚本,这些文件都包含在backend.js

app.js had only the following one line to require bootstrap.js: app.js只有以下一行需要bootstrap.js:

require('./bootstrap');

bootstrap.js required (JQuery, Bootstrap, Vue, et al). bootstrap.js必需的(JQuery,Bootstrap,Vue等)。

backend.js looked something like this: backend.js看起来像这样:

import SpecialComponent from './components/SpecialComponent.vue';
import LoginModal from './components/LoginModal.vue';
import RegisterModal from './components/RegisterModal.vue';
import SongTable from './components/SongTable.vue';

Vue.config.ignoredElements = [
  'wave'
];

const app = new Vue( {
    el: '#app-backend',

    components: {
        SpecialComponent ,
        LoginModal,
        RegisterModal,
        SongTable
    },

    data: {
        currentPath: window.location.pathname
    },
});

Then for a backend page, the html called the files like this: 然后对于后端页面,html调用了如下文件:

<script src="js/app.js"></script>
<script src="js/backend.js"></script>

That DID NOT WORK. 那没用。

Once require('./bootstrap'); 一旦require('./bootstrap'); was added to backend.js and I dropped app.js , the Uncaught TypeError went away. 被添加到backend.js而我删除了app.js ,Uncaught TypeError消失了。

In components, you can only have one root element in your template. 在组件中,模板中只能有一个根元素。 What you need to do is wrap your button and modal in a single div (or some other element) and your component will work. 您需要做的是将按钮和模式包装在单个div(或其他元素)中,组件将正常工作。

Change your template to this in your failing codepen. 在失败的代码笔中将模板更改为此。

<div>
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-primary mt-3 ml-3" @click="showAddSongModal()">
    Launch demo modal
  </button>

  <!-- Modal -->
  <div class="modal fade" id="addSongModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body" v-text="message"></div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>
</div>

Working version . 工作版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM