Vue Js 2 / Vue-CLI 3 / When hosted shows empty blank page - vue.js

Vue Js app working fine in dev mode, but as I upload it on the server, it simply displays a blank page. All the resources are showing up in the developer console. I am using vue-router package.
VueJs 2 / Vue-CLI-3
Below is my App.vue
<template>
<div id="app">
<m-nav/>
<router-view></router-view>
</div>
</template>
<script>
import Navbar from './components/Navbar.vue'
export default {
name: 'app',
components: {
'm-nav':Navbar
}
}
</script>
<style>
</style>
This is my main.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import HomePage from './components/HomePage.vue'
import Brochure from './components/Brochure.vue'
import Register from './components/Register.vue'
Vue.config.productionTip = false
Vue.use(VueRouter);
const routes = [
{
path:'/',
component: HomePage
},
{
path:'/download',
component: Brochure
},
{
path:'/register',
component: Register
}
];
const router = new VueRouter({
routes,
mode: 'history'
});
new Vue({
router,
render: h => h(App),
}).$mount('#app');
after running npm run build, It shows a blank page, although I can see some stuff in the DOM <div id="app"></div>
http://prntscr.com/lvasvo
I am not getting, where I have made a mistake! The project is complete, but stuck on this part! :(

follow this tips:
use <%= BASE_URL %> for script and link tags which wrote on index.html
create vue.config.js in root of project and config your public path for production and development mode like:
module.exports = {
publicPath:
process.env.NODE_ENV === "production" ? "/" : "/"
}
};
just use relative path, to know more about it, read this link:
https://cli.vuejs.org/config/#publicpath
create config.vue.js

In my case the issue was in the Router.beforeEach function that I had in index.js file inside the router folder to add a route guard. I had to rewrite it after upgrading it from Vue 2 to Vue 3 to make it work.

