bbennett36
bbennett36

Reputation: 6355

Nuxt - Using router.push inside a component not changing pages correctly

index.vue -

<template>
<div>
    <Header />
    <div class="container">
        <SearchForm />
    </div>
</div>
</template>

<script>
const Cookie = process.client ? require('js-cookie') : undefined

export default {
    data() {
        return {
            form: {
                email: '',
                password: ''
            },
            show: true
        }
    },
    methods: {
        logout() {
            // Code will also be required to invalidate the JWT Cookie on external API
            Cookie.remove('auth')
            this.$store.commit('setAuth', {
                auth: null,
                user_type: null
            })
        }
    }
}
</script>

<style>
.container {
    /* margin: 0 auto; */
    /* min-height: 100vh; */
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
}

</style>  

jobs.vue -

<template>
<div>
    <Header />
    <SearchForm />
    <b-container class="main_container">
        <b-row>
        <h1> Results for "{{q}}"</h1>
        </b-row>
        <b-row>
        <ul id="array-rendering">
            <li v-for="item in results" :key="item.job_id">
                {{ item.job_title }}
                {{ item.job_city }}
                {{ item.job_state }}
                {{ item.job_work_remote }}
            </li>
        </ul>
        </b-row>
    </b-container>
</div>
</template>

<script>
const Cookie = process.client ? require('js-cookie') : undefined
export default {
    // middleware: 'notAuthenticated',
    watchQuery: ['q'],
    data() {
        return {
            q: null,
            results: []
        }
    },
    async fetch() {
        this.q = this.$route.query.q
        this.results = await this.$axios.$get('/api/job/search', {
            params: {
                keyword: this.q,
            }
        })
    },

    methods: {

    }
}
</script>

<style>
.container {
    align-items: center;
    text-align: center;
}
</style>

SearchForm.vue component -

<template>
<div id='searchFormDiv'>
    <b-form inline @submit.prevent="onSubmit">
        <label class="sr-only" for="inline-form-input-name"> keyword</label>
        <b-form-input v-model="form.keyword" id="inline-form-input-name" class="mb-2 mr-sm-2 mb-sm-0" placeholder="Job title or keyword" size="lg"></b-form-input>

        <label class="sr-only" for="inline-form-input-username">location</label>
        <b-input-group class="mb-2 mr-sm-2 mb-sm-0">
            <b-form-input v-model="form.location" id="inline-form-input-username" size="lg" placeholder="City, state or zip"></b-form-input>
        </b-input-group>

        <b-button type="submit" variant="primary">Find Jobs</b-button>
    </b-form>

</div>
</template>

<script>
import {
    BIconSearch,
    BIconGeoAlt
} from 'bootstrap-vue'

export default {
    data() {
        return {
            form: {
                keyword: '',
                location: ''
            }
        }
    }, 
    created () {
        this.form.keyword = this.$route.query.q
    },
    methods: {
        onSubmit() {
            this.$router.push({
                path: 'jobs',
                query: {
                    q: this.form.keyword
                }
            });
        }
    },
    components: {
        BIconSearch,
        BIconGeoAlt
    },
}
</script>

<style>
#searchFormDiv {
    margin-top: 50px
}
</style>

The route for "http://localhost:3000/" returns the index.vue page.

In this vue page, I have a component with a search form. Once you complete these form and hit the seach button, it will re-direct to a results page

if this.form.keyword = "Data", the next URL will be "http://localhost:3000/jobs?q=Data" and it will be using the jobs.vue page.

The issue I'm running into is the CSS is not being loaded from the jobs.vue page. It's still coming from the index.vue page for some reason. If I refresh the page, then the CSS from jobs.vue is loading. I need the CSS to load from jobs.vue on the initial redirect. All of the query data is working as expected so thats a plus.

However, the following CSS is being applied from index.vue for some reason instead of the CSS from the jobs.vue page -

display: flex;
justify-content: center;

Does anyone know whats going on here? This app is SSR and not SPA.

Upvotes: 0

Views: 1668

Answers (2)

bbennett36
bbennett36

Reputation: 6355

This solved the issue -

methods: {
    onSubmit() {
        window.location = 'http://localhost:3000/jobs?q=' + this.form.keyword;
    }
},

Upvotes: -1

Nicolas Pennec
Nicolas Pennec

Reputation: 7631

You have to scope your css from the index.vue page to the other pages with the scoped directive (see docs https://vue-loader.vuejs.org/guide/scoped-css.html)

<style scoped>
/* local styles */
</style>

<style>
/* global styles */
</style>

You can add your global CSS in your layouts/default.vue file.

Upvotes: 1

Related Questions