Why can't i reach data after Vue-Router params? - vuejs2

When i search jobs and go to the details (to another page) i can't reach any results.
i think i need vuex-store but i dont understand that property.
this is jobs detail page (and value in json's)
export default {
data() {
return {
positionName: this.$route.params.positionName,
id: this.$route.params.jobId,
items: []
};
},
created() {
this.$http
.get(
"http://www.json-generator.com/api/json/get/cqdiacEgfC?indent=2" +
this.positionName +
this.id
)
.then(function(data) {
this.items = data.body;
});
}
};
<div>
<div class="card-body"
v-for="item in items"
:key="item.id">
<h5 class="card-title">{{positionName}}</h5>
<p class="card-text">{{item.companyName}}</p>
<p class="card-text">{{item.description}}</p>
<p class="card-text">{{item.countryName}}</p>
<p class="card-text">{{item.cityName}}</p>
<p class="card-text">{{item.address}}</p>
<p class="card-text">0{{item.contactPhone.areaCode}}-{{item.contactPhone.number}}</p>
<input type="button"
value="Başvur!"
class="btn btn-danger">
</div>
</div>
this is router page
import Vue from "vue";
import Router from "vue-router";
import Hey from "./components/hey.vue"
import Search from "./components/search.vue"
Vue.use(Router);
export default new Router({
mode: "history",
base: process.env.BASE_URL,
routes: [
{
path: "/",
name: "Home",
component: Search
},
{
path: "/:positionName/:jobId",
name: "JobDetail",
component: Hey
}
]
});
What can i do know? Why can't i reach json value in another page after router?

Related

How can I have single Vue component and how each contents in that can be routed by buttons of other component

I am new to Vuejs, I am looking to make my code effective just by having one vue component, and i want to specify the routing only once.
Currently i have one info.vue in a apps directive and prises.vue & priseshigh.vue in more directive. I want to have just one component in more directive. But the problem is in info.vue i have used two buttons, each button routes to prises.vue & priseshigh.vue respectively. Just like below code:
<vs-button class="btn" #click="$router.push({name: 'prises'}).catch(err => {})" >Go To</vs-button>
<vs-button class="btn" #click="$router.push({name: 'priseshigh'}).catch(err => {})" >Go There</vs-button>
My first question: So now i want to know, if i make one component as prisescomplete.vue by combining prises.vue & priseshigh.vue, how do i specify the routing to the buttons respectively in info.vue And what should i use in the prisescomplete.vue component to route the prises.vue & priseshigh.vue contents respectively .
My second question: below is my routing.js, so now what changes should i make in routing if i just have one component in views directive, and also with respect to first question.
{
path: '/apps/info',
name: 'info',
component: () => import('./views/apps/info/Info.vue'),
meta: {
rule: 'editor',
no_scroll: true
}
},
{
path: '/apps/info/info-more/prises-card',
name: 'prises',
component: () => import('./views/apps/info/more/prises.vue'),
meta: {
pageTitle: 'info-more',
rule: 'editor',
no_scroll: true
}
},
{
path: '/apps/info/info-more/priseshigh-card',
name: 'priseshigh',
component: () => import('./views/apps/info/more/priseshigh.vue'),
meta: {
pageTitle: 'info-more',
rule: 'editor',
no_scroll: true
}
},
Please send me the modified code, so that i can understand it easily.
You could pass props to route components.
https://router.vuejs.org/guide/essentials/passing-props.html
{
path: '/apps/info/info-more/prises-card',
name: 'prises',
component: () => import('./views/apps/info/more/prisescomplete.vue'),
props: {
prisesType: "prises"
},
meta: {
rule: 'editor'
}
},
{
path: '/apps/info/info-more/priseshigh-card',
name: 'priseshigh',
component: () => import('./views/apps/info/more/prisescomplete.vue'),
props: {
prisesType: "priseshigh"
},
meta: {
rule: 'editor'
}
}
PrisesComplete.vue
<template>
<div>
<span v-if="prisesType === 'prises'"> Prises </span>
<span v-else-if="prisesType === 'priseshigh'"> Prises High </span>
</div>
</template>
<script>
export default {
name: "PrisesComplete",
props: {
prisesType: {
type: String,
required: true
}
}
}
</script>
Also, you could use to="/path"
https://router.vuejs.org/guide/essentials/named-routes.html
<vs-button class="btn" :to="{ name: 'prises' }"> Go To </vs-button>
<vs-button class="btn" :to="{ name: 'priseshigh' }"> Go There </vs-button>
First of all you need to write a navigation.vue component for the navigation and render inside app with routerview. Look the codesandbox and the describtion
TheNavigation.vue
<template>
<div>
<vs-button
class="btn"
#click="$router.push({ name: 'prises' }).catch((err) => {})"
>Prises</vs-button
>
<vs-button
class="btn"
#click="$router.push({ name: 'priseshigh' }).catch((err) => {})"
>Priseshigh</vs-button
>
</div>
</template>
then render the navigation bar with the router view for loading the router.Here is the
App.vue where you render the navigation and routerview.
<template>
<div id="app">
<TheNavigation/>
<hr>
<RouterView/>
</div>
</template>
<script>
import TheNavigation from "./components/TheNavigation";
export default {
name: "App",
components: {
TheNavigation
}
};
</script>
RouterView is reponsible for loading the components which are defined inside router.js
Here is the Router.js
import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);
const router = new Router({
mode: "history",
routes: [
{
path: "/prises-card",
name: "prises",
component: () => import("./components/Prises.vue"),
meta: {
pageTitle: "info-more",
rule: "editor",
no_scroll: true
}
},
{
path: "/priseshigh-card",
name: "priseshigh",
component: () => import("./components/PrisesHigh.vue"),
meta: {
pageTitle: "info-more",
rule: "editor",
no_scroll: true
}
}
]
});
export default router;