Just encounter the same issue with vue-cli3 project.
And my main reason getting this issue is my app doesn't deploy on the root of the domain,instead on a sub-path.
There're several tricky configures:
vue.config.js (it's vue-cli3 exclusive): to set publicPath:/my-app/ ;
router.js (assuming your vue-router config file is this): to set base:/my-app/;
And beware of the history router mode:
this mode needs extra server side config, I'm using nginX, this is my config:
location /my-app {
alias /var/www/my-app;
try_files $uri /index.html last;
index index.html;
}
And beware of the different between alias and root.
this mode is not support relative path publicPath:./ in vue.config.js.

Related

VueJS router configuration not enabled

In my VueJS project, I have configured the routing in a file "router.js".
import VueRouter from 'vue-router'
import personDetails from './components/component.vue'
Vue.use(VueRouter)
export default new VueRouter({
mode : 'history',
base : '/',
routes : [
{
path : '/:group/:id',
name : 'personDetails',
component : personDetails ,
props : true
}
]
})
In the main.js file, I enable the routing configuration like this:
import App from './App.vue'
import router from './router'
new Vue({
router,
el : '#app',
components : { App }
}).$mount('#app')
This is my App.js file:
<template>
<div id="app">
<router-view v-bind="$props" />
</div>
</template>
<script>
export default {
name: 'App',
props : ['group', 'id'],
mounted() {
console.log('Mounted App')
}
}
</script>
I run the project by running "vue-cli-service build" and "npx http-server ./dist" commands.
I notice that when I call the configured router like this - "http://localhost:8080/1234/56", I get a 404 error. However when I call the default URL "http://localhost:8080" I get a blank page, since the App is loaded without any routing parameters. I can see the console.log() statements added to main.js and App.js, but not the "personDetails" component.
I added a default router configuration, and that seems to be working.
{
path : '/',
redirect:'/456/123'
}
This default routing loads the 'personDetails' component with the path parameters mapped to 456 and 123.
Please give some tips to debug this?
This is due to mode: 'history'. More details here - HTML5 History Mode
Since our app is a single page client side app, without a proper
server configuration, the users will get a 404 error if they access
http://oursite.com/user/id directly in their browser.
To fix the issue, all you need to do is add a simple catch-all
fallback route to your server. If the URL doesn't match any static
assets, it should serve the same index.html page that your app lives
in.
You can use the fallback proxy of http-server as a catch-all redirect: npx http-server ./dist -P http://localhost:8080?

How can I redirect using vue-router?

I have tried to access to others pages via url, but those pages never loads
http://localhost:8080/index
http://localhost:8080/about
This is my main.js file
import Vue from 'vue'
import App from './App.vue'
import vuetify from './plugins/vuetify';
import VueRouter from 'vue-router'
Vue.config.productionTip = false
Vue.use(VueRouter)
import Index from './views/Index';
const About = { template: '<p>about page</p>' }
const routes = [
{ path: '/index', name: 'index', component: Index },
{ path: '/about', name: 'about', component: About }
]
var router = new VueRouter({
routes: routes,
mode: 'history'
})
new Vue({
router: router,
vuetify,
render: h => h(App)
}).$mount('#app')
Does anyone can help me with vue-router? i am new in this framework
Does it work if you access them like this:
http://localhost:8080/#/about
If yes, your vue-router is working in the default hash-mode. To get rid of the hash and get "normal" URLs you'll need to set it to history mode.
Edit:
As I see you're already using history mode. Do you use Vue CLI for local development? This should normally work out of the box. If not, you need to setup some redirect rules on other web servers. Please see the examples here: Example Server Configurations
Edit 2:
Can you show your App component?
I tried to reproduce your problem in a sandbox, but it works: https://codesandbox.io/s/confident-voice-zyg07
The App component here looks like this, including the router-view:
<template>
<div id="app">
<router-view id="page"/>
</div>
</template>
<script>
export default {
name: "App",
components: {}
};
</script>

Vue.js VueRouter router-link changes URL but page does not

my main.js file has the following
import TurbolinksAdapter from 'vue-turbolinks';
import Vue from 'vue/dist/vue.esm';
import VueRouter from 'vue-router';
import EventIndex from '../src/views/EventsIndex.vue';
import EventsTable from '../src/components/EventsTable.vue'
import { routes } from '../src/router/routes';
Vue.use(TurbolinksAdapter);
Vue.use(VueRouter);
const router = new VueRouter({
routes,
mode: 'history'
});
document.addEventListener('turbolinks:load', () => {
var element = document.getElementById('vue_code');
if (element != null) {
new Vue({
el: '#vue_code',
Store,
router,
render: h => h(EventIndex)
});
My routes.js file has the following:
import Vue from "vue/dist/vue.esm";
import VueRouter from "vue-router";
import EventForm from "../components/EventForm";
Vue.use(VueRouter);
const routes = [
{
path: "/events/new",
name: "EventForm",
component: EventForm
}
];
The router-link snippet from my component file EventsTable.vue file has the following:
<template lang=haml>
...
%tbody
%tr{v-for: "event in events"}
%td
%router-link.fa.fa-pencil{to: "/events/new"}
%router-view
...
<template>
<script>
import EventForm from './EventForm';
components: {
EventForm
}
</script>
When I click on the link, i expect the page to change to the EventForm page. Instead, the url changes, but the browser window does not change. But once I reload the page, the browser jumps to the EventForm page.
Any help is appreciated. Thanks
The first line of the vue-turbolinks package states:
⚠️ If you're using vue-router or another Javascript routing library, you don't need to use Turbolinks or this adapter. Turbolinks is meant to level up the traditional request-render cycle by loading the new page in the background and this adapter makes it possible to use Vue components on pages rendered in this manner. If you've decided to use a single-page app, you already have everything you need. 🤘
You cannot use vue-router and Turbolinks together. Turbolinks handles all of your site navigation. Navigating using vue-router doesn't work because it is attempting to do a history push instead of an entire page load.
I suggest programmatically routing using the Turbolinks.visit() function instead of using vue-router. You'll need to create a new view for the EventForm page that loads your vue component.

vue-router not routing properly, no components are shown

I'm trying to use vue-router to show different components for different route. However it doesn't seem to be working.
I have a codepen of the compiled program here.
My main.js is just defining the router and starting the vue application. It's importing the components and setting the routes.
import scss from './stylesheets/app.styl';
import Vue from 'vue';
import VueRouter from 'vue-router';
import Resource from 'vue-resource';
import App from './components/app.vue';
import Home from './components/home.vue';
import Key from './components/key.vue';
import Show from './components/show.vue';
// Install plugins
Vue.use(VueRouter);
Vue.use(Resource);
// Set up a new router
var router = new VueRouter({
mode: 'history',
routes:[
{ path: '/home', name: 'Home', component: Home },
{ path: '/key', name: 'Key', component: Key },
{ path: '/show', name: 'Show', component: Show },
// catch all redirect
{ path: '*', redirect: '/home' }
]
});
// For every new route scroll to the top of the page
router.beforeEach(function () {
window.scrollTo(0, 0);
});
var app = new Vue({
router,
render: (h) => h(App)
}).$mount('#app');
My app.vue is really very simple, just a wrapper div and the router-view
<script>
export default {
name: "app"
}
</script>
<template lang="pug">
.app-container
router-view
</template>
The three other components that router should be showing are equally as simple, each looks basically the same. Just the name and the h1 content are different.
<script>
export default {
name: "home"
}
</script>
<template lang="pug">
h1 Home
</template>
Webpack build everything into an app.js without any errors. I have a super simple index.html file that I open in Chrome.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sagely Sign</title>
</head>
<body>
<div id="app"></div>
<script src="js/app.js"></script>
</body>
</html>
Looking at the console I see no errors. What I do notice is the URL stays the same, it looks like the router is not redirecting to /home.
file:///Users/username/Development/test-app/build/index.html
I would have expected it to change to the new route.
file:///Users/username/Development/test-app/build/index.html#/!/home
But even if I goto that route directly the home.vue component is not displayed.
The function you are using in the beforeEach method is a navigation guard. Navigation guards receive 3 parameters: to, from and next. From the Navigation Guards documentation:
Make sure to always call the next function, otherwise the hook will never be resolved.
Here, you just scroll at the top of the page but the hook is never resolved, therefore the router stops here, right after the scroll.
Write your function like this:
router.beforeEach(function (to, from, next) {
window.scrollTo(0, 0);
next();
});
And it should work.

Components not showing up in Vue DevTools

Has anyone experienced this problem? I'm using Vue Devtools but can't inspect any components on a count of none are showing up. No Root component or anything. Pretty much just a blank DevTools. I'm new to Vue so I'm sure I'm missing something obvious. I'm using the webpack cli template and haven't implemented any Vue Router stuff yet. Nothing comes up when searching for components either. I'm assuming it's something in these 3 files?
main.js
import Vue from 'vue'
import App from './App'
var db = firebase.database();
var vm = new Vue({
el: '#app',
created: function() {
// Import firebase data
var quizzesRef = db.ref('quizzes');
quizzesRef.on('value', function(snapshot) {
console.log(snapshot.val());
vm.quizzes = snapshot.val();
});
},
data: function() {
return {
authenticated: false,
quizzes: {},
resources: []
}
},
template: '<App/>',
components: { App }
})
App.vue
<template>
<div id="app">
<navbar></navbar>
<resource-info></resource-info>
</div>
</template>
<script>
import Navbar from './components/Navbar'
import ResourceInfo from './components/ResourceInfo'
export default {
name: 'app',
components: {
Navbar,
ResourceInfo
}
}
</script>
<style>
</style>
Index.html (Omitted header)
<body>
<div id="app" class="container-fluid"></div>
<!-- built files will be auto injected -->
<script>
// Initialize Firebase
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
</script>
</body>
I had the same issue and here's what fixed it for me:
Ensure Allow access to file URLs in the chrome extension settings (chrome://extensions) is enabled. Restart Chrome.
Open the Vue DevTools extension and click the Refresh button on the top right after opening a Vue component on the page.
I hope this helps someone.
I see that you are using vue-cli and I assume it is running in dev mode (npm run dev).
Obviously it will not work after you build the production app using npm run build and serve from the dist folder.
Assuming you have taken care of the above, did you install the Vue.js devtools recently in Chrome? If so, your browser might need a restart. I think I had to do it when I installed Vue devtools for the first time.
After all that, you should start seeing your components in "Vue" tab of developer tools. You might see Anonymous component for some components, but all you need is name: app which is something you are already doing in your App.vue component.
I had the same issue! Not much to find scratching around for help, but this worked for me:
change the webpack.config.js setting for NODE_ENV: '"production"' to NODE_ENV: '"development"'
Seems almost too obvious but the rendered Vue app(s) then appeared like magic showing all the object goodness!