简体   繁体   English

Vue.js调用组件方法导致错误

[英]Vue.js call component method results in an error

The problem is the following, I basically have one Component which is 'card' and I the cards are rendered inside a Vue called 'dash' using v-for. 问题如下,我基本上有一个组件是'card',我使用v-for将卡片渲染到称为'dash'的Vue中。 Now what I try to do is add an on-click event to the card, and I have a method called expand declared inside my Component but I get an error when I try to do so. 现在,我想做的是向卡中添加一个单击事件,并且我在Component内声明了一个名为expand的方法,但尝试这样做时出现错误。 My HTML code looks like this 我的HTML代码如下所示

    <div id="dash" class="dash" style="margin-left: 350px; margin-top:50px;">
        <div v-for="state in states" class="state slide-in-fwd-center"  v-bind:style="{'margin-left' : state.margin + 'px'}">
            <h3 class="header">{{state.state}}</h3>
            <card v-for="card in state.cards" v-bind:overall_progress="card.overall_progress" v-bind:test_progress="card.test_progress" v-bind:status="card.status" v-bind:practice_progress="card.practice_progress" v-bind:due_date="card.due_date"  v-bind:study_progress="card.study_progress" v-bind:key="card.id" v-bind:description="card.description"
                v-bind:title="card.title"   @click="$emit('expand')"></card>
        </div>
    </div>

The outer div "dash" is a Vue, that has stated and each state holds an array of cards which are Vue Components. 外面的div“破折号”是Vue,上面已经声明,每个状态都包含一系列卡,这些卡是Vue组件。 The component looks like this 该组件看起来像这样

Vue.component("card", {
  props: [
    "title",
    "description",
    "due_date",
    "study_progress",
    "practice_progress",
    "test_progress",
    "overall_progress",
    "status"
  ],
  template: `TEMPLATE CODE HERE`,
  methods: {
    calcColor: function(value) {
        if(value > 89){
            return 'bg-success'
        }
        else if(value < 90 && value > 39){
            return 'bg-info'
        }
        else{
            return 'bg-danger'
        }
    },
    expand : function(){
      console.log('123')
    }
  }
});

While the dash is really simple: 虽然破折号非常简单:

var dash = new Vue({
  el: "#dash",
  data: {
    states: []
  }
});

I am not sure what might be the problem since the method I am trying to call is defined inside the methods:{} of the Component 'card' itself it is not in the Vue or global function 我不确定可能是什么问题,因为我要调用的方法是在组件“卡”本身的方法{}中定义的,它不在Vue或全局函数中

firstly it might be easier for you to bind just the whole card object and access it on card component. 首先,对于您来说,仅绑定整个卡对象并在卡组件上进行访问可能会更容易。 So instead of this: 所以代替这个:

<div id="dash" class="dash" style="margin-left: 350px; margin-top:50px;">
  <div
    v-for="state in states"
    class="state slide-in-fwd-center"
    v-bind:style="{'margin-left' : state.margin + 'px'}"
  >
    <h3 class="header">{{ state.state }}</h3>
    <card
      v-for="card in state.cards"
      v-bind:overall_progress="card.overall_progress"
      v-bind:test_progress="card.test_progress"
      v-bind:status="card.status"
      v-bind:practice_progress="card.practice_progress"
      v-bind:due_date="card.due_date"
      v-bind:study_progress="card.study_progress"
      v-bind:key="card.id"
      v-bind:description="card.description"
      v-bind:title="card.title"
      @click="$emit('expand')"
    ></card>
  </div>
</div>

you will have that: 您将拥有:

<div id="dash" class="dash" style="margin-left: 350px; margin-top:50px;">
        <div v-for="state in states" class="state slide-in-fwd-center"  v-bind:style="{'margin-left' : state.margin + 'px'}">
            <h3 class="header">{{state.state}}</h3>
            <card v-for="card in state.cards"
             v-bind:card = "card"
             @click="$emit('expand')"></card>
        </div>
    </div>

For the event, don't use the $emit, try just to call expand, like this: 对于事件,请勿使用$ emit,仅尝试调用expand,如下所示:

@click="event_name(argument1, argument2)"

so - for your case: 所以-对于您的情况:

@click="expand"

Be sure, that you define expand method in the component, you are using it. 确保在组件中定义了expand方法,并且正在使用它。 in this case - in the parent component, not in the card. 在这种情况下-在父组件中,而不在卡片中。

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

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