4

Trying to achieve inserting some computed methods onto an element depending on mobile viewports only. Here's a basic gist of what I'm working with:

<a class="nav-link float-left p-x-y-16" v-bind:class={active:isCurrentTopicId(t.id)} @click="onTopicClicked($event, m, t)" href="#">{{t.title}}</a>

<script>

export default {
    data() {
        return {
            isClosed: false
        }
    },
    computed: {
        toggleMenu() {
            return {
                isClosed: this.isClosed
            }
        }
    },
    watch: {
        browserWidth(prevWidth, newWidth) {
            console.log('width changed from ' + newWidth + ' to ' + prevWidth);
    },
    mounted() {
        var that = this;
        this.$nextTick(function() {
            window.addEventListener('resize', function(e) {
                that.browserWidth = window.innerWidth;
                if(that.browserWidth > 824) {
                    console.log('Desktop View');
                } else {
                    console.log('Mobile View');
                }
            })
        })
    }
}
</script>

I would like to try to use the resize event to determine browser width so that I can dynamically insert the computed function onto that <a> tag

4
  • What's your goal, but purely functional perspective? Commented Jun 4, 2018 at 20:24
  • I just want it to work lol. It seems like a simple enough thing to do where it shouldn't affect the entire project drastically performance wise. All it's really doing is toggling some CSS. It works when I have @click="onTopicClicked($event, m, t); isClosed = !isClosed". I'd like to achieve the same thing but instead of already declaring the event there - I'd like to dynamically insert it on mobile views. Commented Jun 4, 2018 at 20:26
  • declare one data property like customEvent: function () {} and in the template, <a @click="customEvent()" />, then if some event triggered, assign customEvent=function (data, m, t) {/*do something*/} Commented Jun 4, 2018 at 21:02
  • What's the reason for the downvote lol. Commented Jun 4, 2018 at 21:49

2 Answers 2

3

You could either provide two different elements (one for desktop and another for mobile) as stated by Karthikeyan, or conditionally add click event to that element:

v-on="isMobileView ? { mouseover: onTopicClicked($event, m, t) } : {}"
Sign up to request clarification or add additional context in comments.

3 Comments

I like this one @Nimaj suggested than my solution, as here the html code is not duplicated
I added these two methods for the sake of testing what is firing enabled() {console.log('enabled')}, disabled() {console.log('disabled')} and on the element it looks like v-on:click="isDesktopView ? {click: disabled()} : {click: enabled()}" but it seems to only log "disabled" whether I'm desktop or mobile.
I use Vue3, If event handler with brackets, like this, <canvas id="s-canvas" :width="contentWidth" :height="contentHeight" v-on="refreshByClick?{click:refreshCode()}:{}"></canvas>. It seems doesn't work and component doesn't render, but if without brackets, It works fine. I don't know why this happen.
1

You can add a data that says if the view is mobile or not and use v-if , v-else and have the @click only added to the v-if="isMobileView"

<a v-if="isMobileView" class="nav-link float-left p-x-y-16" v-bind:class={active:isCurrentTopicId(t.id)} @click="onTopicClicked($event, m, t)" href="#">{{t.title}}</a>

<a v-else class="nav-link float-left p-x-y-16" v-bind:class={active:isCurrentTopicId(t.id)} href="#">{{t.title}}</a>

<script>

export default {
    data() {
        return {
            isClosed: false,
            isMobileView: false
        }
    },
    computed: {
        toggleMenu() {
            return {
                isClosed: this.isClosed
            }
        }
    },
    watch: {
        browserWidth(prevWidth, newWidth) {
            console.log('width changed from ' + newWidth + ' to ' + prevWidth);
    },
    mounted() {
        var that = this;
        function checkIfMobileView() {
            that.isMobileView = window.innerWidth <= 824;
        }
        this.$nextTick(function() {
            window.addEventListener('resize', checkIfMobileView);
        });
        checkIfMobileView();
    }
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.