How update component after click and url change? - javascript

I try use vue-router, but something went wrong.
On page rendered router-link, by click url changed. localhost/#/ -> localhost/#/contacts but component steal main page. Update only if page reload.
main.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import App from './index.vue';
import Error404 from './404.vue';
import Routes from './route';
Vue.use(VueRouter);
const router = new VueRouter({
mode: 'hash',
routes: Routes
});
window.onload = () => {
new Vue({
router: router,
el: '#app-container',
data: {
currentRoute: window.location.hash.split('#')[1]
},
computed: {
ViewComponent () {
return Routes.filter(item => item.path == this.currentRoute)[0] || Error404
}
},
render (h) { return h(this.ViewComponent.component ? this.ViewComponent.component : this.ViewComponent) }
});
}
route.js
const routes = [
{path: '/', component: mainPage},
{path: '/example1', component: examplePage},
{path: '/example2', component: exampleForm},
{path: '/contacts', component: contacts}
];
export default routes
index.vue
<template lang="jade">
div(id="app")
router-link(to="/example1") Example1
br
router-link(to="/example2") Example2
br
router-link(to="/contacts") Contacts
</template>
<script type="text/javascript">
export default {
}
</script>

I fix problem by add
watch: {
'$route'(to, from) {
this.$router.go(this.$router.currentRoute)
}
}
for main.js, but i think this is very bad solution.

you are missing router-view. this is the component responsible to render the component that the router needs to display
please consider this code structure based on official boilerplate:
main.js
import Vue from 'vue'
import App from './App'
import router from './router'
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
})
index.vue
<template>
<div id="app">
<router-view/>
<router-link to="example1">example1</router-link>
<router-link to="example2">example2</router-link>
</div>
</template>
index.js (like your route.js)
import Vue from 'vue'
import Router from 'vue-router'
import example1 from '#/components/example.vue'
import example2 from '#/components/example2.vue'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/example-1',
name: 'example-1',
component: example1
}
{
path: '/example-2',
name: 'example-2',
component: example2
}
]
})

Related

routers and components in vue.js

