I don't understand the exact purpose of each file.
Suppose, I want to put authentication code, where should I place it, in main.js or App.vue
main. js contains the JavaScript to initialise a Vue app. App. vue contains the root component of a Vue app.
Vue Single-File Components (a.k.a. *.vue files, abbreviated as SFC) is a special file format that allows us to encapsulate the template, logic, and styling of a Vue component in a single file.
VueJS is primarily used to build web interfaces and one-page applications. In saying that, it can also be applied to both desktop and mobile app development thanks to the HTML extensions and JS base working in tandem with an Electron framework – making it a heavily favoured frontend tool.
Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue.
I believe you might be missing on some of the basics behind the structure of VueJS and where and/or how to put in functionality like authentication. It might be worth going through their Introduction again to solidify your knowledge.
To answer more directly, when you run a Vue JS application you need to have a basic html page (like index.html
) as an entry point and the intialisation for your Vue app loaded in a <script>
in that page.
When you write a Vue JS application you can choose to do it in pure JavaScript, in TypeScript or in the .vue component format which combines the HTML, CSS and JavaScript you need to define components.
The vue
format is not run directly, it has to be transpiled into plain JavaScript by the Vue-CLI/builder and packed using a packager like WebPack first and then loaded by your entry point. Luckily the Vue CLI handles nearly all of this process so you can get on with building.
App.vue
This is typically the root of your application defined in Vue Component file format. It's usually something that defines the template for your page:
<template>
<div id="app">
<SideBar />
<router-view v-if="loaded" />
</div>
</template>
<script>
import SideBar from "./pages/SideBar";
export default {
components: { SideBar },
computed: {
loaded() {
return this.$store.state.loadState == "loaded";
}
}
};
</script>
main.js
This is usually the JavaScript file that will initialise this root component into a element on your page. It is also responsible for setting up plugins and 3rd party components you may want to use in your app:
import Vue from "vue";
import { store } from "./store/store";
import router from "./router";
import App from "./App.vue";
new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
index.html
The index page provides your entry point in html providing an element for VueJs to load into and imports main.js
to intialise your app.
<!-- the html element that hosts the App.vue component -->
<div id="app"></div>
<!-- built files will be auto injected -->
<script type="text/javascript" src="main.js"></script>
On another note a decent place to put your authentication logic is in the router where you can add navigation guards to restrict access to pages based on the current authentication state and send your users to a login page:
// GOOD
router.beforeEach((to, from, next) => {
if (!isAuthenticated) next('/login')
else next()
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With