Clicking routes in Header refreshes page in vuejs with prerender-spa plugin after serving dist

The connection in between components gets disrupted. Which shouldnt happen since I am using bootstrap-vue inbuilt router link (using to=" " instead of href="").
The app works perfectly fine when running without dist.
App.vue
<template>
<div class="container">
<app-header></app-header>
<div class="row">
<div class="col-xs-12">
<transition name="slide" mode="out-in">
<router-view></router-view>
</transition>
</div>
</div>
</div>
</template>
<script>
import Header from "./components/Header.vue";
export default {
components: {
appHeader: Header
},
created() {
this.$store.dispatch("initStocks");
}
};
</script>
Header.vue
<template>
<div>
<b-navbar toggleable="lg" type="dark" variant="info">
<b-navbar-brand to="/">Stock Broker</b-navbar-brand>
<b-navbar-toggle target="nav-collapse"></b-navbar-toggle>
<b-collapse id="nav-collapse" is-nav>
<b-navbar-nav>
<b-nav-item to="/portfolio">Portfolio</b-nav-item>
<b-nav-item to="/stocks">Stocks</b-nav-item>
<b-nav-item #click="endDay">End Day</b-nav-item>
<b-navbar-nav right>
<b-nav-item right>Funds: {{ funds }}</b-nav-item>
</b-navbar-nav>
</b-navbar-nav>
</b-collapse>
</b-navbar>
</div>
</template>
<script>
import { mapActions } from "vuex";
export default {
data() {
return {
isDropdownOpen: false
};
},
computed: {
funds() {
return this.$store.getters.funds;
}
},
methods: {
...mapActions({
randomizeStocks: "randomizeStocks",
fetchData: "loadData"
}),
endDay() {
this.randomizeStocks();
},
saveData() {
const data = {
funds: this.$store.getters.funds,
stockPortfolio: this.$store.getters.stockPortfolio,
stocks: this.$store.getters.stocks
};
this.$http.put("data.json", data);
},
loadData() {
this.fetchData();
}
}
};
</script>
vue.config.js
module.exports = {
pluginOptions: {
prerenderSpa: {
registry: undefined,
renderRoutes: ["/", "/portfolio", "/stocks"],
useRenderEvent: true,
headless: true,
onlyProduction: true
}
}
};
router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/stocks',
name: 'Stocks',
component: () => import(/ '../views/Stocks.vue')
},
{
path: '/portfolio',
name: 'Portfolio',
component: () => import('../views/Portfolio.vue')
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
I figured it a minute after making this post haha.
The issue was that in App.vue I had my main div with a class of "container" instead of id of "app".
Corrected below:
<template>
<div id="app"> //correction here
<app-header></app-header>
<div class="row">
<div class="col-xs-12">
<transition name="slide" mode="out-in">
<router-view></router-view>
</transition>
</div>
</div>
</div>
</template>

Vue router dynamic link and children reload page - not load correctly component

I add to my routes file path with children:
path: '/warehouse/:id',
name: 'ShowWarehouse',
component: ShowWarehouse,
children: [{
path: 'edit',
name: 'EditWarehouse',
component: EditWarehouse
}
]
Now in component ShowWarehouse I have:
<div v-if="!changeEdit">
<div v-if="warehouseData">
<div>Name: {{ warehouseData.warehouse.name }}</div>
<div>
<router-link
:to="{ name: 'EditWarehouse', params: {id: warehouseData.warehouse.id }}"
>Edit</router-link>
</div>
</div>
</div>
<router-view v-else></router-view>
When the user click edit button I need load component EditWarehouse, but component ShowWarehouse must be disappear, and if user back (without /edit) disappear componet EditWarehouse and load component ShowWarehouse. I write method in watch:
watch: {
$route() {
if (this.$route.path == '/warehouse/' + id_get_from_API + '/edit') {
this.changeEdit = true;
} else {
this.changeEdit = false;
}
}
},
The problem is when the user is at mydomain.com/warehouse/23/edit and click reload page (F5), then Vue loads component ShowWarehouse instead of loading EditWarehouse.
I using mode: 'history'.
Problem:
From the Vue.JS website: "Vue does provide a more generic way to observe and react to data changes on a Vue instance: watch properties." When you refresh the page the watch() method will not be executed because it is a new Vue instance and no data has changed on the Vue instance yet. You should probably use a different pattern to determine which component to show. (https://v2.vuejs.org/v2/guide/computed.html#Computed-vs-Watched-Property)
Solution:
I suggest making the EditWarehouse a sibling route to ShowWarehouse, and make EditWarehouse its own component (you already have this). Your router-link in the ShowWarehouse component can stay the same.
Code Snippet:
const ShowWarehouse = {
template: `<div><h1>ShowWarehouse</h1> <div v-if="warehouseData">
<div>Name: {{ warehouseData.warehouse.name }}</div>
<div>ID: {{ $route.params.id }}</div>
<div>
<router-link :to="{ name: 'EditWarehouse'}">Edit</router-link>
</div>
</div></div>`,
computed: {
warehouseData: function() {
let data;
let id = this.$route.params.id;
if (id) {
data = {
warehouse: {
name: 'Some Warehouse Name',
id: id
}
}
}
return data;
}
}
};
const EditWarehouse = {
template: "<h1>EditWarehouse [{{ $route.params.id }}]</h1>"
}
const router = new VueRouter({
routes: [{
path: '/warehouse/:id',
name: 'ShowWarehouse',
component: ShowWarehouse
},
{
path: '/warehouse/:id/edit',
name: 'EditWarehouse',
component: EditWarehouse
}
]
});
new Vue({
el: '#app',
router
});
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<p>
<router-link :to="{ name: 'ShowWarehouse', params: { id: 123 }}">Go to Warehouse 123</router-link>
</p>
<router-view/>
</div>
Here is a jsfiddle with the same code:
https://jsfiddle.net/austinwasinger/oruswb3a/39/

how to programmatically return to Vue cli's pre-made Home.vue

I'm using Vue CLI 3 and it makes a few routes. One is Home.vue. In my program I am trying to programmaticaly go to different pages. I added the routes I need in router.js but kept the already created routes for Home.vue and About.vue. It works fine until I get to 'Home' and get a warning: [vue-router] Route with name 'Home' does not exist.'
Here is the code:
<template>
<div class='secondItem'>
<h4 v-for="item in menuItems"
#click="bindMe(item)" v-bind:class="{'active':(item === current)}">{{item}}</h4>
</div>
</template>
<script>
export default {
name: 'Header',
data() {
return {
current: '',
menuItems: ['Home', 'About', 'Portfolio', 'Contact'],
}
},
methods: {
bindMe(item) {
this.current = item;
this.$router.push({
path: item
})
}
}
}
<script>
Are you using named routes? In that case you need to use name instead of path:
this.$router.push({
name: item
})
Also, your example can be simplified quite a lot. Try this:
<template>
<div class="secondItem">
<router-link :to="{ name: item }" tag="h4" active-class="active" v-for="item in menuItems" v-bind:key="item">{{item}}</router-link>
</div>
</template>
<script>
export default {
name: 'Header',
data() {
return {
menuItems: ['Home', 'About', 'Portfolio', 'Contact']
}
}
}
<script>

Nested-routes is not working in vue-router 2

Here is the codes.
html
<div id="app">
<nav>
<ul>
<router-link to="/user">User List</router-link>
</ul>
</nav>
<router-view></router-view>
</div>
Routes definition
const routes = [
{
path: '/user', component: User,
children: [
{path: '', name:'user-list', component: UserList},
{path: ':id', name: 'user-detail', component: UserDetail}
]
}];
let router = new VueRouter({routes});
export default router;
User
<template>
<div>
<h2>User Center</h2>
<router-view></router-view>
</div>
</template>
<script>
export default {
data () {
return {
}
},
methods:{
}
}
</script>
User list
<template>
<div>
<!--<ul>-->
<!--<li v-for="u of userList">-->
<!--<router-link :to="{ name:'user-detail', params: { id: '4343' }}">{{u.name}}</router-link>-->
<!--</li>-->
<!--</ul>-->
<router-link :to="{ name:'user-detail', params: { id: '4343' }}">3434</router-link>
<!--<a #click="query">++++</a>-->
</div>
</template>
<script>
import {mapActions} from 'vuex'
import {USER_QUERY} from '../store/user.type'
export default {
data () {
return {
userList: []
}
},
methods: {
query: function () {
this.$store.dispatch(USER_QUERY, [{name:'111'},{name:'1222'}])
}
}
}
</script>
User detail
<template>
<div>
<h2>{{ $route.params.id }}</h2>
</div>
</template>
<script>
export default {
data () {
return {
}
},
methods: {
test: ()=> {
}
}
}
</script>
I want click the link in the UserList can navitage to the UserDetail,but it is not working,someone help to check it?
Like this
App
--User
----UserList
http://localhost:8080/#/user
change to
App
--User
----UserDetail
http://localhost:8080/#/user/5454
you have to give path in to in outer-link as /user/:id in your HTML code, like following:
<router-link :to="'/users/' + id">3434</router-link>
See working fiddle here.
I made a stupid mistake like this.
import User from './component/user-list.vue';