My component information is not displayed!!!
the router is working great and I test it to many times but there is nothing in components when I run the project.
They all seem to have nothing in them buy I put lot`s of things in them.
I working with vue.js 2
and I have an error in console "You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build."
app.vue:
<template>
<div>
<router-link to="/">app</router-link>
<router-link to="/RoutOne">RoutOne</router-link>
<router-link to="/RoutTwo">RoutTwo</router-link>
<router-link to="/RoutThree">RoutThree</router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
data() {
return {};
},
methods: {},
};
main.js:
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import RoutOne from "./router/RoutOne.vue";
import RoutTwo from "./router/RoutTwo.vue";
import RoutThree from "./router/RoutThree.vue";
Vue.use(VueRouter)
const routes = [
{ path: "/", component: App },
{ path: "/RoutOne", component: RoutOne },
{ path: "/RoutTwo", component: RoutTwo },
{ path: "/RoutThree", component: RoutThree },
]
const app = new Vue({
router: new VueRouter({
routes
}),
component: app
}).$mount('#app')
component one:
<template>
<div >
<h1>someone you loved</h1>
<title></title>
<button>nothing</button>
<input disabled placeholder="nothing"/>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
component two:
<template>
<div >
<h1>loving you is a losing game</h1>
<h2>the title is : {{title}}</h2>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
component three:
<template>
<div >
<h1>and way down we go</h1>
<b-alert>wrong side</b-alert>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
Just add mode: 'history' to your router, by default vue-router use hash mode, it uses a hash character (#) before the actual URL is internally passed.
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import RoutOne from "./router/RoutOne.vue";
import RoutTwo from "./router/RoutTwo.vue";
import RoutThree from "./router/RoutThree.vue";
Vue.use(VueRouter)
const routes = [
{ path: "/", component: App },
{ path: "/RoutOne", component: RoutOne },
{ path: "/RoutTwo", component: RoutTwo },
{ path: "/RoutThree", component: RoutThree },
]
const app = new Vue({
router: new VueRouter({
mode: "history",
routes,
}),
component: app
}).$mount('#app')

Vue-router does not show component on page

I have problem with my router. I want show bodyComponent on my index page, but nothing happen.
I use laravel+vue. Where I can make mistake? I not got mistake
My router:
import Vue from 'vue'
import VueRouter from 'vue-router'
import bodyComponent from '../views/bodyComponent'
Vue.use(VueRouter)
const router= new VueRouter({
mode: "history",
routes:[
{
path:"/",
components: {
default: bodyComponent,
}
}
]
})
export default router;
app.js
import router from "./router"
const app = new Vue({
el: '#app',
router
});
My index.blade.php
<div id="app">
<header-component></header-component>
<!--<body-component></body-component>-->
<router-view></router-view>
<footer-component></footer-component>
</div>
I change path in my vue route and components show on index page. On application index page I come from laravel router. So I change path to index page in vue router on name same in laravel route
My laravel router
Route::get('/tobyten', function () {
return view('tobyten.index');
})->name('tobyten');
My vue router
const router= new VueRouter({
mode: "history",
routes:[
{
path:"/tobyten",
components: {
default: bodyComponent,
}
}
]
})

routes content does not show in Vue-router and Laravel

Im following this YT tutorial, I followed the steps but it seems that the ExampleComponent does not show. The App.vue shows but not the route.
Here are my code:
app.js
import Vue from 'vue';
import router from './router';
import App from './components/App';
require('./bootstrap');
const app = new Vue({
el: '#app',
components: {
App
},
router,
});
router.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import ExampleComponent from './components/ExampleComponent';
Vue.use(VueRouter);
export default new VueRouter({
routes : [
{ path : '/', component : ExampleComponent }
],
mode: 'history'
});
App.vue
<template>
<div>
<h1> Hello!</h1>
<router-view></router-view>
</div>
</template>
<script>
export default {
name : "App"
}
</script>
<style scoped>
</style>
app.blade.php
<body>
<div id="app">
<App></App>
</div>
</body>
web.php
Auth::routes();
Route::get('/{any}', 'HomeController#index')->where('any', '.*');
Thanks in advance.
Change the mode from history to hash because the history mode in this case is reserved to Laravel routing system :
export default new VueRouter({
routes : [
{ path : '/', component : ExampleComponent }
],
mode: 'hash'
});
The mode: 'hash' is a great solution.
But if u want to use mode: "history" Then you have to add base Parameter . Base is useful when the application is hosted inside of a folder like http://localhost/laravel-8/. So code rewrite like following -
export default new VueRouter({
mode: "history",
base: '/laravel-8/',
fallback:true,
routes : [
{ path : '/', component : ExampleComponent }
]
})
Instead of /laravel-8/, you use your project subdirectory name.
Documentation: https://router.vuejs.org/guide/migration/#moved-the-base-option

vue-router : failed to resolve async component render

I am a beginner to vue-router and I can't make it work. When I start my app, I get the following errors :
[vue-router] Failed to resolve async component render: TypeError: _vm is undefined
49:16:39
[vue-router] uncaught error during route navigation:
49:16:39
TypeError: _vm is undefined
Stack trace:
render#webpack-internal:///63:3:7
resolveAsyncComponents/</<#webpack-internal:///49:1774:17
flatMapComponents/</<#webpack-internal:///49:1801:66
...
Here are my files : main.js
import Vue from 'vue'
import App from './App.vue'
import router from './routes.js'
import 'bootstrap'
new Vue({
el: '#app',
router,
render: h => h(App)
})
routes.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './Home.vue'
export const routes = [
{ name: 'Home', path: '', components: Home }
]
Vue.use(VueRouter)
const router = new VueRouter({
routes
})
export default router
There is a typo in routes, change components to component:
export const routes = [
{ name: 'Home', path: '', component: Home }
]

Vuejs lazy loading routes in webpack template

I have created vuejs project with vue-cli tools and webpack template.
vue init webpack my-project
I don't know how to implement lazy loading on routes with templates
currently i have two routes in router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '#/components/HelloWorld'
import Test from '#/components/Test'
Vue.use(Router)
export default new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},
{
path: '/test',
name: 'test',
component: Test
}
]
})
And its imported in main.js
import Vue from 'vue'
import App from './App'
import router from './router'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
But it doesn't contain the lazy loading implementation how can i do it??
instead of using
import Test from '#/components/Test'
use as
const Test = () => import('#/components/Test');
lazy loading Documentataion
Nice way to do this:
function load (component) {
return () => import(`#/${component}.vue`)
}
...
routes: [
{ path: '/', component: load('Hello') },

Categories

Resources