поведение vuejs onclick на компоненте шаблона - PullRequest
0 голосов
/ 07 декабря 2018

У меня возникла проблема при разработке события onclick для шаблона vue.Итак, я могу открыть модуль, когда я нажимаю на файл, я видел пример с родным.к сожалению, все еще не вписывается в мой код и не может добраться до консоли.Вот мой код:

Я пытаюсь добавить нативный, но по-прежнему нет ответа в журнале консоли.

html

<div id="app">
<notification :notice="notice" ></notification>
</div>

js

const myTest = [ 
  {
    title: 'mamam',
    body: 'Mamamia',
    type: 'success'
  },{
    title: 'Perlu',
    body: 'Hati-hati sedang ada perbaikan',
    type: 'Danger'
  },{
    title: 'Notification',
    body: 'Repellendus quod aliquid ad, quae harum similique facilis aliquam. Dolorum deserunt alias officia atque dolor, voluptas harum. Inventore reiciendis reprehenderit provident!',
    type: 'primary'
  } 
];

function randomInt(min, max) {
  let rand = min - 0.5 + Math.random() * (max - min + 1);
  rand = Math.round(rand);
  return rand;
};

Vue.component('notification', {
  template: 
  `
  <div class="Notification" v-show="notice">
    <div class="notice" :class="notice.type">
      <svg @click="notice=!notice" class="close" width="20" height="20" viewbox="0 0 20 20" stroke-width="2">
        <line x1="3" y1="3" x2="17" y2="17"></line>
        <line x1="3" y1="17" x2="17" y2="3"></line>
      </svg>
      <div class="title">
        <i class="type" :class="notice.type">{{notice.type}}</i>
        {{notice.title}}
      </div>
      <div class="body">
        <i class="type" :class="notice.type">{{notice.body}}</i>
        <a @click.native="showFile"> File </a>
      </div>
    </div>
  </div>
  `,
  props: {
     notice: {
      type: [Object, Array]
    },
  },
});

setInterval(() => ( vm.$data.index = randomInt(0, 6) ), randomInt(100, 10000) );

const vm = new Vue({
  el: "#app",
  data: {
    index: 0
  },
  computed: {
    notice() {return myTest[this.index]}
  },
  methods: {
        showFile(e) {
          console.log(' show file was clicked. ')
      }
}
});

1 Ответ

0 голосов
/ 07 декабря 2018

Есть некоторые проблемы с вашим кодом:

  1. showFile должно быть определено в компоненте уведомления.Он не может достичь родительских showFile методов

Vue.component('notification', {
      template: 
      `
      <div class="Notification" v-show="notice">
        <div class="notice" :class="notice.type">
          <svg @click="notice=!notice" class="close" width="20" height="20" viewbox="0 0 20 20" stroke-width="2">
            <line x1="3" y1="3" x2="17" y2="17"></line>
            <line x1="3" y1="17" x2="17" y2="3"></line>
          </svg>
          <div class="title">
            <i class="type" :class="notice.type">{{notice.type}}</i>
            {{notice.title}}
          </div>
          <div class="body">
            <i class="type" :class="notice.type">{{notice.body}}</i>
            <a @click.native="showFile"> File </a>
          </div>
        </div>
      </div>
      `,
      props: {
         notice: {
          type: [Object, Array]
        },
      },
      methods: {
        showFile(e) {
          console.log(' show file was clicked. ')
        }
      }
    });
Вы не должны мутировать notice реквизиты в дочернем компоненте ( Дополнительная информация )
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...