javascript - v-on:click not working bootstrap - Stack Overflow

Initially I thought this was an issue with how I was using the @click directive according to this quest

Initially I thought this was an issue with how I was using the @click directive according to this question. I added the .native to the directive and my method is still not getting invoked.

I know this is bootstrap as if I use a normal <button> then the method is invoked as expected.

There are no errors in the logs so it is just as if the element is not registering the directive?

UpingBirthdays.vue

<template>
    <div>
        <h1>{{ section_title }}</h1>
        <b-card style="max-width: 20rem;"
                v-for="birthday in birthdays.birthdays"
                :key="birthday.name"
                :title="birthday.name"
                :sub-title="birthday.birthday">
            <b-button href="#"
                    @click.native="toWatch(birthday, $event)"
                    variant="primary">Watch
            </b-button>
        </b-card>
    </div>
</template>

<script>
    import { mapState } from "vuex";

    export default {
        name: "UpingBirthdays",
        data: () => {
            return {
                section_title: "Uping Birthdays",
            };
        },
        methods: {
            toWatch: (birthday, event) => {
                event.preventDefault();
                console.log("watched called");
                console.log(birthday.name);
                console.log(`BEFORE: ${birthday.watch}`);
                birthday.watch = !birthday.watch;
                console.log(`AFTER: ${birthday.watch}`);
            }
        },
        puted: mapState([
            "birthdays",
        ]),
    };
</script>

<style>
</style>

EDIT

Worth mentioning that when using HTML5 <button>, I do not have to append the .native property to the @click directive.

EDIT 2

Here is my codesandbox I created to replicate this error. I would expect an error here to say BirthdaysApi is not defined but I am not getting anything when the button is clicked.

Initially I thought this was an issue with how I was using the @click directive according to this question. I added the .native to the directive and my method is still not getting invoked.

I know this is bootstrap as if I use a normal <button> then the method is invoked as expected.

There are no errors in the logs so it is just as if the element is not registering the directive?

UpingBirthdays.vue

<template>
    <div>
        <h1>{{ section_title }}</h1>
        <b-card style="max-width: 20rem;"
                v-for="birthday in birthdays.birthdays"
                :key="birthday.name"
                :title="birthday.name"
                :sub-title="birthday.birthday">
            <b-button href="#"
                    @click.native="toWatch(birthday, $event)"
                    variant="primary">Watch
            </b-button>
        </b-card>
    </div>
</template>

<script>
    import { mapState } from "vuex";

    export default {
        name: "UpingBirthdays",
        data: () => {
            return {
                section_title: "Uping Birthdays",
            };
        },
        methods: {
            toWatch: (birthday, event) => {
                event.preventDefault();
                console.log("watched called");
                console.log(birthday.name);
                console.log(`BEFORE: ${birthday.watch}`);
                birthday.watch = !birthday.watch;
                console.log(`AFTER: ${birthday.watch}`);
            }
        },
        puted: mapState([
            "birthdays",
        ]),
    };
</script>

<style>
</style>

EDIT

Worth mentioning that when using HTML5 <button>, I do not have to append the .native property to the @click directive.

EDIT 2

Here is my codesandbox I created to replicate this error. I would expect an error here to say BirthdaysApi is not defined but I am not getting anything when the button is clicked.

Share Improve this question edited May 8, 2018 at 22:41 wmash asked May 8, 2018 at 21:56 wmashwmash 4,2024 gold badges38 silver badges74 bronze badges 5
  • You probably have to pass $event as the first argument: vuejs/v2/guide/events.html#Methods-in-Inline-Handlers. If that does not work, can you create an MCVE to show us a working example? If you need to use your code as-is, codesandbox.io might be the place to create a proof-of-concept example. – Terry Commented May 8, 2018 at 22:26
  • @Terry that is not the problem as I have attempted that way before. I have made a codesandbox and will edit my question to include – wmash Commented May 8, 2018 at 22:39
  • .native only matters for form submissions. It will do nothing for a normal button click outside of a form. – Korgrue Commented May 8, 2018 at 22:46
  • 1 [Vue warn]: The puted property "birthdays" is already defined in data. found in ---> <UpingBirthdays> at /src/ponents/UpingBirthdays.vue <App> at /src/App.vue <Root> – connexo Commented May 8, 2018 at 22:47
  • 1 github./bootstrap-vue/bootstrap-vue/issues/1146 – Phil Commented May 8, 2018 at 23:06
Add a ment  | 

1 Answer 1

Reset to default 3

Just remove the href="#" from your buttons (this makes the Bootstrap b-button ponent render your buttons as anchors) and it's working as expected:

https://codesandbox.io/s/w0yj3vwll7

Edit:

Apparently this is intentional behaviour from the authors, a decision I disagree upon. What they are doing is apparently executing event.stopImmediatePropagation() so any additional listener isn't triggered.

https://github./bootstrap-vue/bootstrap-vue/issues/1146

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745343089a4623418.html

相关推荐

  • javascript - v-on:click not working bootstrap - Stack Overflow

    Initially I thought this was an issue with how I was using the @click directive according to this quest

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信