Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue @click.native not working?

I have navigation component like this:

<template>
<nav class="navbar navbar-default navbar-fixed-top">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
        <a class="navbar-brand" href="">Website Builder</a>
    </div>
    <div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
                <li>
                    <a href="#" @click.native="currentView='create'">Create</a>
                </li>
                <li>
                    <a href="#" @click.native="currentView='how'">How</a>
                </li>
                <li>
                    <a href="#" @click.native="currentView='about'">About</a>
                </li>
                <li>
                    <a href="#" @click.native="currentView='youtube'">Videos</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
</template>

and my js:

Vue.component('navigation', Navigation)
Vue.component('create', Create)
Vue.component('how', How)
Vue.component('about', About)
Vue.component('youtube', Youtube)

new Vue({
    el: '#app',
    data: {
        currentView: 'create'
    }

and html:

<div id="app">
    <navigation></navigation>
    <keep-alive>
        <transition name="slide-fade" mode="out-in">
            <component :is="currentView"></component>
        </transition>
    </keep-alive>
</div>

However when I click on navigation, it doesn't change therefore I am assuming it's not working properly. if I put navigation away from component and stick it in div id=app it works fine, why is this happening?

like image 336
Przemyslaw Wojtas Avatar asked Nov 03 '17 11:11

Przemyslaw Wojtas


People also ask

Is it possible to use native event handlers in Vue?

Yes I did check the manual and these event handlers are working only for native dom elements like div, they don't work on custom components you create, like <UploadComponent />, for them you have to use event.native, but in my case, event.native.stop or event.native.prevent just do not work on latest vue. – DmitrySemenov Mar 20 '18 at 17:43

How to disable compat mode in Vue Dev Tools?

Click the "Click me!" text and observe that nothing is logged to your Dev Tools Console. Quit the dev server and comment out the config.resolve.alias line in vue.config.js to disable the compat mode. Run the dev server again. Click the "Click me!" text and observe that this time the "Component was clicked!"

Is it possible to use modifiers in Vue?

In the Vue, modifiers can be chained. So, you are free to use modifiers like this: @click.native.preventor @click.stop.prevent <my-component @click.native.prevent="doSomething"></my-component> Check events Share Improve this answer Follow answered Jul 10 '19 at 1:57 Akshat BajajAkshat Bajaj 8311 silver badge66 bronze badges

Can I listen to a native event on a specific element?

There are times that you want to listen directly to a native event on the root element of a component. In such cases, the .native modifier for v-on comes to rescue. However, using it on a very specific element is not a good idea. Read more about the native events here.


1 Answers

  1. The .native modifier is not necessary on native elements - it can only be used on component elements. Remove it.

  2. It seems you want to change data in the root component. Your current code changes data in the navigation component.

Two ways to do this:

1) The clean way:

In the navigation component, we emit an event with the new value to the parent:

<a href="#" @click="$emit('current-view', 'youtube')">

in the parent (here, the root) component, we receive the event and set the value:

<navigation @current-view="currentView = $event"></navigation>

2) The hacky way:

<a href="#" @click="$root.currentView='youtube'">
like image 106
Linus Borg Avatar answered Oct 23 '22 06:10

Linus Borg